Components aren't layed out correctly using BoyLayout

The components I want to pace onto the JPanel on the left side of a JSplitPane are note arranged as expected when using BoxLayout. Why not? When I am using JButtons only they are aligned left but as soon as I place another component onto the JPanel the alignment plays up.

private void initGuiComponents()
  {
    //The layout of the application itself.
    this.getContentPane().setLayout(new BorderLayout());

    //NORTH components
    openFileButton = new JButton(openFileButtonStr);

    groupCriterionChooser = new JComboBox();

    fileloadProgresslabel = new JLabel(fileloadProgressStr);
    fileloadProgressbar = new JProgressBar();
    fileloadProgressbar.setStringPainted(true);

    topPane.setLayout(new FlowLayout(FlowLayout.LEADING));
    topPane.add(openFileButton);
    topPane.add(fileloadProgresslabel);
    topPane.add(fileloadProgressbar);
    this.getContentPane().add(topPane, BorderLayout.PAGE_START);

    //CENTER components
    JScrollPane mainScrollPane = new JScrollPane(mainPane);
    {
      //RIGHT JSplitPane components
      BoxLayout bl = new BoxLayout(mainPane, BoxLayout.PAGE_AXIS);
      mainPane.setLayout(bl);
      mainPane.setBackground(Color.orange);

      //LEFT JSplitPane components
      leftNavPane.setLayout(new BoxLayout(leftNavPane, BoxLayout.PAGE_AXIS));
      leftNavPane.setBorder(BorderFactory.createEmptyBorder(10,0,10,10));
      leftNavPane.setBackground(Color.green);

      JButton b1 = new JButton("111");
      b1.setAlignmentX(LEFT_ALIGNMENT);
      leftNavPane.add(b1);
leftNavPane.add(Box.createRigidArea(new Dimension(0,5)));
      JButton b2 = new JButton("222");
      b2.setAlignmentX(LEFT_ALIGNMENT);
      leftNavPane.add(b2);
leftNavPane.add(Box.createRigidArea(new Dimension(0,5)));
      traceGroupJumperList = new JList();
      traceGroupJumperList.setAlignmentY(RIGHT_ALIGNMENT);
      leftNavPane.add(traceGroupJumperList);
      traceGroupJumperList.add(new JLabel("cc"));
      java.util.Vector v = new java.util.Vector();
      v.add(new String("aaa"));
      v.add(new String("zzz"));
      traceGroupJumperList.setListData(v);
      traceGroupJumperList.revalidate();
      traceGroupJumperList.repaint();

      /*
      leftNavPane.setLayout(new BoxLayout(leftNavPane, BoxLayout.PAGE_AXIS));
      leftNavPane.setAlignmentY(leftNavPane.LEFT_ALIGNMENT);
      leftNavPane.setBorder(BorderFactory.createEmptyBorder(10,0,10,10));
      java.util.Vector v = new java.util.Vector();
      v.add(new String("aaa"));
      v.add(new String("zzz"));
      traceGroupJumperList = new JList(v);
      traceGroupJumperList.setAlignmentY(this.LEFT_ALIGNMENT);
      JButton b3 = new JButton("33333");
      b3.setAlignmentY(LEFT_ALIGNMENT);
      b3.setBackground(Color.green);
      leftNavPane.add(b3);
      leftNavPane.add(Box.createRigidArea(new Dimension(1, 10)));
      leftNavPane.add(traceGroupJumperList);
      leftNavPane.add(Box.createRigidArea(new Dimension(1, 10)));
      JButton b4 = new JButton("44444");
      b4.setAlignmentY(RIGHT_ALIGNMENT);
      leftNavPane.add(b4);
      */
    }
    splitPane = new JSplitPane(JSplitPane.HORIZONTAL_SPLIT,
                               leftNavPane, mainScrollPane);

    //SOUTH components
    statusPane = new JPanel();

    //Putting it altogether
    this.getContentPane().add(statusPane, BorderLayout.SOUTH);
    this.getContentPane().add(splitPane, BorderLayout.CENTER);

    addListeners();
  }//end initGuiComponents
  //----------------------------------------------------------------------------
dirkuAsked:
Who is Participating?

Improve company productivity with a Business Account.Sign Up

x
 
moduloConnect With a Mentor Commented:
Closed, 25 points refunded.
modulo
Community Support Moderator
Experts Exchange
0
 
mmuruganandamCommented:
0
 
mmuruganandamCommented:
traceGroupJumperList.setListData(v);

// insert the following line after the above line....
traceGroupJumperList.setAlignmentX(JComponent.LEFT_ALIGNMENT);



Regards,
Muruga
0
Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

 
dirkuAuthor Commented:
Sorry. I played around with the XXX_ALIGNMENT.

What I mean is that when I insert this line:
traceGroupJumperList.setAlignmentX(JComponent.LEFT_ALIGNMENT);

The two JBUttons are not aligned at the left side of the panel but seems to have an indent of approximately 10 pixel and I don't know why. When I remove the JList, the JButtons are aligned properly.

The Java tutorial doesn't help in this case. I read this chapter previously.
0
 
mmuruganandamCommented:
So what is the problem now
0
 
dirkuAuthor Commented:
The problem is that not all components are aligned at the left side of the panel.
When I add the two JButtons only to the pane everything is fine.
when I then add the JList to the pane, the JList is aligned well on the left side of the panel but the JButtons are not any longer. There is now a gap between the left side of the panel and the left side of the JButtons.
0
 
mmuruganandamCommented:
For me, the alignment is exactly at the edge of the panel.  Both button and Jlist.
0
 
dirkuAuthor Commented:
Weird!
There must be something wrong in the rest of my program's code. I guess nobody of you good guys can help me then.
Of course, I would expect a behaviour like your program has with the code above.
0
 
mmuruganandamCommented:
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.FlowLayout;

import javax.swing.*;

public class BoxLayoutTest extends JFrame
{
      private JPanel statusPane;
      private JSplitPane splitPane;
      private JList traceGroupJumperList;
      private JPanel topPane = new JPanel(), mainPane = new JPanel(), leftNavPane = new JPanel();
      private JProgressBar fileloadProgressbar;
      private JLabel fileloadProgresslabel;
      private JComboBox groupCriterionChooser;
      private JButton openFileButton;
      
      public BoxLayoutTest()
      {
            super();
            
            initGuiComponents();
      }
      
      private void initGuiComponents()
      {
            //The layout of the application itself.
            getContentPane().setLayout(new BorderLayout());

            //NORTH components
            openFileButton = new JButton("Open file");

            groupCriterionChooser = new JComboBox();

            fileloadProgresslabel = new JLabel("Progress bar");
            fileloadProgressbar = new JProgressBar();
            fileloadProgressbar.setStringPainted(true);

            topPane.setLayout(new FlowLayout(FlowLayout.LEADING));
            topPane.add(openFileButton);
            topPane.add(fileloadProgresslabel);
            topPane.add(fileloadProgressbar);
            this.getContentPane().add(topPane, BorderLayout.PAGE_START);

            //CENTER components
            JScrollPane mainScrollPane = new JScrollPane(mainPane);
            {
                  //RIGHT JSplitPane components
                  BoxLayout bl = new BoxLayout(mainPane, BoxLayout.PAGE_AXIS);
                  mainPane.setLayout(bl);
                  mainPane.setBackground(Color.orange);

                  //LEFT JSplitPane components
                  leftNavPane.setLayout(new BoxLayout(leftNavPane, BoxLayout.PAGE_AXIS));
                  leftNavPane.setBorder(BorderFactory.createEmptyBorder(10, 0, 10, 10));
                  leftNavPane.setBackground(Color.green);

                  JButton b1 = new JButton("111");
                  b1.setAlignmentX(LEFT_ALIGNMENT);
                  leftNavPane.add(b1);
                  leftNavPane.add(Box.createRigidArea(new Dimension(0, 5)));
                  JButton b2 = new JButton("222");
                  b2.setAlignmentX(LEFT_ALIGNMENT);
                  leftNavPane.add(b2);
                  leftNavPane.add(Box.createRigidArea(new Dimension(0, 5)));
                  traceGroupJumperList = new JList();
                  traceGroupJumperList.setAlignmentY(RIGHT_ALIGNMENT);
                  leftNavPane.add(traceGroupJumperList);
                  traceGroupJumperList.add(new JLabel("cc"));
                  
                  //b1.setBounds(b1.getX(), b1.getY(), 80, 25);
                  //b2.setBounds(b2.getX(), b2.getY(), 80, 25);
                  
                  java.util.Vector v = new java.util.Vector();
                  v.add(new String("aaa"));
                  v.add(new String("zzz"));
                  traceGroupJumperList.setListData(v);
                  traceGroupJumperList.setAlignmentX(JComponent.LEFT_ALIGNMENT);
                  
                  traceGroupJumperList.revalidate();
                  traceGroupJumperList.repaint();

                  /*
                  leftNavPane.setLayout(new BoxLayout(leftNavPane, BoxLayout.PAGE_AXIS));
                  leftNavPane.setAlignmentY(leftNavPane.LEFT_ALIGNMENT);
                  leftNavPane.setBorder(BorderFactory.createEmptyBorder(10,0,10,10));
                  java.util.Vector v = new java.util.Vector();
                  v.add(new String("aaa"));
                  v.add(new String("zzz"));
                  traceGroupJumperList = new JList(v);
                  traceGroupJumperList.setAlignmentY(this.LEFT_ALIGNMENT);
                  JButton b3 = new JButton("33333");
                  b3.setAlignmentY(LEFT_ALIGNMENT);
                  b3.setBackground(Color.green);
                  leftNavPane.add(b3);
                  leftNavPane.add(Box.createRigidArea(new Dimension(1, 10)));
                  leftNavPane.add(traceGroupJumperList);
                  leftNavPane.add(Box.createRigidArea(new Dimension(1, 10)));
                  JButton b4 = new JButton("44444");
                  b4.setAlignmentY(RIGHT_ALIGNMENT);
                  leftNavPane.add(b4);
                  */
            }
            splitPane = new JSplitPane(JSplitPane.HORIZONTAL_SPLIT, leftNavPane, mainScrollPane);

            //SOUTH components
            statusPane = new JPanel();

            //Putting it altogether
            this.getContentPane().add(statusPane, BorderLayout.SOUTH);
            this.getContentPane().add(splitPane, BorderLayout.CENTER);

            setSize(300,300);
            setVisible(true);
            
            //addListeners();
      } //end initGuiComponents
      //----------------------------------------------------------------------------

      public static void main(String[] args)
      {
            new BoxLayoutTest();
      }
}


Try this code....
0
 
dirkuAuthor Commented:
No effect. What did you change?
When I print out the bounds of the relevant components I get these values:

JButton b1 = java.awt.Rectangle[x=11,y=10,width=55,height=26]

JButton b2 = java.awt.Rectangle[x=11,y=41,width=55,height=26]

JList traceGroupJumperList = java.awt.Rectangle[x=0,y=72,width=23,height=36]
0
 
dirkuAuthor Commented:
I reported this as a bug to JavaSoft. Maybe they can find out if it's really a bug.
0
 
mmuruganandamCommented:
Okie.  Then you can close/delete this question.


Regards,
Muruga
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.