• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

How to Align JCheckBox

 
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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" );
c.add(snap);

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 );
show();
}
public static void main( String args[] )
{
Excercise1208 app = new Excercise1208();
app.addWindowListener(
new WindowAdapter() {
public void windowClosing( WindowEvent e )
{
System.exit( 0 );
}
}
);
}
}
 
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hey,
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.
banana
 
Ranch Hand
Posts: 1492
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.

Enjoy,
Manfred.
 
David Roseberry
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Banana and Manfred,
Thanks. I will try these ideas.
[ January 13, 2002: Message edited by: David Roseberry ]
 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic