• Post Reply Bookmark Topic Watch Topic
  • New Topic

Calling methods 'up the chain'

 
Sergey Schek
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
This may be in the wrong subforum since I didnt know what category to put this question under. It concerns some GUI's though, so itll go here for now.

Now, to my question:
Ive got 5 classes, a Main class that runs everything ( its the class I run my project through), a JFrame that holds a set of menu's, a JPanel for a menu that gets added to the first JFrame; a second JFrame that holds the main game window and a set of JPanels that shows the different parts of the game. What I want to do is have it that when a used clicks a button in JPanel 1 ( lets call it MainMenu from here on), I want to first off dispose of the parent JFrame, which is rather easy. Now, after that, I want the Main class to create the game JFrame, and to it add all the necessary JPanels. I can write a method in Main that will build the main game GUI, but I have no good idea on how to call that method from the MainMenu JPanel.
What are my options here?
 
Maneesh Godbole
Bartender
Posts: 11445
18
Android Eclipse IDE Google Web Toolkit Java Mac Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You really should not have have multiple instances of JFrame.
If you are interested in switching your UI to display something else, on some trigger, a button click in your case, you can always show/hide the panels.

Check out CardLayout. It would suit your requirement very nicely.
 
Sergey Schek
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thats not the question here. I havent quite sorted out the details of how things will be shown, but I know that the methods to initialize all the GUI's will be in Main, and thus need to be called from MainMenu.
Note, Im not using JButtons in my case since they werent quite as flexible for what I wanted to do.
 
Maneesh Godbole
Bartender
Posts: 11445
18
Android Eclipse IDE Google Web Toolkit Java Mac Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Well you did say
used clicks a button in JPanel 1


Looks like I misunderstood your problem.
I got got the impression that your problem was in switching the frame contents.
Can you please rephrase it?
 
Sergey Schek
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The user clicks/hits enter on a button in the MainMenu JPanel. This click then should call a method in the Main class which will then initialize the main game GUI, which insofar will just be another JFrame, but it does not necessarily *have* to be. Either way, I need some way to pass a call from MainMenu to Main.
The code is arranged as such:
Main.java
package UI:
|MainFrame.java
||-----MainMenu.java
|GameFrame.java
||-----Other JPanels

If I make the method in Main static I need to make all the panel's and frame's static, at least in the way Main is referencing them, which Id rather not do.
 
Paul Clapham
Sheriff
Posts: 21875
36
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If you have an instance of a class X (let's say) and you want to call a method of that instance, then you have to have a reference to it.

It's as simple as that.

This is true for anything in Java, not just Swing. So in your example if you have a MainMenuPanel instance and it needs to call a method of a Main instance, then it has to have a reference to that instance. Pass it to the MainMenuPanel via the constructor or via a setter method.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!