Win a copy of Spring in Action (5th edition) 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 all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Bear Bibeault
  • Devaka Cooray
  • Liutauras Vilda
  • Jeanne Boyarsky
Sheriffs:
  • Knute Snortum
  • Junilu Lacar
  • paul wheaton
Saloon Keepers:
  • Ganesh Patekar
  • Frits Walraven
  • Tim Moores
  • Ron McLeod
  • Carey Brown
Bartenders:
  • Stephan van Hulst
  • salvin francis
  • Tim Holloway

gridbagconstraints problem  RSS feed

 
Greenhorn
Posts: 21
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi ,

im trying to use a gridbag constraing layout.

I want to add 2 Jpannels and 2 Jbuttons. But the panels do not appear on the screen just the buttons. All other layout managers add my panels fine.

any ideas?
JInternalFrame fr1 =
new JInternalFrame("app", true, true);
fr1.setBounds(0, 05, 900, 900);
Container cn = fr1.getContentPane();
cn.setLayout(new GridBagLayout());

GridBagConstraints c = new GridBagConstraints();

c.gridx=0;
c.gridy=0;
cn.add(a,c); //button

c.gridx=0;
c.gridy=1;
cn.add(b,c); //button

c.gridx=0;
c.gridy=2;
cn.add(ca,c); //button

c.gridx=0;
c.gridy=3;
cn.add(a,c); //button

c.gridx=1;
c.gridy=1;
cn.add(panelb,c); //panel

c.gridx=1;
c.gridy=2;
cn.add(panela,c); //panel
 
Ranch Hand
Posts: 1535
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
GridBagLayout respects the preferred size of the components it is given to layout. It asks each component for the size it requires for proper display. In turn the component will get this information from its layout manager which asks the child components of the component (lots of components in here). If the component has no children then it will report a default size such as (0,0) or (10,10). You can specify size hints for your JPanels with the setPreferredSize method (which is also a Component (AWT) method in j2se 1.5). The gridbag layout manager can use this to display your panels. A trick you can use is to add an etched border or background color to your panels to track them as you build up your gui. You can also control the display size of components with combinations of the weight and fill constraints but this is more advanced.
 
Don't get me started about those stupid light bulbs.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!