PDA

View Full Version : Working with GridLayouts




Uma888
Mar 21, 2006, 08:43 AM
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:

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:

http://img1.imageshack.us/thumbnail.png (http://img103.imageshack.us/my.php?image=untitled12kb.gif)

^^ CLick to see the image

Any ideas?



Uma888
Mar 21, 2006, 09:29 AM
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)