aspose file tools*
The moose likes Swing / AWT / SWT and the fly likes jpanel into jframe Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Swing / AWT / SWT
Bookmark "jpanel into jframe" Watch "jpanel into jframe" New topic
Author

jpanel into jframe

sa sam
Ranch Hand

Joined: Mar 01, 2009
Posts: 46
i have jframe1. jframe having menu bar and i have 5 jpanel.

now i want a result like, when i select one menu item it will show me a panel (i.e. - jpanel1)
when i click on other menu item then it has to show other jpanel (i.e. - jpanel2) and jpanel1 get hide.
Maneesh Godbole
Saloon Keeper

Joined: Jul 26, 2007
Posts: 10523
    
    9

Ajay,
Please read this http://faq.javaranch.com/java/CarefullyChooseOneForum
This forum is for j2me related questions. I am moving this post for you to the Swing forum.

Check out card layout. You can have a listener to the menu items and based on user selection, you can switch the panels.


[How to ask questions] [Donate a pint, save a life!] [Onff-turn it on!]
Gregg Bolinger
GenRocket Founder
Ranch Hand

Joined: Jul 11, 2001
Posts: 15299
    
    6

Another option, if you'd prefer the Lazy Loading approach, is you can, on a menu item action, instantiate your new JPanel object and simply replace whatever the existing JPanel is for the main frame, and then call a repaint() so the UI updates with the new panel. If you don't care about Lazy Loading, Maneesh's approach is a good one.


GenRocket - Experts at Building Test Data
karthick Soundararaj
Greenhorn

Joined: Mar 09, 2009
Posts: 26
Its infact quite easy if I am right in getting your needs.

when you click one menu item, use the setVisible(Boolean) method to make one panel visible while the other one invisible.


A java addict
Gregg Bolinger
GenRocket Founder
Ranch Hand

Joined: Jul 11, 2001
Posts: 15299
    
    6

karthick Soundararaj wrote:Its infact quite easy if I am right in getting your needs.

when you click one menu item, use the setVisible(Boolean) method to make one panel visible while the other one invisible.


Unfortunately, it isn't really that simple because of layout issues and it doesn't solve the rancher's needs. I'd stick with Maneesh's approach or mine if you need lazy loading.
karthick Soundararaj
Greenhorn

Joined: Mar 09, 2009
Posts: 26
Well I have to agree with you that the layout manager issues would come in but if that is the only problem then the following 2 lines of code should definitely work

jPanel1.removeAll();
this.repaint();
Gregg Bolinger
GenRocket Founder
Ranch Hand

Joined: Jul 11, 2001
Posts: 15299
    
    6

karthick Soundararaj wrote:Well I have to agree with you that the layout manager issues would come in but if that is the only problem then the following 2 lines of code should definitely work

jPanel1.removeAll();
this.repaint();


This is more or less what I was talking about in my first post in this thread. That doesn't mean that setVisible is the right solution. But I understand what you mean now.
Maneesh Godbole
Saloon Keeper

Joined: Jul 26, 2007
Posts: 10523
    
    9

Gregg Bolinger wrote:Another option, if you'd prefer the Lazy Loading approach, is you can, on a menu item action, instantiate your new JPanel object and simply replace whatever the existing JPanel is for the main frame, and then call a repaint() so the UI updates with the new panel. If you don't care about Lazy Loading, Maneesh's approach is a good one.


Just to add to Gregg's solution here, you need to call revalidate() and repaint() for the UI to refresh properly.
takatoshi yanase
Greenhorn

Joined: Mar 04, 2009
Posts: 6
Hi,

The following URL of Java Applet is a sample of switching JPanels. Can you try it?
http://www.sys-audit.com/app1.html

Thanks
 
 
subject: jpanel into jframe