Layout problem

I tried almost all layout managers and can't seem to figure this out. I have 4 panels each with a label and a text field. These four panels are added to one panel using BoxLayout.Y_AXIS. The size of this main panel is determined by surrounding components (which stretch the panel to a larger size). Using BoxLayout.Y_AXIS the four panels align at equal vertical distance of each other. So far so good.

The problem is that I want to align the textfields of each panel with the textfields of the other panels. When I use BorderLayout and add the labels to West and the textfields to EAST they are aligned but my textfields get stretched vertically, which is not desired. It is very hard for me to align these textfield since they are added to 4 different panels. I tried many different things but none gave the desired effect. Any ideas?

[849 byte] By [phalkonea] at [2007-11-26 19:04:16]
# 1

the textfields are not aligned because the labels have differing lengths?

if that is the problem, couple of things might work

1)

determine the longest label, then set the other labels to the same size

otherLabel.setPreferredSize(longestLabel.getPreferredSize());

2)

instead of your Boxlayout, use a couple of gridLayouts

JPanel p = new JPanel(new BorderLayout());

JPanel p1 = new JPanel(new GridLayout(4,1));

JPanel p2 = new JPanel(new GridLayout(4,1));

p1.add(4 x labels)

p2.add(4 x textFields)

p.add(p1,BorderLayout.WEST);

p.add(p2,BorderLayout.CENTER);

Michael_Dunna at 2007-7-9 20:52:45 > top of Java-index,Desktop,Core GUI APIs...
# 2

The main problem is not the aligning, but the stretching of my textfield components. Even if I set a preferred size it doesn't help. I can get the components aligned using GridLayout or BorderLayout, but then they stretch because the main panel is higher (caused by the panel next to it). My textfields really look ugly because they vertically stretch to fill my main panel height.

phalkonea at 2007-7-9 20:52:45 > top of Java-index,Desktop,Core GUI APIs...
# 3

the panels containing the components that stretch to fill the borderlayout areas.

Add each of these panels to other separate panels, then add that panel to the

borderlayout panel.

this extra panel will take the additional space.

if this doesn't do you what you want, post a small sample program, so we can

see exactly the problem

Michael_Dunna at 2007-7-9 20:52:45 > top of Java-index,Desktop,Core GUI APIs...
# 4

This additional space would be empty. This is not my objective. Say I have the following code:

JPanel overall = new JPanel(new BorderLayout());

JPanel mainpanel1 = new JPanel(new BoxLayout(mainpanel1, BoxLayout.Y_AXIS));

JPanel mainpanel2 = new JPanel();

JPanel mainpanel3 = new JPanel();

JPanel labelandfield1 = new JPanel(?);

labelandfield1.add(new JLabel("Input 1: "));

labelandfield1.add(new JTextField());

JPanel labelandfield2 = new JPanel(?);

labelandfield2.add(new JLabel("In 2: "));

labelandfield2.add(new JTextField());

JPanel labelandfield3 = new JPanel(?);

labelandfield3.add(new JLabel("Inputfield 3: "));

labelandfield3.add(new JTextField());

JPanel labelandfield4 = new JPanel(?);

labelandfield4.add(new JLabel("Input 4: "));

labelandfield4.add(new JTextField());

overall.add(BorderLayout.WEST, mainpanel1);

overall.add(BorderLayout.CENTER, mainpanel2);

overall.add(BorderLayout.EAST, mainpanel3);

I have an overall panel which contains 3 mainpanels. Mainpanel2 contains a large image which is very high. To fill mainpanel1 completely I want to spread each label and textfield so that the vertical distance between them is equal. This is achieved by using BoxLayout.Y_AXIS and adding each label and texfield to a different panel (namely labelandfield1, labelandfield2..). The problem is that I can't seem to find a lay-out for these seperate panels, which allows me to align and not stretch my components. BorderLayout and GridLayout allow me to align, but stretch my components. BoxLayout.X_AXIS allows me to keep my components a proper size, but doesn't align my components (labels are different size). I want to align my textfiels to the right of the panel and my labels to the left.

phalkonea at 2007-7-9 20:52:45 > top of Java-index,Desktop,Core GUI APIs...
# 5

see if this gets you any closer

import javax.swing.*;

import java.awt.*;

import java.awt.event.*;

class Testing

{

public void buildGUI()

{

JPanel overall = new JPanel(new BorderLayout());

JPanel mainpanel1 = new JPanel();

mainpanel1.setLayout(new BoxLayout(mainpanel1, BoxLayout.Y_AXIS));

JPanel labelandfield1 = new JPanel(new BorderLayout());

labelandfield1.add(new JLabel("Input 1: "),BorderLayout.WEST);

labelandfield1.add(new JTextField(10),BorderLayout.EAST);

JPanel labelandfield2 = new JPanel(new BorderLayout());

labelandfield2.add(new JLabel("In 2: "),BorderLayout.WEST);

labelandfield2.add(new JTextField(10),BorderLayout.EAST);

JPanel labelandfield3 = new JPanel(new BorderLayout());

labelandfield3.add(new JLabel("Inputfield 3: "),BorderLayout.WEST);

labelandfield3.add(new JTextField(10),BorderLayout.EAST);

JPanel labelandfield4 = new JPanel(new BorderLayout());

labelandfield4.add(new JLabel("Input 4: "),BorderLayout.WEST);

labelandfield4.add(new JTextField(10),BorderLayout.EAST);

mainpanel1.add(labelandfield1);

mainpanel1.add(labelandfield2);

mainpanel1.add(labelandfield3);

mainpanel1.add(labelandfield4);

JPanel bigImagePanel = new JPanel();

bigImagePanel.setPreferredSize(new Dimension(400,300));

JPanel holdingPanel = new JPanel();

holdingPanel.add(mainpanel1);

overall.add(BorderLayout.WEST, holdingPanel);

overall.add(BorderLayout.CENTER, bigImagePanel);

JFrame f = new JFrame();

f.getContentPane().add(overall);

f.pack();

f.setLocationRelativeTo(null);

f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

f.setVisible(true);

}

public static void main(String[] args)

{

SwingUtilities.invokeLater(new Runnable(){

public void run(){

new Testing().buildGUI();

}

});

}

}

Michael_Dunna at 2007-7-9 20:52:45 > top of Java-index,Desktop,Core GUI APIs...
# 6

Drop these lines from your source code:

JPanel holdingPanel = new JPanel();

holdingPanel.add(mainpanel1);

and adjust this line

overall.add(BorderLayout.WEST, holdingPanel);

to

overall.add(BorderLayout.WEST, mainpanel1);

Now you can clearly see the problem that needs to be fixed. The labels are positioned correct, but as you can see the textfields are stretched.

phalkonea at 2007-7-9 20:52:45 > top of Java-index,Desktop,Core GUI APIs...
# 7
yes, that's why I used the holding panel (to take up the additional space), butthis puts them all at the top.do you want the label/textfield panels evenly spaced top to bottom?
Michael_Dunna at 2007-7-9 20:52:46 > top of Java-index,Desktop,Core GUI APIs...
# 8
> do you want the label/textfield panels evenly spaced> top to bottom?This is exactly what I want.
phalkonea at 2007-7-9 20:52:46 > top of Java-index,Desktop,Core GUI APIs...
# 9

import javax.swing.*;

import java.awt.*;

import java.awt.event.*;

class Testing

{

public void buildGUI()

{

JPanel overall = new JPanel(new BorderLayout());

JPanel mainpanel1 = new JPanel();

mainpanel1.setLayout(new BoxLayout(mainpanel1, BoxLayout.Y_AXIS));

JPanel labelandfield1 = new JPanel(new BorderLayout());

labelandfield1.add(new JLabel("Input 1: "),BorderLayout.WEST);

labelandfield1.add(new JTextField(10),BorderLayout.EAST);

JPanel p1 = new JPanel(new GridBagLayout());

p1.add(labelandfield1,new GridBagConstraints());

JPanel labelandfield2 = new JPanel(new BorderLayout());

labelandfield2.add(new JLabel("In 2: "),BorderLayout.WEST);

labelandfield2.add(new JTextField(10),BorderLayout.EAST);

JPanel p2 = new JPanel(new GridBagLayout());

p2.add(labelandfield2,new GridBagConstraints());

JPanel labelandfield3 = new JPanel(new BorderLayout());

labelandfield3.add(new JLabel("Inputfield 3: "),BorderLayout.WEST);

labelandfield3.add(new JTextField(10),BorderLayout.EAST);

JPanel p3 = new JPanel(new GridBagLayout());

p3.add(labelandfield3,new GridBagConstraints());

JPanel labelandfield4 = new JPanel(new BorderLayout());

labelandfield4.add(new JLabel("Input 4: "),BorderLayout.WEST);

labelandfield4.add(new JTextField(10),BorderLayout.EAST);

JPanel p4 = new JPanel(new GridBagLayout());

p4.add(labelandfield4,new GridBagConstraints());

//mainpanel1.add(labelandfield1);

//mainpanel1.add(labelandfield2);

//mainpanel1.add(labelandfield3);

//mainpanel1.add(labelandfield4);

mainpanel1.add(p1);

mainpanel1.add(p2);

mainpanel1.add(p3);

mainpanel1.add(p4);

labelandfield1.setPreferredSize(labelandfield3.getPreferredSize());

labelandfield2.setPreferredSize(labelandfield3.getPreferredSize());

labelandfield4.setPreferredSize(labelandfield3.getPreferredSize());

JPanel bigImagePanel = new JPanel();

bigImagePanel.setPreferredSize(new Dimension(400,300));

overall.add(BorderLayout.WEST, mainpanel1);

overall.add(BorderLayout.CENTER, bigImagePanel);

JFrame f = new JFrame();

f.getContentPane().add(overall);

f.pack();

f.setLocationRelativeTo(null);

f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

f.setVisible(true);

}

public static void main(String[] args)

{

SwingUtilities.invokeLater(new Runnable(){

public void run(){

new Testing().buildGUI();

}

});

}

}

Michael_Dunna at 2007-7-9 20:52:46 > top of Java-index,Desktop,Core GUI APIs...
# 10
Thanks a lot, that finally did the trick.
phalkonea at 2007-7-9 20:52:46 > top of Java-index,Desktop,Core GUI APIs...
# 11
Next time, just google for "TableLayout" :o)
itchyscratchya at 2007-7-9 20:52:46 > top of Java-index,Desktop,Core GUI APIs...