2 Replies Latest reply: Sep 12, 2012 11:15 AM by Jörg RSS

    BoxLayout alignment

    Jörg
      Hello,

      the following code shows four "column" panels, each having a BoxLayout.Y_AXIS and a header label inside. In the tutorial I read:
      "In general, all the components controlled by a top-to-bottom BoxLayout object should have the same X alignment."
      Although a should is not a must, the code fulfills this requirement. Still the Total header is not right justified and the
      Quantity header is not centered, also when removing the html tags. Any idea why?
      import java.awt.*;
      import javax.swing.*;
      
      public class ColumnsWithHeader extends JFrame {
      
        public ColumnsWithHeader() {
          setSize(460,200);
          setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
          JPanel panel= new JPanel();
          JPanel pNumber= new JPanel();
          JPanel pTotal= new JPanel();
          JPanel pQuantity= new JPanel();
          JPanel pRemark= new JPanel();
          pNumber.setLayout(new BoxLayout(pNumber, BoxLayout.Y_AXIS));
          pTotal.setLayout(new BoxLayout(pTotal, BoxLayout.Y_AXIS));
          pQuantity.setLayout(new BoxLayout(pQuantity, BoxLayout.Y_AXIS));
          pRemark.setLayout(new BoxLayout(pRemark, BoxLayout.Y_AXIS));
          panel.add(pNumber);
          panel.add(pTotal);
          panel.add(pQuantity);
          panel.add(pRemark);
      
          Dimension dimNumber= new Dimension(45,20);
          Dimension dimTotal= new Dimension(50,20);
          Dimension dimQuantity= new Dimension(87,20);
          Dimension dimRemark= new Dimension(230,20);
          JLabel lb= new JLabel("<html><u>Number</u></html>");
          lb.setAlignmentX(Component.RIGHT_ALIGNMENT);
          lb.setPreferredSize(dimNumber);
          lb.setMaximumSize(dimNumber);
          pNumber.add(lb);
          lb= new JLabel("<html><u>Total</u></html>");
          lb.setAlignmentX(Component.RIGHT_ALIGNMENT);
      //    lb.setAlignmentX(Component.CENTER_ALIGNMENT); // works
          lb.setPreferredSize(dimTotal);
          lb.setMaximumSize(dimTotal);
          pTotal.add(lb);
          lb= new JLabel("<html><u>Quantity</u></html>");
          lb.setAlignmentX(Component.CENTER_ALIGNMENT);
          lb.setPreferredSize(dimQuantity);
          lb.setMaximumSize(dimQuantity);
          pQuantity.add(lb);
          lb= new JLabel("<html><u>Remark</u></html>");
          lb.setAlignmentX(Component.LEFT_ALIGNMENT);
          lb.setPreferredSize(dimRemark);
          lb.setMaximumSize(dimRemark);
          pRemark.add(lb);
      
          int icnt= 3;
          for (int i=0; i<icnt; i++) {
           int j= new Double(Math.pow(10, i+1)).intValue();
           lb= new JLabel(""+j, SwingConstants.RIGHT);
           lb.setAlignmentX(Component.RIGHT_ALIGNMENT);
           lb.setPreferredSize(dimNumber);
           lb.setMaximumSize(dimNumber);
           pNumber.add(lb);
           lb= new JLabel(""+j*2, SwingConstants.RIGHT);
           lb.setAlignmentX(Component.RIGHT_ALIGNMENT);
           lb.setPreferredSize(dimTotal);
           lb.setMaximumSize(dimTotal);
           pTotal.add(lb);
           JTextField tfQuantity= new JTextField();
           tfQuantity.setAlignmentX(Component.CENTER_ALIGNMENT);
           tfQuantity.setPreferredSize(dimQuantity);
           tfQuantity.setMaximumSize(dimQuantity);
           pQuantity.add(tfQuantity);
           JTextField tfRemark= new JTextField();
           tfRemark.setAlignmentX(Component.LEFT_ALIGNMENT);
           tfRemark.setPreferredSize(dimRemark);
           tfRemark.setMaximumSize(dimRemark);
           pRemark.add(tfRemark);
          }
          int height= (icnt+1)*22;
          pNumber.setPreferredSize(new Dimension(dimNumber.width, height));
          pTotal.setPreferredSize(new Dimension(dimTotal.width, height));
          pQuantity.setPreferredSize(new Dimension(dimQuantity.width, height);
          pRemark.setPreferredSize(new Dimension(dimRemark.width, height);
          add(panel);
          setVisible(true);
        }
      
      
        public static void main(String args[]) {
          EventQueue.invokeLater(new Runnable() {
            public void run() {
           new ColumnsWithHeader();
            }
          });
        }
      
      }
        • 1. Re: BoxLayout alignment
          800268
          The labels probably just take up the entire width (add a border to the label to see). You'll want to call JLabel#setHorizontalAlignment() to determine the alignment of the text inside the label.
          • 2. Re: BoxLayout alignment
            Jörg
            The labels probably just take up the entire width
            Yes, because I used one and the same dimension for all elements in one column.

            So either one creates the header labels with a dimension width of their actual text width,
            or if they take up all the panel's width, with SwingConstants.RIGHT or .CENTER or the method you mentioned.

            Thank you very much, indeed, Walter.