Working with GridLayouts

Discussion in 'Mac Programming' started by Uma888, Mar 21, 2006.

  1. macrumors 6502

    Joined:
    Jan 10, 2005
    Location:
    Birmingham, United Kingdom
    #1
    Hello. Im working with layouts.

    I have 5 labels, 4 buttons and a few checkboxes and a few optionboxes.

    im having trouble arranging them.

    im using the following code:

    Code:
    import javax.swing.*;
    import java.awt.*;
    public class ShowComponents extends JFrame
    {
    	public ShowComponents (String title)
    	{
    		super(title);
    		Container contentPane = getContentPane();
    				
    		//create components
    		JLabel label1 = new JLabel("Printer: HP Laser Jet 4000 Series PS");
    		JLabel label2 = new JLabel("");
    		JLabel label3 = new JLabel("");
    		JCheckBox checkbox1 = new JCheckBox("Image");
    		JRadioButton radiobutton1 = new JRadioButton("Selection");
    		JButton button1 = new JButton("OK");
    		JCheckBox checkbox2 = new JCheckBox("Text");
    		JRadioButton radiobutton2 = new JRadioButton("All");
    		JButton button2 = new JButton("Cancel");
    		JCheckBox checkbox3 = new JCheckBox("Code");
    		JRadioButton radiobutton3 = new JRadioButton("Applet");
    		JButton button3 = new JButton("Setup");
    	   JLabel label4 = new JLabel("");
    		JLabel label5 = new JLabel("");
    		JButton button4 = new JButton("Help");
    		JLabel label6 = new JLabel("Print Quality");
    		JComboBox combo1 = new JComboBox(new String[] {"Option 1", "Option 2"});
    		JCheckBox checkbox4 = new JCheckBox("Print To File");
    		//add components to the container
    		contentPane.setLayout(new GridLayout(10, 10));
    		contentPane.add(label1);
    		contentPane.add(label2);
    		contentPane.add(label3);
    		contentPane.add(label4);
    		contentPane.add(label5);
    		contentPane.add(checkbox1);
    		contentPane.add(checkbox2);
    		contentPane.add(checkbox3);
    		contentPane.add(checkbox4);
    		contentPane.add(radiobutton1);
    		contentPane.add(radiobutton2);
    		contentPane.add(radiobutton3);
    		contentPane.add(button1);
    		contentPane.add(button2);
    		contentPane.add(button3);
    		contentPane.add(button4);
    		contentPane.add(combo1);
    		//close and exit window on click
    		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    		pack();
    		setVisible(true);
    	}
    	
    	public static void main(String[] args)
    	{
    		ShowComponents frame = new ShowComponents("Show Components");
    	}
    }
    The thing i i want them to appear in this way:

    [​IMG]

    ^^ CLick to see the image

    Any ideas?
     
  2. thread starter macrumors 6502

    Joined:
    Jan 10, 2005
    Location:
    Birmingham, United Kingdom
    #2
    Fixed it

    I needed to add the components in order, i.e the order i anted em in. I had to set the grid to (3,3)
     

Share This Page