jQuery in Action, 3rd edition
The moose likes Swing / AWT / SWT and the fly likes How to Align JCheckBox Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Swing / AWT / SWT
Bookmark "How to Align JCheckBox" Watch "How to Align JCheckBox" New topic

How to Align JCheckBox

David Roseberry

Joined: Jan 03, 2002
Posts: 4
I am trying to create an applet using class FlowLayout to display: 2 - JCheckBoxes (stacked) on the LEFT, 2 - JLabels/JTextFields(stacked) in the MIDDLE and 3 - JButtons(stacked) on the right.
I am having trouble aligning the JCheckBoxes so they align left. I know FlowLayout arranges components in a left-to-right flow but my Check Boxes do not line up. I have reviewed the API but can't figure this out.
I have tried the GridLayout to no avail.
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class Excercise1208 extends JFrame {
private JTextField t;
private JCheckBox snap, show;
private JLabel label1, label2;
private JTextField text1, text2;
private JButton okButton, cancelButton, helpButton;
private FlowLayout layout;
public Excercise1208()
super( "Align" );
Container c = getContentPane();
c.setLayout(new FlowLayout());
snap = new JCheckBox( "Snap To Grid" );

label1 = new JLabel( "X: " );
label1.setToolTipText( "This is label1" );
c.add( label1 );
text1 = new JTextField( 3 );
c.add( text1 );

okButton = new JButton( "Ok" );
c.add( okButton );
show = new JCheckBox( "Show Grid" );
c.add(show );

label2 = new JLabel( "Y: " );
c.add( label2 );
text2 = new JTextField( 3 );
c.add( text2 );

cancelButton = new JButton( "Cancel" );
c.add( cancelButton );

helpButton = new JButton( "Help" );
c.add( helpButton );
setSize( 300, 125 );
public static void main( String args[] )
Excercise1208 app = new Excercise1208();
new WindowAdapter() {
public void windowClosing( WindowEvent e )
System.exit( 0 );
Banana Tower

Joined: Nov 22, 2001
Posts: 5
I feel your pain. My suggestion is to get a handle on
a layout manager like gridlayout or even gridbadlayout. Its worth the pain required to learn these managers in the long run. Especially for relatively complicated GUIs. Another thing you might consider is putting your checkboxes in panels. I have heard this works in helping you align your controls and setting their sizes.
One more thing, take a look at a tool like JBuilder or Forte. You may despise the code they create but you can design your apps in such a way that you never really have to deal with that generated code and just concentrate on the handlers etc. Plus, their free in one form or another.
Manfred Leonhardt
Ranch Hand

Joined: Jan 09, 2001
Posts: 1492
Hi David,
You can't get the result you are looking for with a FlowLayout. Anytime you want something aligned you need some layout that has grid in it. Since the GridLayout itself makes all components the same size that won't work either. You have 2 approaches:
1. Use GridLayouts for each vertical stack (i.e., checkBoxes, labels, textfields, and buttons ) then put the label and textfield gridPanels together with a single Borderlayout using labels in the west and textfields in the center. Then use another Borderlayout to place checkBoxes (west), labels&textfields (center), and buttons (east) together into a single Panel. Then add this panel into the contentPane using a BorderLayout and placing it into the North section.
2. Use a GridBagLayout. It might seem confusing, but it can emulate any other layout manager. The code below uses the GridBagLayout.

David Roseberry

Joined: Jan 03, 2002
Posts: 4
Banana and Manfred,
Thanks. I will try these ideas.
[ January 13, 2002: Message edited by: David Roseberry ]
I agree. Here's the link: http://aspose.com/file-tools
subject: How to Align JCheckBox
It's not a secret anymore!