dirku
asked on
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().setL ayout(new BorderLayout());
//NORTH components
openFileButton = new JButton(openFileButtonStr) ;
groupCriterionChooser = new JComboBox();
fileloadProgresslabel = new JLabel(fileloadProgressStr );
fileloadProgressbar = new JProgressBar();
fileloadProgressbar.setStr ingPainted (true);
topPane.setLayout(new FlowLayout(FlowLayout.LEAD ING));
topPane.add(openFileButton );
topPane.add(fileloadProgre sslabel);
topPane.add(fileloadProgre ssbar);
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(Col or.orange) ;
//LEFT JSplitPane components
leftNavPane.setLayout(new BoxLayout(leftNavPane, BoxLayout.PAGE_AXIS));
leftNavPane.setBorder(Bord erFactory. createEmpt yBorder(10 ,0,10,10)) ;
leftNavPane.setBackground( Color.gree n);
JButton b1 = new JButton("111");
b1.setAlignmentX(LEFT_ALIG NMENT);
leftNavPane.add(b1);
leftNavPane.add(Box.create RigidArea( new Dimension(0,5)));
JButton b2 = new JButton("222");
b2.setAlignmentX(LEFT_ALIG NMENT);
leftNavPane.add(b2);
leftNavPane.add(Box.create RigidArea( new Dimension(0,5)));
traceGroupJumperList = new JList();
traceGroupJumperList.setAl ignmentY(R IGHT_ALIGN MENT);
leftNavPane.add(traceGroup JumperList );
traceGroupJumperList.add(n ew JLabel("cc"));
java.util.Vector v = new java.util.Vector();
v.add(new String("aaa"));
v.add(new String("zzz"));
traceGroupJumperList.setLi stData(v);
traceGroupJumperList.reval idate();
traceGroupJumperList.repai nt();
/*
leftNavPane.setLayout(new BoxLayout(leftNavPane, BoxLayout.PAGE_AXIS));
leftNavPane.setAlignmentY( leftNavPan e.LEFT_ALI GNMENT);
leftNavPane.setBorder(Bord erFactory. createEmpt yBorder(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.setAl ignmentY(t his.LEFT_A LIGNMENT);
JButton b3 = new JButton("33333");
b3.setAlignmentY(LEFT_ALIG NMENT);
b3.setBackground(Color.gre en);
leftNavPane.add(b3);
leftNavPane.add(Box.create RigidArea( new Dimension(1, 10)));
leftNavPane.add(traceGroup JumperList );
leftNavPane.add(Box.create RigidArea( new Dimension(1, 10)));
JButton b4 = new JButton("44444");
b4.setAlignmentY(RIGHT_ALI GNMENT);
leftNavPane.add(b4);
*/
}
splitPane = new JSplitPane(JSplitPane.HORI ZONTAL_SPL IT,
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
//------------------------ ---------- ---------- ---------- ---------- ---------- --
private void initGuiComponents()
{
//The layout of the application itself.
this.getContentPane().setL
//NORTH components
openFileButton = new JButton(openFileButtonStr)
groupCriterionChooser = new JComboBox();
fileloadProgresslabel = new JLabel(fileloadProgressStr
fileloadProgressbar = new JProgressBar();
fileloadProgressbar.setStr
topPane.setLayout(new FlowLayout(FlowLayout.LEAD
topPane.add(openFileButton
topPane.add(fileloadProgre
topPane.add(fileloadProgre
this.getContentPane().add(
//CENTER components
JScrollPane mainScrollPane = new JScrollPane(mainPane);
{
//RIGHT JSplitPane components
BoxLayout bl = new BoxLayout(mainPane, BoxLayout.PAGE_AXIS);
mainPane.setLayout(bl);
mainPane.setBackground(Col
//LEFT JSplitPane components
leftNavPane.setLayout(new BoxLayout(leftNavPane, BoxLayout.PAGE_AXIS));
leftNavPane.setBorder(Bord
leftNavPane.setBackground(
JButton b1 = new JButton("111");
b1.setAlignmentX(LEFT_ALIG
leftNavPane.add(b1);
leftNavPane.add(Box.create
JButton b2 = new JButton("222");
b2.setAlignmentX(LEFT_ALIG
leftNavPane.add(b2);
leftNavPane.add(Box.create
traceGroupJumperList = new JList();
traceGroupJumperList.setAl
leftNavPane.add(traceGroup
traceGroupJumperList.add(n
java.util.Vector v = new java.util.Vector();
v.add(new String("aaa"));
v.add(new String("zzz"));
traceGroupJumperList.setLi
traceGroupJumperList.reval
traceGroupJumperList.repai
/*
leftNavPane.setLayout(new BoxLayout(leftNavPane, BoxLayout.PAGE_AXIS));
leftNavPane.setAlignmentY(
leftNavPane.setBorder(Bord
java.util.Vector v = new java.util.Vector();
v.add(new String("aaa"));
v.add(new String("zzz"));
traceGroupJumperList = new JList(v);
traceGroupJumperList.setAl
JButton b3 = new JButton("33333");
b3.setAlignmentY(LEFT_ALIG
b3.setBackground(Color.gre
leftNavPane.add(b3);
leftNavPane.add(Box.create
leftNavPane.add(traceGroup
leftNavPane.add(Box.create
JButton b4 = new JButton("44444");
b4.setAlignmentY(RIGHT_ALI
leftNavPane.add(b4);
*/
}
splitPane = new JSplitPane(JSplitPane.HORI
leftNavPane, mainScrollPane);
//SOUTH components
statusPane = new JPanel();
//Putting it altogether
this.getContentPane().add(
this.getContentPane().add(
addListeners();
}//end initGuiComponents
//------------------------
traceGroupJumperList.setLi stData(v);
// insert the following line after the above line....
traceGroupJumperList.setAl ignmentX(J Component. LEFT_ALIGN MENT);
Regards,
Muruga
// insert the following line after the above line....
traceGroupJumperList.setAl
Regards,
Muruga
ASKER
Sorry. I played around with the XXX_ALIGNMENT.
What I mean is that when I insert this line:
traceGroupJumperList.setAl ignmentX(J Component. LEFT_ALIGN MENT);
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.
What I mean is that when I insert this line:
traceGroupJumperList.setAl
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.
So what is the problem now
ASKER
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.
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.
For me, the alignment is exactly at the edge of the panel. Both button and Jlist.
ASKER
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.
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.
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.setStr ingPainted (true);
topPane.setLayout(new FlowLayout(FlowLayout.LEAD ING));
topPane.add(openFileButton );
topPane.add(fileloadProgre sslabel);
topPane.add(fileloadProgre ssbar);
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(Col or.orange) ;
//LEFT JSplitPane components
leftNavPane.setLayout(new BoxLayout(leftNavPane, BoxLayout.PAGE_AXIS));
leftNavPane.setBorder(Bord erFactory. createEmpt yBorder(10 , 0, 10, 10));
leftNavPane.setBackground( Color.gree n);
JButton b1 = new JButton("111");
b1.setAlignmentX(LEFT_ALIG NMENT);
leftNavPane.add(b1);
leftNavPane.add(Box.create RigidArea( new Dimension(0, 5)));
JButton b2 = new JButton("222");
b2.setAlignmentX(LEFT_ALIG NMENT);
leftNavPane.add(b2);
leftNavPane.add(Box.create RigidArea( new Dimension(0, 5)));
traceGroupJumperList = new JList();
traceGroupJumperList.setAl ignmentY(R IGHT_ALIGN MENT);
leftNavPane.add(traceGroup JumperList );
traceGroupJumperList.add(n ew 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.setLi stData(v);
traceGroupJumperList.setAl ignmentX(J Component. LEFT_ALIGN MENT);
traceGroupJumperList.reval idate();
traceGroupJumperList.repai nt();
/*
leftNavPane.setLayout(new BoxLayout(leftNavPane, BoxLayout.PAGE_AXIS));
leftNavPane.setAlignmentY( leftNavPan e.LEFT_ALI GNMENT);
leftNavPane.setBorder(Bord erFactory. createEmpt yBorder(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.setAl ignmentY(t his.LEFT_A LIGNMENT);
JButton b3 = new JButton("33333");
b3.setAlignmentY(LEFT_ALIG NMENT);
b3.setBackground(Color.gre en);
leftNavPane.add(b3);
leftNavPane.add(Box.create RigidArea( new Dimension(1, 10)));
leftNavPane.add(traceGroup JumperList );
leftNavPane.add(Box.create RigidArea( new Dimension(1, 10)));
JButton b4 = new JButton("44444");
b4.setAlignmentY(RIGHT_ALI GNMENT);
leftNavPane.add(b4);
*/
}
splitPane = new JSplitPane(JSplitPane.HORI ZONTAL_SPL IT, 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....
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
//NORTH components
openFileButton = new JButton("Open file");
groupCriterionChooser = new JComboBox();
fileloadProgresslabel = new JLabel("Progress bar");
fileloadProgressbar = new JProgressBar();
fileloadProgressbar.setStr
topPane.setLayout(new FlowLayout(FlowLayout.LEAD
topPane.add(openFileButton
topPane.add(fileloadProgre
topPane.add(fileloadProgre
this.getContentPane().add(
//CENTER components
JScrollPane mainScrollPane = new JScrollPane(mainPane);
{
//RIGHT JSplitPane components
BoxLayout bl = new BoxLayout(mainPane, BoxLayout.PAGE_AXIS);
mainPane.setLayout(bl);
mainPane.setBackground(Col
//LEFT JSplitPane components
leftNavPane.setLayout(new BoxLayout(leftNavPane, BoxLayout.PAGE_AXIS));
leftNavPane.setBorder(Bord
leftNavPane.setBackground(
JButton b1 = new JButton("111");
b1.setAlignmentX(LEFT_ALIG
leftNavPane.add(b1);
leftNavPane.add(Box.create
JButton b2 = new JButton("222");
b2.setAlignmentX(LEFT_ALIG
leftNavPane.add(b2);
leftNavPane.add(Box.create
traceGroupJumperList = new JList();
traceGroupJumperList.setAl
leftNavPane.add(traceGroup
traceGroupJumperList.add(n
//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.setLi
traceGroupJumperList.setAl
traceGroupJumperList.reval
traceGroupJumperList.repai
/*
leftNavPane.setLayout(new BoxLayout(leftNavPane, BoxLayout.PAGE_AXIS));
leftNavPane.setAlignmentY(
leftNavPane.setBorder(Bord
java.util.Vector v = new java.util.Vector();
v.add(new String("aaa"));
v.add(new String("zzz"));
traceGroupJumperList = new JList(v);
traceGroupJumperList.setAl
JButton b3 = new JButton("33333");
b3.setAlignmentY(LEFT_ALIG
b3.setBackground(Color.gre
leftNavPane.add(b3);
leftNavPane.add(Box.create
leftNavPane.add(traceGroup
leftNavPane.add(Box.create
JButton b4 = new JButton("44444");
b4.setAlignmentY(RIGHT_ALI
leftNavPane.add(b4);
*/
}
splitPane = new JSplitPane(JSplitPane.HORI
//SOUTH components
statusPane = new JPanel();
//Putting it altogether
this.getContentPane().add(
this.getContentPane().add(
setSize(300,300);
setVisible(true);
//addListeners();
} //end initGuiComponents
//------------------------
public static void main(String[] args)
{
new BoxLayoutTest();
}
}
Try this code....
ASKER
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=5 5,height=2 6]
JButton b2 = java.awt.Rectangle[x=11,y= 41,width=5 5,height=2 6]
JList traceGroupJumperList = java.awt.Rectangle[x=0,y=7 2,width=23 ,height=36 ]
When I print out the bounds of the relevant components I get these values:
JButton b1 = java.awt.Rectangle[x=11,y=
JButton b2 = java.awt.Rectangle[x=11,y=
JList traceGroupJumperList = java.awt.Rectangle[x=0,y=7
ASKER
I reported this as a bug to JavaSoft. Maybe they can find out if it's really a bug.
Okie. Then you can close/delete this question.
Regards,
Muruga
Regards,
Muruga
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
http://java.sun.com/docs/books/tutorial/uiswing/layout/box.html