Greg Guerin wrote:
Greg,
Thanks for taking a look at this for me. I tried converting from negative to positive pads as you suggested. That certainly changed the behavior, but doesn't accomplish what I'm looking for. If I specify the constraints that way, then I still have to specify the sizes for the contained Components, in order to get them to show up on Windows and OSX My strategy with SpringLayout, which works on Windows, is to build the GUI from the outside-in. I specify the size of the contentPane, and then anchor the outermost layer of Containers to contentPane's borders. Then when I add Components to each of these Containers, I do the same thing, anchoring each set of Contained components to the borders of its Container. Hence the negative pads for up to two of the four edges. This gives me very nice re-size behavior, as everything re-sizes to fit the outermost container as it changes size, and I don't have to set sizes for the contained components explicitly.
I created a simplified Class that demonstrates my problem. On Windows, the GUI is drawn as intended, showing a top, middle, and bottom panel, and the middle panel contains two panels, which between them contain three scrollable JLists and a JTextArea. With OSX, the inner panes of the middle panel are drawn with virtually no height, showing up as barely more than a line. I have some print statements that show the size and preferred size of the middle panel as it's being drawn. It looks like the middle panel does not get its size set properly, and you can also see that the constraint where I anchor it to the east edge of its container is null, even though the Container object is not null when I set the constraint.
I think the print statements show where the problem is occurring, but I have no idea why, or how to get it to work properly on OSX. Perhaps I'm doing something wrong, and Windows is more forgiving. It's interesting that print statements show the problem output even on Windows, when the JFrame is drawn correctly.
My system info is: Windows: XP/Pro, Java 1.5.0_11; Mac: OS 10.4.11 Java 1.5.0_13
Here's my test class.
import java.awt.Container;
import java.awt.Dimension;
import java.awt.Toolkit;
import javax.swing.BorderFactory;
import javax.swing.Box;
import javax.swing.BoxLayout;
import javax.swing.DefaultListModel;
import javax.swing.JFrame;
import javax.swing.JList;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.Spring;
import javax.swing.SpringLayout;
import javax.swing.SwingUtilities;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;
public class SpringLayoutTest extends JFrame{
private int w, h;
private JPanel topPanel, midPanel, bottomPanel, labelPanel,
midLeftPanel, midRightPanel;
private JTextArea fileAText, fileBText, commonText;
private JScrollPane scrollerA, scrollerB, scrollerC;
private int screenWidth;
private JList listA, listB, listC;
public SpringLayoutTest(){
super("SpringLayout Test");
SwingUtilities.invokeLater(new Runnable(){
public void run(){
createAndShowGUI();
}
});
} public static void main(String[] args){
new SpringLayoutTest();
}
private void createAndShowGUI(){
screenWidth = Toolkit.getDefaultToolkit().getScreenSize().width;
int screenHeight = Toolkit.getDefaultToolkit().getScreenSize().height;
w = (int)(0.9 * (double) screenWidth);
h = (int) (0.9 * (double)screenHeight);
setSize(new Dimension(w,h));
setPreferredSize(new Dimension(w,h));
Container mainPanel = getContentPane();
SpringLayout mainLayout, listPanelLayout, midLeftLayout, midRightLayout;
SpringLayout.Constraints cons;
mainPanel.setSize(new Dimension(w, h));
mainPanel.setLayout(mainLayout = new SpringLayout());
midPanel = new JPanel();
topPanel = new JPanel();
bottomPanel = new JPanel();
mainPanel.add(topPanel);
mainPanel.add(midPanel);
mainPanel.add(bottomPanel);
cons = new SpringLayout.Constraints();
cons.setY(Spring.constant(5));
cons.setHeight(Spring.constant(110, 120, 130));
cons.setWidth(mainLayout.getConstraint(SpringLayout.EAST, mainPanel));
mainLayout.addLayoutComponent(topPanel, cons);
mainLayout.putConstraint(SpringLayout.WEST, topPanel, 5, SpringLayout.WEST,
mainPanel);
mainLayout.putConstraint(SpringLayout.EAST, topPanel, -5, SpringLayout.EAST,
mainPanel);
cons = new SpringLayout.Constraints();
cons.setHeight(Spring.constant( (int)((double)h * 0.25),
(int)((double)h * 0.55),
(int)((double)h * 0.7)));
//cons.setWidth(mainLayout.getConstraint(SpringLayout.EAST, mainPanel));
cons.setWidth(mainLayout.getConstraint(SpringLayout.EAST, mainPanel));
mainLayout.addLayoutComponent(midPanel, cons);
mainLayout.putConstraint(SpringLayout.WEST, midPanel, 5, SpringLayout.WEST,
mainPanel);
//mainLayout.putConstraint(SpringLayout.EAST, listPanel, -5, SpringLayout.EAST,
//mainPanel);
mainLayout.putConstraint(SpringLayout.NORTH, midPanel, 5, SpringLayout.SOUTH,
topPanel);
System.err.println("listPanel height constraint = " + mainLayout.getConstraints(midPanel).getHeight());
System.err.println("listPanel width constraint = " + mainLayout.getConstraints(midPanel).getWidth());
System.err.println("listPanel height value = " + mainLayout.getConstraints(midPanel).getHeight().getValue());
System.err.println("listPanel width value = " + mainLayout.getConstraints(midPanel).getWidth().getValue());
cons = new SpringLayout.Constraints();
cons.setHeight(Spring.constant(120, 130, 140));
cons.setWidth(mainLayout.getConstraint(SpringLayout.EAST, mainPanel));
mainLayout.addLayoutComponent(bottomPanel, cons);
mainLayout.putConstraint(SpringLayout.WEST, bottomPanel, 5, SpringLayout.WEST,
mainPanel);
mainLayout.putConstraint(SpringLayout.EAST, bottomPanel, -5, SpringLayout.EAST,
mainPanel);
mainLayout.putConstraint(SpringLayout.NORTH, bottomPanel, 5, SpringLayout.SOUTH,
midPanel);
mainLayout.putConstraint(SpringLayout.SOUTH, bottomPanel, -5, SpringLayout.SOUTH,
mainPanel);
//setup top panel
topPanel.setLayout(new BoxLayout(topPanel, BoxLayout.Y_AXIS));
topPanel.setBorder(BorderFactory.createTitledBorder("Top Panel"));
//setup listPanel
System.err.println("listPanel size = " + midPanel.getSize().width + " " + midPanel.getSize().height);
System.err.println("mainPanel size = " + mainPanel.getSize().width + " " + mainPanel.getSize().height);
System.err.println("listPanel preferred size = " + midPanel.getPreferredSize().width + " " + midPanel.getPreferredSize().height);
System.err.println("mainPanel preferred size = " + mainPanel.getPreferredSize().width + " " + mainPanel.getPreferredSize().height);
midPanel.setLayout(listPanelLayout = new SpringLayout());
midPanel.setBorder(BorderFactory.createTitledBorder("Middle Panel"));
midLeftPanel = new JPanel();
midRightPanel = new JPanel();
midPanel.add(midLeftPanel);
midPanel.add(midRightPanel);
cons = new SpringLayout.Constraints();
cons.setWidth(Spring.constant((int)((double)w / 2)));
listPanelLayout.addLayoutComponent(midLeftPanel, cons);
listPanelLayout.putConstraint(SpringLayout.WEST, midLeftPanel, 5, SpringLayout.WEST,
midPanel);
listPanelLayout.putConstraint(SpringLayout.NORTH, midLeftPanel, 5, SpringLayout.NORTH,
midPanel);
listPanelLayout.putConstraint(SpringLayout.SOUTH, midLeftPanel, -5, SpringLayout.SOUTH,
midPanel);
cons = new SpringLayout.Constraints();
cons.setWidth(Spring.constant((int)((double)w / 2)));
listPanelLayout.addLayoutComponent(midRightPanel, cons);
listPanelLayout.putConstraint(SpringLayout.WEST, midRightPanel, 5, SpringLayout.EAST,
midLeftPanel);
listPanelLayout.putConstraint(SpringLayout.NORTH, midRightPanel, 5, SpringLayout.NORTH,
midPanel);
listPanelLayout.putConstraint(SpringLayout.SOUTH, midRightPanel, -5, SpringLayout.SOUTH,
midPanel);
//setup midLeftPanel
midLeftPanel.setLayout(midLeftLayout = new SpringLayout());
midLeftPanel.setBorder(BorderFactory.createTitledBorder("Mid Left Panel"));
labelPanel = new JPanel();
labelPanel.setLayout(new BoxLayout(labelPanel, BoxLayout.X_AXIS));
commonText = new JTextArea();
commonText.setLineWrap(true);
commonText.setWrapStyleWord(true);
commonText.setText("Sample text");
labelPanel.add(Box.createHorizontalStrut(2));
labelPanel.add(commonText); labelPanel.add(Box.createHorizontalGlue());
listA = new JList(new DefaultListModel());
scrollerA = new JScrollPane(listA);
midLeftPanel.add(labelPanel);
midLeftPanel.add(scrollerA);
cons = new SpringLayout.Constraints();
cons.setHeight(Spring.constant(45));
midLeftLayout.addLayoutComponent(labelPanel, cons);
midLeftLayout.putConstraint(SpringLayout.WEST, labelPanel, 5, SpringLayout.WEST,
midLeftPanel);
midLeftLayout.putConstraint(SpringLayout.NORTH, labelPanel, 5, SpringLayout.NORTH,
midLeftPanel);
midLeftLayout.putConstraint(SpringLayout.EAST, labelPanel, -5, SpringLayout.EAST,
midLeftPanel);
cons = new SpringLayout.Constraints();
midLeftLayout.addLayoutComponent(scrollerA, cons);
midLeftLayout.putConstraint(SpringLayout.WEST, scrollerA, 5, SpringLayout.WEST,
midLeftPanel);
midLeftLayout.putConstraint(SpringLayout.NORTH, scrollerA, 5, SpringLayout.SOUTH,
labelPanel);
midLeftLayout.putConstraint(SpringLayout.EAST, scrollerA, -5, SpringLayout.EAST,
midLeftPanel);
midLeftLayout.putConstraint(SpringLayout.SOUTH, scrollerA, -5, SpringLayout.SOUTH,
midLeftPanel);
//setup midRightPanel
midRightPanel.setLayout(midRightLayout = new SpringLayout());
midRightPanel.setBorder(BorderFactory.createTitledBorder("Mid Right Panel"));
fileAText = new JTextArea();
fileAText.setLineWrap(true);
fileAText.setWrapStyleWord(true);
fileBText = new JTextArea();
fileBText.setLineWrap(true);
fileBText.setWrapStyleWord(true);
listB = new JList();
scrollerB = new JScrollPane(listB);
listC = new JList();
scrollerC = new JScrollPane(listC);
midRightPanel.add(fileAText);
midRightPanel.add(fileBText);
midRightPanel.add(scrollerB);
midRightPanel.add(scrollerC);
cons = new SpringLayout.Constraints();
cons.setHeight(Spring.constant(25));
midRightLayout.addLayoutComponent(fileAText, cons);
midRightLayout.putConstraint(SpringLayout.WEST, fileAText, 5, SpringLayout.WEST,
midRightPanel);
midRightLayout.putConstraint(SpringLayout.NORTH, fileAText, 5, SpringLayout.NORTH,
midRightPanel);
midRightLayout.putConstraint(SpringLayout.EAST, fileAText, -5, SpringLayout.EAST,
midRightPanel);
cons = new SpringLayout.Constraints();
cons.setHeight(Spring.scale(midRightLayout.getConstraint(
SpringLayout.SOUTH, midRightPanel), 0.4f));
midRightLayout.addLayoutComponent(scrollerB, cons);
midRightLayout.putConstraint(SpringLayout.WEST, scrollerB, 5, SpringLayout.WEST,
midRightPanel);
midRightLayout.putConstraint(SpringLayout.NORTH, scrollerB, 0, SpringLayout.SOUTH,
fileAText);
midRightLayout.putConstraint(SpringLayout.EAST, scrollerB, -5, SpringLayout.EAST,
midRightPanel);
cons = new SpringLayout.Constraints();
cons.setHeight(Spring.constant(25));
midRightLayout.addLayoutComponent(fileBText, cons);
midRightLayout.putConstraint(SpringLayout.WEST, fileBText, 5, SpringLayout.WEST,
midRightPanel);
midRightLayout.putConstraint(SpringLayout.NORTH, fileBText, 5, SpringLayout.SOUTH,
scrollerB);
midRightLayout.putConstraint(SpringLayout.EAST, fileBText, -5, SpringLayout.EAST,
midRightPanel);
cons = new SpringLayout.Constraints();
cons.setHeight(Spring.scale(midRightLayout.getConstraint(
SpringLayout.SOUTH, midRightPanel), 0.4f));
midRightLayout.addLayoutComponent(scrollerC, cons);
midRightLayout.putConstraint(SpringLayout.WEST, scrollerC, 5, SpringLayout.WEST,
midRightPanel);
midRightLayout.putConstraint(SpringLayout.NORTH, scrollerC, 0, SpringLayout.SOUTH,
fileBText);
midRightLayout.putConstraint(SpringLayout.EAST, scrollerC, -5, SpringLayout.EAST,
midRightPanel);
//setup bottom panel
bottomPanel.setLayout(new BoxLayout(bottomPanel, BoxLayout.Y_AXIS));
bottomPanel.setBorder(BorderFactory.createTitledBorder("Bottom Panel"));
try {
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
} catch (ClassNotFoundException e) {
System.err.println("A problem occured setting the Look and Feel");
} catch (InstantiationException e) {
System.err.println("A problem occured setting the Look and Feel");
} catch (IllegalAccessException e) {
System.err.println("A problem occured setting the Look and Feel");
} catch (UnsupportedLookAndFeelException e) {
System.err.println("A problem occured setting the Look and Feel");
}
SwingUtilities.updateComponentTreeUI(this);
setVisible(true);
setLocationRelativeTo(null);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}//end createAndShowGUI()
}
_______________________________________________
Do not post admin requests to the list. They will be ignored.
Help/Unsubscribe/Update your Subscription: