File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Swing / AWT / SWT and the fly likes gridbagconstraints problem Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Swing / AWT / SWT
Bookmark "gridbagconstraints problem" Watch "gridbagconstraints problem" New topic

gridbagconstraints problem

alun groome

Joined: Jan 21, 2005
Posts: 21
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();

cn.add(a,c); //button

cn.add(b,c); //button

cn.add(ca,c); //button

cn.add(a,c); //button

cn.add(panelb,c); //panel

cn.add(panela,c); //panel
Craig Wood
Ranch Hand

Joined: Jan 14, 2004
Posts: 1535
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.
I agree. Here's the link:
subject: gridbagconstraints problem
jQuery in Action, 3rd edition