aspose file tools*
The moose likes Developer Certification (SCJD/OCMJD) and the fly likes Adding Panels in Frame or in a Panel Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of The Java EE 7 Tutorial Volume 1 or Volume 2 this week in the Java EE forum
or jQuery UI in Action in the JavaScript forum!
JavaRanch » Java Forums » Certification » Developer Certification (SCJD/OCMJD)
Bookmark "Adding Panels in Frame or in a Panel" Watch "Adding Panels in Frame or in a Panel" New topic
Author

Adding Panels in Frame or in a Panel

Tahir Abbas
Ranch Hand

Joined: May 05, 2007
Posts: 44
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!
K. Tsang
Bartender

Joined: Sep 13, 2007
Posts: 2379
    
    7

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.


K. Tsang JavaRanch SCJP5 SCJD/OCM-JD OCPJP7 OCPWCD5
Roel De Nijs
Bartender

Joined: Jul 19, 2004
Posts: 5216
    
  12

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


SCJA, SCJP (1.4 | 5.0 | 6.0), SCJD
http://www.javaroe.be/
Tahir Abbas
Ranch Hand

Joined: May 05, 2007
Posts: 44
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
Bartender

Joined: Jul 19, 2004
Posts: 5216
    
  12

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
     
     
    subject: Adding Panels in Frame or in a Panel