aligning components within boxes

D

Duane Evenson

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

Ian Shef

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.
 
K

Knute Johnson

Duane said:
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();
}
});
}
}
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,009
Latest member
GidgetGamb

Latest Threads

Top