Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / General / September 2007

Tip: Looking for answers? Try searching our database.

vertical BoxLayout, JLabel, and JButton and alignment

Thread view: 
Duane Evenson - 26 Sep 2007 23:10 GMT
If a button is in a jPanel with vertical BoxLayout, as soon as a label is
added to the panel, the button aligns itself with the left edge of the
label. Also the label doesn't align itself with the left edge of the
panel--it's indented an amount.

Why?

I want the button to stay put in the center after a label is added to the
panel. I also want the labels to start at the left edge of the panel.
Roedy Green - 26 Sep 2007 23:45 GMT
On Wed, 26 Sep 2007 22:10:27 GMT, Duane Evenson
<duane@invalid.address> wrote, quoted or indirectly quoted someone who
said :

>I want the button to stay put in the center after a label is added to the
>panel. I also want the labels to start at the left edge of the panel.

see http://java.sun.com/docs/books/tutorial/uiswing/layout/box.html
Signature

Roedy Green Canadian Mind Products
The Java Glossary
http://mindprod.com

Duane Evenson - 27 Sep 2007 11:43 GMT
> If a button is in a jPanel with vertical BoxLayout, as soon as a label is
> added to the panel, the button aligns itself with the left edge of the
[quoted text clipped - 5 lines]
> I want the button to stay put in the center after a label is added to the
> panel. I also want the labels to start at the left edge of the panel.

Sorry, missing info...
The panel also needs a JComboBox for the components to act wierd, although
the box itself doesn't move.
Andrew Thompson - 27 Sep 2007 12:28 GMT
...
>Sorry, missing info...

Don't be sorry, be smart.  Post an SSCCE.
<http://www.physci.org/codes/sscce.html>

Signature

Andrew Thompson
http://www.athompson.info/andrew/

RedGrittyBrick - 27 Sep 2007 19:52 GMT
> ..
>> Sorry, missing info...
>
> Don't be sorry, be smart.  Post an SSCCE.
> <http://www.physci.org/codes/sscce.html>

I'm not the OP, Duane, but I think he means this:
Compile, run, resize it.
---------------------8<-------------------------------------
import javax.swing.BoxLayout;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.SwingUtilities;

public class VerticalOddity {

    public static void main(String[] args) {
        SwingUtilities.invokeLater(new Runnable() {
            public void run() {
                new VerticalOddity();
            }
        });
    }

    VerticalOddity() {
        String[] list = {"apples", "pears", "oranges"};
        JComboBox box = new JComboBox(list);
        JButton button = new JButton("Button");
        JLabel label1 = new JLabel("label 1");
        JLabel label2 = new JLabel("label 2");

        // X marks the spot

        JPanel p = new JPanel();
        p.setLayout(new BoxLayout(p, BoxLayout.PAGE_AXIS));
        p.add(box);
        p.add(button);
        p.add(label1);
        p.add(label2);

        JFrame f = new JFrame("Vertical Layout Oddity");
        f.add(p);
        f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        f.pack();
        f.setVisible(true);
    }

}
------------------------------8<------------------------------------------
Part of the answer is to add the following at spot "X" above
        JComponent[] cs = new JComponent[] {box,button,label1,label2};
        for (JComponent c: cs)
            c.setAlignmentX(Component.LEFT_ALIGNMENT);

However I'm a little intriuged at the eagerness of JComboBox to expand!
RedGrittyBrick - 27 Sep 2007 20:33 GMT
> Part of the answer is to add the following at spot "X" above
>         JComponent[] cs = new JComponent[] {box,button,label1,label2};
>         for (JComponent c: cs)
>             c.setAlignmentX(Component.LEFT_ALIGNMENT);

Oops, whilst boggling at the JComboBox I failed to notice I hadn't
really addressed the OP's problem

The Op might enjoy reading
http://java.sun.com/docs/books/tutorial/uiswing/layout/box.html#features

I suspect the OP wants something more like
----------------------------8<-------------------------------------
import javax.swing.BoxLayout;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JComponent;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.SwingUtilities;

public class VerticalOddity {

    public static void main(String[] args) {
        SwingUtilities.invokeLater(new Runnable() {
            public void run() {
                new VerticalOddity();
            }
        });
    }

    VerticalOddity() {

        // smallest components
        JComboBox box = new JComboBox(new String[]
                {"apples", "pears", "oranges"});
        JButton button = new JButton("Button");
        JLabel label1 = new JLabel("label 1");
        JLabel label2 = new JLabel("label 2");

    // collect all but button into a left aligned container
        JPanel p1 = new JPanel();
        p1.setLayout(new BoxLayout(p1, BoxLayout.PAGE_AXIS));
        for (JComponent c: new JComponent[] {box, label1, label2}) {
            c.setAlignmentX(JComponent.LEFT_ALIGNMENT);
            p1.add(c);
        }

        // assemble container and button into a centered container
        JPanel p = new JPanel();
        p.setLayout(new BoxLayout(p, BoxLayout.PAGE_AXIS));
        for (JComponent c: new JComponent[] {p1, button}) {
            c.setAlignmentX(JComponent.CENTER_ALIGNMENT);
            p.add(c);
        }

    // add outer container to a frame
        JFrame f = new JFrame("Vertical Layout Oddity");
        f.add(p);
        f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        f.pack();
        f.setVisible(true);
    }
}
-------------------------------8<---------------------------------------

I still haven't worked out why the JComboBox has an inordinate
propensity for growth.


Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.