aligning components within boxes

Discussion in 'Java' started by Duane Evenson, Jul 2, 2008.

  1. I'm having trouble getting components to center align in a Box or JPanel
    with BoxLayout. The problem component seems to be a JTextField. Here is my
    code:

    // Text.java
    import java.awt.*;
    import javax.swing.*;

    public class Test extends JFrame {
    Test() {
    Box box = Box.createVerticalBox();
    box.setAlignmentX((float) 0.5);
    box.add(new JLabel("Client"));
    box.add(new JTextField(40));
    box.add(new JButton("Send"));

    Container cp = getContentPane();
    cp.add(box);
    setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    pack();
    setLocationRelativeTo(null);
    setVisible(true);
    }
    public static void main(String[] args) {
    javax.swing.SwingUtilities.invokeLater(new Runnable() {
    public void run() {
    new Test();
    }
    });
    }
    }
     
    Duane Evenson, Jul 2, 2008
    #1
    1. Advertising

  2. Duane Evenson

    Ian Shef Guest

    Duane Evenson <> wrote in
    news:p:

    > I'm having trouble getting components to center align in a Box or JPanel
    > with BoxLayout. The problem component seems to be a JTextField. Here is

    my
    > code:
    >
    > // Text.java
    > import java.awt.*;
    > import javax.swing.*;
    >
    > public class Test extends JFrame {
    > Test() {
    > Box box = Box.createVerticalBox();
    > box.setAlignmentX((float) 0.5);
    > box.add(new JLabel("Client"));
    > box.add(new JTextField(40));
    > box.add(new JButton("Send"));
    >
    > Container cp = getContentPane();
    > cp.add(box);
    > setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    > pack();
    > setLocationRelativeTo(null);
    > setVisible(true);
    > }
    > public static void main(String[] args) {
    > javax.swing.SwingUtilities.invokeLater(new Runnable() {
    > public void run() {
    > new Test();
    > }
    > });
    > }
    > }
    >


    I haven't tested your example so this may be all wrong, but...

    I thought that
    box.setAlignmentX((float) 0.5)
    sets the desired alignment for box within its Container cp.
    If you want to center the JLabel, JTextField, and JButton within box, you
    need to use setAlignmentX(...) on each of them.

    See "Getting to Know BoxLayout" at
    http://blogs.sun.com/CoreJavaTechTips/entry/getting_to_know_boxlayout

    and especially the class AlignX2, where each of the three JButton gets its
    alignment set before being added to the Box.
     
    Ian Shef, Jul 2, 2008
    #2
    1. Advertising

  3. Duane Evenson wrote:
    > I'm having trouble getting components to center align in a Box or JPanel
    > with BoxLayout. The problem component seems to be a JTextField. Here is my
    > code:
    >
    > // Text.java
    > import java.awt.*;
    > import javax.swing.*;
    >
    > public class Test extends JFrame {
    > Test() {
    > Box box = Box.createVerticalBox();
    > box.setAlignmentX((float) 0.5);
    > box.add(new JLabel("Client"));
    > box.add(new JTextField(40));
    > box.add(new JButton("Send"));
    >
    > Container cp = getContentPane();
    > cp.add(box);
    > setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    > pack();
    > setLocationRelativeTo(null);
    > setVisible(true);
    > }
    > public static void main(String[] args) {
    > javax.swing.SwingUtilities.invokeLater(new Runnable() {
    > public void run() {
    > new Test();
    > }
    > });
    > }
    > }
    >


    Everybody else gave you excellent examples of how to fix your alignment
    problems with BoxLayout. I might suggest for a layout such as this that
    you use GridBagLayout. It has several advantages but the one I like
    best is that the components stay the same size if you increase the size
    of the frame. Not only that but I think it is a simpler layout and
    easier to use than BoxLayout.

    import java.awt.*;
    import javax.swing.*;

    public class Test extends JFrame {
    Test() {
    JPanel p = new JPanel(new GridBagLayout());
    GridBagConstraints c = new GridBagConstraints();
    c.gridx = 0;

    p.add(new JLabel("Client"),c);
    p.add(new JTextField(40),c);
    p.add(new JButton("Send"),c);

    Container cp = getContentPane();
    cp.add(p);
    setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    pack();
    setLocationRelativeTo(null);
    setVisible(true);
    }
    public static void main(String[] args) {
    javax.swing.SwingUtilities.invokeLater(new Runnable() {
    public void run() {
    new Test();
    }
    });
    }
    }

    --

    Knute Johnson
    email s/nospam/knute2008/

    --
    Posted via NewsDemon.com - Premium Uncensored Newsgroup Service
    ------->>>>>>http://www.NewsDemon.com<<<<<<------
    Unlimited Access, Anonymous Accounts, Uncensored Broadband Access
     
    Knute Johnson, Jul 2, 2008
    #3
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. =?Utf-8?B?R2FyeQ==?=
    Replies:
    2
    Views:
    469
    Wasi Rehman
    Oct 29, 2004
  2. Phillip
    Replies:
    2
    Views:
    3,221
  3. Mickey Segal
    Replies:
    0
    Views:
    913
    Mickey Segal
    Feb 2, 2004
  4. Stefan Mueller
    Replies:
    5
    Views:
    12,461
    jamesxa
    Jun 16, 2009
  5. Marc Bradshaw
    Replies:
    18
    Views:
    792
    Marc Bradshaw
    Sep 17, 2008
Loading...

Share This Page