Hi!
At first, I have to complain, that these Layout Managers drive me crazy. Even as an experienced developer, very often, I am frustrated and I am never 100% sure which layout manager I should choose (according to the Sun How Tos) and once I have decided, the never do what I'd expect them to do. I'm not sure if I am just too dumb or if there might be an unreasonable API architecture/design...
OK, here again is a problem with layouts. And it's basically like this (See code for details):
Row one: Text Area 1, Button 1; Row two: Text Area 2, Button 2.
I just want to have the buttons aligned, the text areas should be same sized. Moreover, I want to disable vertical resizing and on horizontal resizing, I want the TextArea to be resized as well.
But it doesn't. I could partly solve the vertical resizing by setting the minimum size, but the maximum size dosen't seem to work. I can still enlarge the window. The horizontal resizing doesn't work at all. I tried border layout before and set the Text area as center, but it also doesn't work. I read that some layout managers use only preferred size, some also use min and max size and this is very confusing.
And I saw that component resizing should work for box layout as well (see Sun box layout how to). If this problem is easy to solve, please excuse my question, but I couldn't find it in the forum.
OK, here is an SSCCE:
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package com.ettex.componentresizetest;
import java.awt.ComponentOrientation;
import java.awt.Container;
import java.awt.Dimension;
import java.awt.FlowLayout;
import javax.swing.BoxLayout;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;
/**
*
* @author Jan Wedel
*/
public class ComponentResizeTest extends JFrame{
static ComponentResizeTest frame;
JPanel mainPanel;
JPanel firstPanel;
JPanel secondPanel;
JPanel statusPanel;
JButton firstButton;
JButton secondButton;
JTextField firstField;
JTextField secondField;
JLabel statusText;
public ComponentResizeTest(String name) {
super(name);
}
public void addComponentsToPane() {
Container pane = this.getContentPane();
mainPanel = new JPanel();
firstPanel = new JPanel();
secondPanel = new JPanel();
statusPanel = new JPanel();
pane.setLayout(new BoxLayout(pane, BoxLayout.Y_AXIS));
firstField = new JTextField();
secondField = new JTextField();
statusText = new JLabel("Status Bar");
firstField.setPreferredSize(new Dimension(500, 25));
secondField.setPreferredSize(new Dimension(500, 25));
secondButton = new JButton("A Button");
firstButton = new JButton("Another Button");
firstPanel.add(firstField);
firstPanel.add(secondButton);
firstPanel.setLayout(new BoxLayout(firstPanel, BoxLayout.X_AXIS));
secondPanel.add(secondField);
secondPanel.add(firstButton);
secondPanel.setLayout(new BoxLayout(secondPanel, BoxLayout.X_AXIS));
mainPanel.add(firstPanel);
mainPanel.add(secondPanel);
mainPanel.setLayout(new BoxLayout(mainPanel, BoxLayout.Y_AXIS));
statusPanel.add(statusText);
FlowLayout tabLayout = new FlowLayout();
tabLayout.setAlignment(FlowLayout.LEFT);
firstPanel.setLayout(tabLayout);
firstPanel.setComponentOrientation(ComponentOrientation.LEFT_TO_RIGHT);
secondPanel.setLayout(tabLayout);
secondPanel.setComponentOrientation(ComponentOrientation.LEFT_TO_RIGHT);
statusPanel.setLayout(tabLayout);
statusPanel.setComponentOrientation(ComponentOrientation.LEFT_TO_RIGHT);
pane.add(mainPanel);
pane.add(statusPanel);
}
/**
* Create the GUI and show it. For thread safety,
* this method should be invoked from the
* event dispatch thread.
*/
private static void createAndShowGUI() {
//Create and set up the window.
frame = new ComponentResizeTest("Component Resize Test");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
//Set up the content pane.
frame.addComponentsToPane();
/* Layout componets */
frame.pack();
/* Prevent vertical resize */
Dimension cur = frame.getSize();
Dimension max = frame.getMaximumSize();
frame.setMinimumSize(new Dimension(cur.width, cur.height));
frame.setMaximumSize(new Dimension(max.width, cur.height));
frame.invalidate();
/* show frame */
frame.setVisible(true);
}
/**
* @param args the command line arguments
*/
public static void main(String[] args) {
//Schedule a job for the event dispatchi thread:
//creating and showing this application's GUI.
javax.swing.SwingUtilities.invokeLater(new Runnable() {
public void run() {
createAndShowGUI();
}
});
}
}
Thanks, Jan
Edited by: stelzbock on Nov 9, 2009 6:54 AM