I am developing a GUI using GridBagLayout
for a panel which contains several other panels. One of the panels contain only a single button. I want this to appear horizontally following the upper two panels and not occupy height more than the button's height.
But somehow my grids are divided in rows of equal heights and the button row occupies almost ten times more height than itself.
Being a newbie in Swing, I am not able to fix this. Please suggest me the solution to this problem.
Following is the source code:
class SplitPane extends JFrame {
private static JPanel panel2;
private static JPanel panel5;
private static JScrollPane panel3;
private static JScrollPane panel4;
protected JSplitPane split;
public SplitPane(JChemPaintPanel p){
JFrame f = new JFrame("");
//f.setLayout(new GridLayout(3, 2, 10, 10 ));
// f.addWindowListener(new JChemPaintPanel.AppCloser());
// f.setDefaultCloseOperation(JFrame.DO_NOTHING_ON_CLOSE);
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
f.setPreferredSize(new Dimension(800, 800));
JPanel pane = new JPanel(new GridBagLayout());
GridBagConstraints c = new GridBagConstraints();
c.gridx=0;
c.gridy=0;
c.fill=GridBagConstraints.BOTH;
c.weightx=1;
c.weighty=1;
c.gridheight=1;
c.gridwidth=1;
p.setPreferredSize( new Dimension( 100, 100 ) );
p.setMaximumSize(new Dimension(100, 100));
pane.add(p, c);
c.gridx=1;
c.gridy=0;
c.fill=GridBagConstraints.BOTH;
c.weightx=1;
c.weighty=1;
c.gridheight=1;
c.gridwidth=1;
pane.add(createPanel2(), c);
c.gridx=0;
c.gridy=1;
c.gridheight=1;
c.weightx=1;
c.weighty=1;
c.gridwidth=GridBagConstraints.REMAINDER;
c.anchor=GridBagConstraints.CENTER;
c.fill=GridBagConstraints.HORIZONTAL;
pane.add(createPanel5(), c);
c.gridx=0;
c.gridy=2;
c.gridheight=1;
c.gridwidth=1;
c.fill=GridBagConstraints.BOTH;
c.weightx=1;
c.weighty=1;
pane.add(createPanel3(), c);
c.gridx=1;
c.gridy=2;
c.gridheight=1;
c.gridwidth=1;
c.fill=GridBagConstraints.BOTH;
c.weightx=1;
c.weighty=1;
pane.add(createPanel4(), c);
f.add(pane);
f.pack();
Point point = GraphicsEnvironment.getLocalGraphicsEnvironment().getCenterPoint();
import java.awt.BorderLayout;
int w2 = 1000;
int h2 = 1000;
f.setLocation(point.x - w2, point.y - h2);
f.setVisible(true);
}
CreatePanel2 - Is a simple text editor CreatePanel3 - Has a scrollable table CreatePanel4 - Another scrollable table CreatePanel5 - Panel having a single button