Hi again all. Having continued with my project I have managed to finish my Board() which is where my main game occurs. I have also created a main menu which is instantiated from my main() method and appears as soon as the program is run.

My current problem is invoking the Board() and adding it to the JFrame when my MouseListener is triggered from my MainMenu. Both Board and MainMenu inherit JPanel, so effectively, I'm trying to replace a JPanel based on the first panel's MouseListener.

I've tried to add a getInvoker() method to my MainMenu which is set using the class' constructor. This, along with a type cast has allowed me to target methods on MainClass from MainMenu. Although the message dialog that I've entered to appear when the Board is invoked, I can only see a grey JFrame when Board is instantiated in this way.

Here's the method that I've added to my MainClass to instantiate a Board JPanel.

public void addBoard(){
	Board board = new Board();

Here's the getInvoker() method I've added to my MainMenu.

private JFrame _invoker;
	public JFrame getInvoker(){
		return _invoker;
	public void setInvoker(JFrame _invoker){
		this._invoker = _invoker;

Here's where I've set the invoker variable in the MainMenu constructor. Note that I have specified the invoker when the class' constructor is called using MainMenu mainMenu = new MainMenu(this); from MainClass.

public MainMenu(JFrame invoker){ //Constructor

And here's the method callback from MainMenu that is triggered by the class' mouse listener.

private void switchToBoard(){ //Changes the Menu for Board
		 ((MainClass) this.getInvoker()).addBoard();

Sorry if this doesn't make much sense, but I appreciate it if someone would try and follow what I'm trying to do. If anyone has questions or needs more code examples, please let me know.



5 Years
Discussion Span
Last Post by Morley93

To clarify:
You have a JFrame with a JPanel subclass that displays a menu. When one of the menu items is selected you want to display another JPanel subclass that contains your board (and this replaces the menu panel?).
If so, I don't see the need for anything more than just adding the new panel (and removing or hiding the menu panel). The constructor for the board JPanel should contain or call evethung needed to populate the board. Depending on what layout manager you are using you may need to pack() or revalidate() your JFrame afterwards. I don't understand what the Invoker code is for, or why its needed.


Yes, that's exactly what I was trying to do! The invoker code is used in order to target a method on the menu's parent (JFrame MainClass) which adds JPanel Board to the JFrame.

Putting this.validate and board.requestFocus() into the addBoard() method of MainClass had the desired effect.

Many thanks!


The invoker code is used in order to target a method on the menu's parent (JFrame MainClass)...

OK, right. You really don't need the mutator or accessor methods in this case. Just declare the variable private final in the JPanel, set it in the constructor, then simply use it. (Unless you plan to move an menu instance between JFrames!).
You should make the parameter and variable type MainClass, not JFrame, then you don't need the dodgy cast.

This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.