I have a question. I have one main frame, and to the left i have a sidebar with menus. My question is, is it possible to make another panel within the main frame, so that if menu1 is clicked, the related contents should be displayed to the second half of the main frame, and when other menus are pressed then obviously the relevant stuff according to what is selected. its a bit hard to explain, sorry. Has anyone got an idea, whether that is possible in Java with Eclipse?
Asked
Active
Viewed 3.4k times
2 Answers
7
yes this's pretty possible you have look at CardLayout, this LayoutManager could be provide the simple way how to implement switching betweens JPanel in the JFrame

mKorbel
- 109,525
- 20
- 134
- 319
-
Out of curiosity, how well does CardLayout scale? From what I can see, all of the JPanels are loaded and put in memory when the Frame is initialized. Would it be reliable to use this method for 100+ JPanels? – Alexandre Feb 15 '12 at 17:04
-
@PeekaySwitch: Try it with this [example](http://stackoverflow.com/a/5655843/230513) and see. – trashgod Feb 15 '12 at 17:09
-
should be possible, notice 100+ JPanel will be take huge amount of memory, I'd suggest not creating huge amount of components for any of programing languages, for why reason you want / needed that ... – mKorbel Feb 15 '12 at 17:12
-
-
In my main frame, now I have 2 JPanels. One to the left, used as a sidebar with menus. Another one which is to the right, and I set the layout of the JPanel to Card Layout. Now, how do I go about creating different cards. And when menu1 is clicked, then Card1 should be displayed and when menu2 is clicked, then Card2 should be displayed, and so on? – Feb 15 '12 at 17:22
-
@mKorbel Simply wondering, The approach I used with Winforms .Net where you had an application with 30+ Views was to create UserControls and create + swap them as needed. I liked that approach and wanted to replicate something with Java for School, what I ended up doing is having one JFrame that create+swap JPanels as needed. I just wanted to know if CardLayout would have been a better alternative, I'm always open to trying new things out! – Alexandre Feb 15 '12 at 17:30
4
Yes, you can add 2 JPanels to 1 frame.
JFrame frame = new JFrame();
JPanel pane1 = new JPanel();
JPanel pane2 = new JPanel();
frame.add(pane1, BorderLayout.WEST);
frame.add(pane2, BorderLayout.EAST);
-
Could I then create different classes with interfaces, and call them in pane2 when a particular button is pressed? is that possible? or how else could that be done? – Feb 15 '12 at 17:01
-
Yes, you can create a new object and implement JPanel, JButton etc... Also in there you can specify to add any elements directly to that panel, without having to complicate it in 1 class. Using MouseListeners then, creating static methods to set the new options etc. – Feb 15 '12 at 17:03
-
-
1You need to mention something, anything about layout managers. – Hovercraft Full Of Eels Feb 15 '12 at 17:14
-
@Legend JFrame has by default implemented (>=Java5) BorderLayout, this code put pane1 & pane2 to the BorderLayout.CENTER, then only pane2 could be visible on the screen, – mKorbel Feb 15 '12 at 17:15
-
Ahh sorry about that, I guess it would need BorderLayout.LEFT and BorderLayout.RIGHT – Feb 15 '12 at 17:19