Results 1 to 3 of 3
  1. #1
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,565
    Rep Power
    12

    Default Width of labels containing html

    When I add a JLabel to a Box its (preferred?) width is larger than I want when I have set the label's contents to a piece of html. Subsequent labels are pushed over to the right.

    In the following the north box illustrates the layout I want and the south box illustrates what I get.

    Java Code:
    import java.awt.BorderLayout;
    import java.awt.Dimension;
    
    import javax.swing.Box;
    import javax.swing.JFrame;
    import javax.swing.JLabel;
    import javax.swing.SwingUtilities;
    import javax.swing.UIManager;
    
    public class BoxEg extends JFrame {
    	
        public BoxEg(String title) {
            super(title);
    		
            Box box = Box.createHorizontalBox();
            box.add(new JLabel("H"));
            box.add(new JLabel("7"));
            box.add(new JLabel("6"));
            add(box, BorderLayout.NORTH);
    		
            box = Box.createHorizontalBox();
            box.add(new JLabel("<html>&hearts;"));
            //JLabel label = new JLabel("<html>&hearts;");
            //label.setPreferredSize(new Dimension(30, 30));
            //box.add(label);
            box.add(new JLabel("7"));
            box.add(new JLabel("6"));
            add(box, BorderLayout.SOUTH);
        }
    	
        public static void main(String[] args) {
            SwingUtilities.invokeLater(new Runnable() {
                @Override
                public void run() {
                    BoxEg test = new BoxEg("BoxEg");
                    test.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                    test.pack();
                    test.setLocationRelativeTo(null);
                    test.setVisible(true);
                }
            });
        }
    }

    Things are no better if I set the label's preferred width explicitly, so I'm wondering what I'm doing wrong.

  2. #2
    camickr is offline Senior Member
    Join Date
    Jul 2009
    Posts
    1,234
    Rep Power
    7

    Default

    A Box respects the minimum/maximum size of a component when possible. So components shrink/grow depending on the space available.

    It looks like when using HTML the maximum width is set to Integer.MAX so it grows to take all the available space.
    Java Code:
    JLabel label = new JLabel("<html>&hearts;");
    label.setMaximumSize( label.getPreferredSize() );
    Last edited by camickr; 04-22-2011 at 05:51 AM.

  3. #3
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,565
    Rep Power
    12

    Default

    Thank you very much!

    I was taken in by the BoxLayout API docs: "BoxLayout attempts to arrange components at their preferred widths (for horizontal layout)" with no mention of the maximum. (and I assumed "preferred" would mean ... preferred.) I should have guessed what was up when I saw what happened when you added another such label.

    Anyway, for anyone else who happens on this, Oracle's Tutorial is crystal clear about what happens "When a BoxLayout lays out components from top to bottom, it tries to size each component at the component's preferred height. If the vertical space of the layout does not match the sum of the preferred heights, then BoxLayout tries to resize the components to fill the space. The components either grow or shrink to fill the space, with BoxLayout honoring the minimum and maximum sizes of each of the components. Any extra space appears at the bottom of the container." I should have read it I guess...

    Thanks again.

Similar Threads

  1. Changing GUI labels from another class
    By porchrat in forum New To Java
    Replies: 15
    Last Post: 02-08-2011, 09:54 AM
  2. Printing labels to A4
    By eggy524 in forum New To Java
    Replies: 3
    Last Post: 09-23-2010, 04:53 PM
  3. size och swt labels
    By larsk in forum SWT / JFace
    Replies: 0
    Last Post: 10-04-2009, 12:01 PM
  4. Working with Labels on Panels.
    By vargihate in forum AWT / Swing
    Replies: 2
    Last Post: 01-04-2008, 04:09 AM
  5. Aligning Labels
    By Java Tip in forum Java Tip
    Replies: 0
    Last Post: 01-02-2008, 06:26 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •