Question

Can anyone help me. Why is the Label "Current" NOT left aligned in Panel/Frame?

   public static void main(String[] args) {



            JFrame TFrame = new JFrame("Test DisplayLayout");
            TFrame.setResizable(true);
            TFrame.setSize(new Dimension(900, 840));
            TFrame.setLocationRelativeTo(null);
            TFrame.setTitle("DisplayLayout");   
            TFrame.setVisible(true); 


            JPanel P  = DisplayLayout2();

            P.setVisible(true);
            P.setOpaque(true);
            P.setLayout(new BoxLayout(P, BoxLayout.Y_AXIS));

            TFrame.add(P);

            TFrame.revalidate();
            TFrame.repaint();


        }

       public static JPanel DisplayLayout2() {

            JPanel Panel=new JPanel();
            Panel.setVisible(true);
            Panel.setOpaque(true);
            Panel.setLayout(new BoxLayout(Panel, BoxLayout.Y_AXIS));
            Panel.setAlignmentX(Component.LEFT_ALIGNMENT);

            JLabel lab = new JLabel("Current");
            lab.setHorizontalAlignment(SwingConstants.LEFT);
            lab.setForeground(Color.WHITE);
            lab.setBackground(Color.PINK);
            lab.setOpaque(true);    
            Panel.add(lab,Component.LEFT_ALIGNMENT);

            JPanel posPanel = new JPanel();
            JScrollPane scrollPane = new JScrollPane(posPanel,JScrollPane.VERTICAL_SCROLLBAR_ALWAYS,JScrollPane.HORIZONTAL_SCROLLBAR_NEVER);
            scrollPane.setPreferredSize(new Dimension(290, 200));
            scrollPane.setOpaque(true);        
            posPanel.setBackground(Color.YELLOW);
            posPanel.setPreferredSize(new Dimension(290, 200));
            posPanel.setLayout(new BoxLayout(posPanel, BoxLayout.Y_AXIS));          
            posPanel.setAlignmentX(Component.LEFT_ALIGNMENT);   

            Panel.add(scrollPane);

            return Panel;

        }

enter image description here

Was it helpful?

Solution

This is one of the quirks of the BoxLayout (well, quirk to me, but it is a documented expected behavior of the layout), and I'm forgetting off the top of my head why it does this, but I do know of at least one way around it: put your JLabel into a JPanel that uses FlowLayout.LEFT (or LEADING), and add that to your BoxLayout-using container:

JPanel labPanel = new JPanel(new FlowLayout(FlowLayout.LEFT, 0, 0));
labPanel.add(lab);
panel.add(labPanel, Component.LEFT_ALIGNMENT);

Note, that I believe that it has something to do with the JLabel not wanting to expand while the JPanel that encloses it does, but don't quote me on that.


Edit
From the BoxLayout Tutorial:

For a top-to-bottom box layout, the preferred width of the container is that of the maximum preferred width of the children. If the container is forced to be wider than that, BoxLayout attempts to size the width of each component to that of the container's width (minus insets). If the maximum size of a component is smaller than the width of the container, then X alignment comes into play.

You could probably solve this by setting both the JLabel's and the JScrollPane's x-alignment to Component.LEFT_ALIGNMENT. Your current code forgets to set the JScrollPane's x-alignment, and that's where your trouble lies:

scrollPane.setAlignmentX(Component.LEFT_ALIGNMENT);

For example:

import java.awt.*;
import javax.swing.*;

public class Foo2 {

   private static void createAndShowGui() {
      JLabel topLabel = new JLabel("Top Label", SwingConstants.LEFT);
      topLabel.setOpaque(true);
      topLabel.setBackground(Color.pink);

      JScrollPane scrollpane = new JScrollPane(
            Box.createRigidArea(new Dimension(400, 400)),
            JScrollPane.VERTICAL_SCROLLBAR_ALWAYS, 
            JScrollPane.HORIZONTAL_SCROLLBAR_ALWAYS);

      JPanel mainPanel = new JPanel();
      mainPanel.setLayout(new BoxLayout(mainPanel, BoxLayout.PAGE_AXIS));

      topLabel.setAlignmentX(Component.LEFT_ALIGNMENT);
      mainPanel.add(topLabel);
      scrollpane.setAlignmentX(Component.LEFT_ALIGNMENT);
      mainPanel.add(scrollpane);

      JFrame frame = new JFrame("Foo");
      frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
      frame.getContentPane().add(mainPanel);
      frame.pack();
      frame.setLocationByPlatform(true);
      frame.setVisible(true);
   }

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

This results in:

enter image description here

OTHER TIPS

Use constant into Component(Left,Right,Center)

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top