Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Java > aligning components within boxes

Reply
Thread Tools

aligning components within boxes

 
 
Duane Evenson
Guest
Posts: n/a
 
      07-02-2008
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();
}
});
}
}

 
Reply With Quote
 
 
 
 
Ian Shef
Guest
Posts: n/a
 
      07-02-2008
Duane Evenson <(E-Mail Removed)> wrote in
news(E-Mail Removed) s:

> 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/CoreJavaTechTip...know_boxlayout

and especially the class AlignX2, where each of the three JButton gets its
alignment set before being added to the Box.




 
Reply With Quote
 
 
 
 
Knute Johnson
Guest
Posts: n/a
 
      07-02-2008
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
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Space between input boxes and selection boxes is not the same in Internet Explorer Stefan Mueller HTML 5 06-16-2009 02:06 PM
Vertically Aligning Images within a <div> Marc Bradshaw HTML 18 09-17-2008 03:35 PM
SWING components adjustment in different resolutions - Should show scrollbars less than 800X600 and expand components over this resolution Bluetears76 Java 1 07-01-2004 09:01 PM
Can Choice components respond to keyboard input like HTML Choice components? Mickey Segal Java 0 02-02-2004 10:59 PM
Aligning text within a JTextArea Phillip Java 2 01-23-2004 04:03 PM



Advertisments