This week's book giveaway is in the Programmer Certification forum.
We're giving away four copies of OCP Oracle Certified Professional Java SE 21 Developer Study Guide: Exam 1Z0-830 and have Jeanne Boyarsky & Scott Selikoff on-line!
See this thread for details.
  • 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
  • Liutauras Vilda
  • Jeanne Boyarsky
  • paul wheaton
Sheriffs:
  • Ron McLeod
  • Devaka Cooray
  • Henry Wong
Saloon Keepers:
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
  • Tim Moores
  • Mikalai Zaikin
Bartenders:
  • Frits Walraven

Adding Panels in Frame or in a Panel

 
Ranch Hand
Posts: 45
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi to everyone,

I checked some examples of gui programs that extends JFrame, have multiple panels then these panels are added in Frame(Container c = getContentPane() then c.add(panelName),...). But my approach is little bit different.

I wrote gui files with extends JFrame approach. The files have multiple panels and these panels are finally added in another main panel then I get display that main panel (setContentPane(mainPanel)) so instead of putting panels in JFrame I am putting panels in another panel.

I am not sure which approach is better. I would like to know your opinion.

Thanks in advance
Best Regards!
 
Bartender
Posts: 3648
16
Android Mac OS X Firefox Browser Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hello there, are you using layout managers? Ideally, it all depends on how you want the GUI to look. Using a BorderLayout (JFrame default) as an example, you can have up to 5 panels (top, left, botton, right, center). Each panel again depend on what you want on it can have its own layout manager and panels. Therefore, it's really up to your imagination.

As for SCJD is concern, keep it simple. Use basic layout managers. If you are not good with GrigBagLayout, don't use it. Don't try to be too fancy. As for your case, you may want to reuse panel variables. Ultimately, it is the events from text fields, drop down menus, buttons that you need to worry about.
 
Sheriff
Posts: 11604
178
Hibernate jQuery Eclipse IDE Spring MySQL Database AngularJS Tomcat Server Chrome Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Tahir,

My MainWindow is also extending JFrame and I followed the same approach as you: panels adding to another panel and then calling setContentPane


Kind regards,
Roel
 
Tahir Abbas
Ranch Hand
Posts: 45
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks! K. Tsang and Roel De Nijs

My program's first window shows components to open application in required mode (Server/Client/SAlone). The main panel has Grid layout. (mainPanel.setLayout(new GridLayout(9,0))) other child panels has Flowlayout. Then I put all these panels in mainPanel.

This window will disappear after the selection of mode. Then a window with menu will appear that will have menu options. I'll try to get all processing in the same window(frame). Different panels according to required command will apear/disappear in the same frame. This time I dont know how to keep the vertical size of top most panel(that has menu) very thin. But I'd like to do this without the GridBagLayout.

I know many things I'm discussing here are of basic level but infact I'm building almost all gui once again just because of using setlayout(null) in first developed client side interface, so now dont want to repeat such a mistake.

Roel I felt confidence to know that you also used the same approach(Panels inside panel).

Best Regards!
 
Roel De Nijs
Sheriff
Posts: 11604
178
Hibernate jQuery Eclipse IDE Spring MySQL Database AngularJS Tomcat Server Chrome Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Tahir,

In my opinion you can't create a good MainWindow (with search criteria, jtable and some buttons) without using a GridBagLayout. But that's my opinion and of course GridBagLayout is not the most easy to use layout available in Java. But layouting and creating GUIs is simply one of the hardest things to do in Java (certainly compared with vb or vb.net)

I just used the following 3 layouts:
  • BorderLayout
  • FlowLayout
  • GridBagLayout


  • Kind regards,
    Roel
     
    If you open the box, you will find Heisenberg strangling Shrodenger's cat. And waving this tiny ad:
    Gift giving made easy with the permaculture playing cards
    https://coderanch.com/t/777758/Gift-giving-easy-permaculture-playing
    reply
      Bookmark Topic Watch Topic
    • New Topic