Win a copy of Microservices Testing (Live Project) this week in the Spring forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Tim Cooke
  • Ron McLeod
  • Jeanne Boyarsky
  • Paul Clapham
Sheriffs:
  • Liutauras Vilda
  • Henry Wong
  • Devaka Cooray
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • Al Hobbs
  • Carey Brown
Bartenders:
  • Piet Souris
  • Mikalai Zaikin
  • Himai Minh

jpanel into jframe

 
Ranch Hand
Posts: 46
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Bartender
Posts: 11497
19
Android Google Web Toolkit Mac Eclipse IDE Ubuntu Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Ranch Hand
Posts: 15304
6
Mac OS X IntelliJ IDE Chrome
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Greenhorn
Posts: 26
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Gregg Bolinger
Ranch Hand
Posts: 15304
6
Mac OS X IntelliJ IDE Chrome
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Posts: 26
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Ranch Hand
Posts: 15304
6
Mac OS X IntelliJ IDE Chrome
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Bartender
Posts: 11497
19
Android Google Web Toolkit Mac Eclipse IDE Ubuntu Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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.
 
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
reply
    Bookmark Topic Watch Topic
  • New Topic