Reputation: 47
I created one program consisting of 3 columns. Now I want only 2nd column to increase its size. Can anyone suggest me the right code? Here is my code
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;
import java.awt.TextField;
import javax.swing.BorderFactory;
import javax.swing.Box;
import javax.swing.ButtonGroup;
import javax.swing.JButton;
import javax.swing.JCheckBox;
import javax.swing.JComboBox;
import javax.swing.JComponent;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JRadioButton;
import javax.swing.JTextArea;
import javax.swing.JTextField;
public class PizzaGridBagLayout extends JFrame {
public static void main(String[] args) {
new PizzaGridBagLayout();
}
JTextField name = new JTextField(10), phone = new JTextField(10), pup=new JTextField(10);JComboBox address = new JComboBox(new String[]{"ComboBox 1","hi","hello"});
JComboBox address1 = new JComboBox(new String[]{"ComboBox 2","hi","hello"});
JButton labels=new JButton("labels");
JLabel label1 = new JLabel("label1");
JLabel label2 = new JLabel("label2");
JLabel label3 = new JLabel("Label3");
JLabel label4 = new JLabel("Label4");
JLabel label5 = new JLabel("Label5");
JTextArea area=new JTextArea(1,10);
JTextArea area1=new JTextArea(1,10);
JRadioButton yes = new JRadioButton("yes"),
no = new JRadioButton("no");
JCheckBox box1 = new JCheckBox("box1"), box2 = new JCheckBox("box2"),
box3 = new JCheckBox("box3");
JButton okButton = new JButton("OK"), closeButton = new JButton("Close");
public PizzaGridBagLayout() {
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
// HERE I WANT TO INCREASE THE SIZE OF 2nd COLUMN CONSISTING OF NAME,PHONE,ADDRESS,ADDRESS1,OKBUTTON //
JPanel panel1 = new JPanel();
panel1.setLayout(new GridBagLayout());
addItem(panel1, name, 1, 0,1);
addItem(panel1, phone, 1, 2,1);
addItem(panel1, address, 1, 1,1);
addItem(panel1, address1, 1, 4,1);
addItem(panel1, okButton, 1, 5,1);
Box sizeBox = Box.createVerticalBox();
addItem(panel1, label1, 0, 0,1);
addItem(panel1, label2, 0, 1,1);
addItem(panel1, label3, 0, 2,1);
addItem(panel1, label4, 0, 3,1);
addItem(panel1, label5, 0, 4,1);
Box styleBox = Box.createHorizontalBox();
styleBox.add(yes);
styleBox.add(no);
styleBox.setBorder(BorderFactory.createTitledBorder(""));
addItem(panel1, styleBox, 1, 3,1);
Box topBox = Box.createVerticalBox();
ButtonGroup topGroup = new ButtonGroup();
topBox.add(box1);
topBox.add(box2);
topBox.setBorder(BorderFactory.createTitledBorder(""));
addItem(panel1, topBox, 2, 0,2);
addItem(panel1, pup,2,2,1 );
addItem(panel1, area, 2, 4,1);
addItem(panel1,closeButton,2,5,1);
this.add(panel1);
this.pack();
this.setVisible(true);
}
private void addItem(JPanel p, JComponent c, int x, int y,int height) {
GridBagConstraints gc = new GridBagConstraints();
gc.gridx = x;
gc.gridy = y;
gc.gridheight=height;
gc.weightx = 1.0;
gc.weighty = 1.0;
gc.insets = new Insets(2, 2, 2, 2);
gc.fill = GridBagConstraints.BOTH;
p.add(c, gc);
}
private void addItem1(JPanel p, JComponent c, int x, int y, int z, int a){
GridBagConstraints gc1 = new GridBagConstraints();
gc1.gridwidth=z;
gc1.gridheight=a;
gc1.fill=GridBagConstraints.NONE;
p.add(c,gc1);
}
}
This is the output I'm getting
If I enlarge it 1st column is expanding but I want 2nd column to expand. Can anyone suggest me. Thanks in advance.
Upvotes: 2
Views: 100
Reputation: 324098
gc.weightx = 1.0;
The weightx constraint controls this. You set the value to 1.0 for all columns so each column gets the extra space.
You want the value to be:
Read the section from the Swing tutorial on How to Use GridBagLayout for more information about the constraints.
Upvotes: 1