• Post Reply Bookmark Topic Watch Topic
  • New Topic

Whom to setLayout JFrame or JPanel  RSS feed

 
Ganish Patil
Ranch Hand
Posts: 529
19
Chrome Eclipse IDE Hibernate Java jQuery MySQL Database Netbeans IDE Spring Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
In this example of BorderLayout, I've extended JFrame and set the set setLayout() method to JPanel. It run successfully as expected.

But when I was learning GridBagLayout manager the example given in JLS uses JFrame and gets its content pane by
and sets ComponentOrientation and GridBagLayoutto this pane like this . I'm not sure whether to set layout and ComponentOrientation to JPanel like given in my above example OR set layout to JFrame by getting its content pane like given in JLS example in which they didn't use JPanel. Which one should I go for and why? please can someone explain.

 
Ganish Patil
Ranch Hand
Posts: 529
19
Chrome Eclipse IDE Hibernate Java jQuery MySQL Database Netbeans IDE Spring Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I did set layout directly to JFrame without using JPanel worked similarly but which one should I use setting layouts to JFrame or JPanel(by adding JPanel to JFrame at the end when finished adding components to JPanel) ?
 
K. Tsang
Bartender
Posts: 3648
16
Firefox Browser Java Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Personally I prefer to set layout on JPanel, do stuff to it, then add the JPanel to the JFrame.

That way JFrame can have multiple JPanels. Yet setting the layout for JFrame to fit all those JPanels is also needed.

JPanels can have inner JPanels if needed. But I don't recommend that.
 
Ganish Patil
Ranch Hand
Posts: 529
19
Chrome Eclipse IDE Hibernate Java jQuery MySQL Database Netbeans IDE Spring Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Tsang Thank you, yes I set layout to JPanel and then added JPanel to JFrame. I did FlowLayout, BorderLayout, GridLayout, GridBagLayout using notepad. GridBagLayout was little difficult to understand but after lot of practice was able to design form. Do I have to learn other layouts also which are given in JLS ? I'm going to use NetBeans IDE 8.0.2 for designing. are these enough or should learn something more if yes please let me know which one.
 
Campbell Ritchie
Sheriff
Posts: 55351
157
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I never knew there were layouts in the JLS. You can read about the layouts in the Java™ Tutorials. If you can work grid bag you are all right, and needn't use anything else. Find out about Cay Horstmann's GBC class which make grid bag much easier to use.
You don't need any IDEs to use layouts. NetBeans has a GUI builder which uses group layout automatically.
 
Ganish Patil
Ranch Hand
Posts: 529
19
Chrome Eclipse IDE Hibernate Java jQuery MySQL Database Netbeans IDE Spring Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I never knew there were layouts in the JLS
sorry I meant The Java™ Tutorials. thank you for package Cay Horstmann's GBC class. Do you mean to say that If I use NetBeans I don't have to worry about layouts while designing the interface of my desktop application ? because I'm going to use NetBeans for desktop applications so I'll not code it by hand, it will take much time to complete my projects. Actually why I'm learning layouts is, I'd developed a desktop application on a laptop and after finishing that application copied the project and ran same project on other laptop which had small screen and had low resolution( I think 1200 X 800 ) compared to previous laptop so the 1/4 of application interface was hiding, couldn't see completely & properly, perhaps that was because I didn't set layouts properly.
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!