Results 1 to 9 of 9
Thread: Multiple Views
- 04-08-2010, 05:14 PM #1
Member
- Join Date
- Mar 2010
- Posts
- 8
- Rep Power
- 0
Multiple Views
I have an application that needs to display different views in the same JFrame.
I tried creating a class which loads the JFrame and then have classes which inherit JPanel for each view I want to display.
I can load the first view (MainMenu) by adding it to the JFrame but I can't figure out how to add the next view to the JFrame when the user clicks a button in the MainMenu JPanel.
- 04-08-2010, 07:01 PM #2
JFrame2.setVisible(true);
JFrame1.setVisible(false);
Is that what you mean?
-
Try using a CardLayout
I think that you want to look up the Sun Swing tutorial that covers CardLayout as it will allow you to swap JPanels or any JComponent easily. You can find it here: CardLayout tutorial
Last edited by Fubarable; 04-08-2010 at 09:29 PM. Reason: Link added
- 04-09-2010, 04:08 AM #4
Member
- Join Date
- Mar 2010
- Posts
- 8
- Rep Power
- 0
Cheers for the link but I still have the same problem.
The GUI class has the cardLayout and controls what is loads into the JFrame.
The mainMenu class loads at first but I need to make it so when a button is clicked in mainMenu is tells the GUI class to load the next view.
-
If you give your gui classes the appropriate public methods and allow your controller code (the action listeners for the menus) have a reference to the gui's, I think that you can solve this. If not, consider creating an sscce (see my link in my signature below) and post it here.
- 04-09-2010, 04:48 PM #6
Member
- Join Date
- Mar 2010
- Posts
- 8
- Rep Power
- 0
Thanks for the help, I've finally solved the problem...just for reference this is what I did.
I was able to solve the problem using "singleton pattern" for the GUI class since I only ever need one instance of it.
I added a getInstance() and changeView() method to the GUI class which allowed me to change the view of the GUI instance from any other class via GUI.getInstance().changeView()
-
- 04-09-2010, 07:55 PM #8
Member
- Join Date
- Mar 2010
- Posts
- 8
- Rep Power
- 0
- 04-09-2010, 09:20 PM #9
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 11,399
- Blog Entries
- 7
- Rep Power
- 17
The way I would do it is define a 'View' interface:
All view implementations would implement this interface; when I really go fancy I define a ViewFactory that gives my a View given an ID or whatever:Java Code:public interface View { public void display(Data data); // whatever Data may be // more methods here ... }
Note that this class is abstract and just contains a static method that gives me the View I want; there is no need for more fancy patterns such as abstract factories etc.Java Code:public abstract class ViewFactory { public static View createView(ID id) { ... } }
Anything that needs a View can consult the ViewFactory and voila.
kind regards,
Jos
Similar Threads
-
multiple views in one folder tab in RCP app
By natelapp in forum EclipseReplies: 1Last Post: 08-02-2009, 11:48 AM -
Multiple views on user interface application
By dand_dd in forum SWT / JFaceReplies: 36Last Post: 04-24-2009, 09:05 AM -
Relation between 2 views
By tojas in forum SWT / JFaceReplies: 0Last Post: 04-19-2009, 03:22 AM -
List views, a type of object
By Leprechaun in forum New To JavaReplies: 2Last Post: 02-06-2008, 03:07 AM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks