Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

Components aren't layed out correctly using BoyLayout

Posted on 2004-04-15
12
Medium Priority
?
269 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 
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
 

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

Enroll in October's Free Course of the Month

Do you work with and analyze data? Enroll in October's Course of the Month for 7+ hours of SQL training, allowing you to quickly and efficiently store or retrieve data. It's free for Premium Members, Team Accounts, and Qualified Experts!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This was posted to the Netbeans forum a Feb, 2010 and I also sent it to Verisign. Who didn't help much in my struggles to get my application signed. ------------------------- Start The idea here is to target your cell phones with the correct…
Introduction Java can be integrated with native programs using an interface called JNI(Java Native Interface). Native programs are programs which can directly run on the processor. JNI is simply a naming and calling convention so that the JVM (Java…
Viewers learn about the scanner class in this video and are introduced to receiving user input for their programs. Additionally, objects, conditional statements, and loops are used to help reinforce the concepts. Introduce Scanner class: Importing…
This theoretical tutorial explains exceptions, reasons for exceptions, different categories of exception and exception hierarchy.
Suggested Courses

596 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