GridBagLayout always in center of JPanel
843807Oct 2 2005 — edited Oct 5 2005I cannot seem to find a way to accomplish what I am trying to do... In the example code, there is a grid layout with 2 Jpanels, each with its own gridbaglayout. So, the first panel has two buttons, one on top of the other, and the second panel has one button. I am trying to get the second panel's single button to align at the top of the panel, without any space before it (as in the first panels first button).... Can anyone help?
Here's the code......
public class NewJFrame extends javax.swing.JFrame {
public NewJFrame() {
initComponents();
}
private void initComponents() {
java.awt.GridBagConstraints gridBagConstraints;
jPanel1 = new javax.swing.JPanel();
jPanel2 = new javax.swing.JPanel();
jButton1 = new javax.swing.JButton();
jButton2 = new javax.swing.JButton();
jPanel3 = new javax.swing.JPanel();
jButton3 = new javax.swing.JButton();
setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
jPanel1.setLayout(new java.awt.GridLayout(1, 2));
jPanel2.setLayout(new java.awt.GridBagLayout());
jButton1.setText("jButton1");
jPanel2.add(jButton1, new java.awt.GridBagConstraints());
jButton2.setText("jButton2");
gridBagConstraints = new java.awt.GridBagConstraints();
gridBagConstraints.gridx = 0;
gridBagConstraints.gridy = 1;
jPanel2.add(jButton2, gridBagConstraints);
jPanel1.add(jPanel2);
jPanel3.setLayout(new java.awt.GridBagLayout());
jButton3.setText("jButton3");
jPanel3.add(jButton3, new java.awt.GridBagConstraints());
jPanel1.add(jPanel3);
getContentPane().add(jPanel1, java.awt.BorderLayout.CENTER);
pack();
}
public static void main(String args[]) {
java.awt.EventQueue.invokeLater(new Runnable() {
public void run() {
new NewJFrame().setVisible(true);
}
});
}
private javax.swing.JButton jButton1;
private javax.swing.JButton jButton2;
private javax.swing.JButton jButton3;
private javax.swing.JPanel jPanel1;
private javax.swing.JPanel jPanel2;
private javax.swing.JPanel jPanel3;
}