Solved

Components aren't layed out correctly using BoyLayout

Posted on 2004-04-15
12
247 Views
Last Modified: 2008-03-10
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
  //----------------------------------------------------------------------------
0
Comment
Question by:dirku
  • 6
  • 5
12 Comments
 
LVL 9

Expert Comment

by:mmuruganandam
ID: 10831898
0
 
LVL 9

Expert Comment

by:mmuruganandam
ID: 10831957
traceGroupJumperList.setListData(v);

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



Regards,
Muruga
0
 

Author Comment

by:dirku
ID: 10831990
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
 
LVL 9

Expert Comment

by:mmuruganandam
ID: 10832006
So what is the problem now
0
 

Author Comment

by:dirku
ID: 10832074
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
 
LVL 9

Expert Comment

by:mmuruganandam
ID: 10832138
For me, the alignment is exactly at the edge of the panel.  Both button and Jlist.
0
Enabling OSINT in Activity Based Intelligence

Activity based intelligence (ABI) requires access to all available sources of data. Recorded Future allows analysts to observe structured data on the open, deep, and dark web.

 

Author Comment

by:dirku
ID: 10832310
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
 
LVL 9

Expert Comment

by:mmuruganandam
ID: 10832349
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
 

Author Comment

by:dirku
ID: 10833568
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
 

Author Comment

by:dirku
ID: 10877324
I reported this as a bug to JavaSoft. Maybe they can find out if it's really a bug.
0
 
LVL 9

Expert Comment

by:mmuruganandam
ID: 10877364
Okie.  Then you can close/delete this question.


Regards,
Muruga
0
 

Accepted Solution

by:
modulo earned 0 total points
ID: 10880523
Closed, 25 points refunded.
modulo
Community Support Moderator
Experts Exchange
0

Featured Post

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

Java had always been an easily readable and understandable language.  Some relatively recent changes in the language seem to be changing this pretty fast, and anyone that had not seen any Java code for the last 5 years will possibly have issues unde…
Java functions are among the best things for programmers to work with as Java sites can be very easy to read and prepare. Java especially simplifies many processes in the coding industry as it helps integrate many forms of technology and different d…
This tutorial covers a step-by-step guide to install VisualVM launcher in eclipse.
This tutorial explains how to use the VisualVM tool for the Java platform application. This video goes into detail on the Threads, Sampler, and Profiler tabs.

747 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now