Propogating minimumSize

I have always had this problem with Swing. I feel as if my solutions are always work-arounds. So I thought I'd ask in here if there are any defacto solutions.

Problem:

I have a simple JPanel with some components on it. I want the components to assume their preferred size on start up and retain at least their minimumSize while the GUI is in use.

Specific example:

A JFrame that contains:

ContentPane:

BorderLayout:

CENTER: JTextField

EAST: JPanel

GridLayout(1,2)

1,1: JButton

1,2: JButton

I'd like to enforce that the JTextField should never be smaller than 50,20. The JButtons should never be smaller than 20,10. The JFrame should never be smaller than the space required by the JTextField and JButtons minimumSize.

Basically if I call pack() on the JFrame, I'd like the window to restore to the minimumSize required to honor all the minimumSizes of the sub components, right down to the JButtons.

The user would be prevented from reducing the window size further.

It would also be cool to have the window initially open with all components at "preferredSize" on call to pack().

I thought this was the point of perferredSize and minimumSize?

Maybe my problem is with using pack().

Any help would be great. My previous solutions have been to set sizes on the parent components all the way up through the hierarchy of panels and then call pack() followed by a sensible looking setSize() on the main JFrame. This seems ugly.

Cheers,

Paul

[1569 byte] By [PCampbella] at [2007-11-27 4:50:30]
# 1

something to play around with

import javax.swing.*;

import java.awt.*;

class Testing

{

public void buildGUI()

{

JFrame.setDefaultLookAndFeelDecorated(true);

JPanel holdingPanel1 = new JPanel(new GridBagLayout());

JPanel holdingPanel2 = new JPanel(new GridBagLayout());

JPanel holdingPanel3 = new JPanel(new GridBagLayout());

holdingPanel1.add(new JTextField(10),new GridBagConstraints());

holdingPanel2.add(new JButton("Button 1"),new GridBagConstraints());

holdingPanel3.add(new JButton("Button 2"),new GridBagConstraints());

JPanel p = new JPanel(new GridLayout(2,1));

p.add(holdingPanel2);

p.add(holdingPanel3);

JFrame f = new JFrame();

f.getContentPane().add(holdingPanel1,BorderLayout.CENTER);

f.getContentPane().add(p,BorderLayout.EAST);

f.pack();

f.getContentPane().setMinimumSize(f.getContentPane().getPreferredSize());

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-12 10:03:52 > top of Java-index,Desktop,Core GUI APIs...
# 2

> It would also be cool to have the window initially open with all components at "preferredSize" on call to pack().

Thats what the pack method does. It sizes the frame so all components are displayed at their preferred size. So the order of the code would be:

frame.getContentPane().add(....);

frame.pack();

frame.setVisible();

> I thought this was the point of perferredSize and minimumSize?

Read the Swing tutorial on [url http://java.sun.com/docs/books/tutorial/uiswing/layout/visual.html]How to Use Layout Managers[/url]. The preferred/minimum/maximum sizes are just suggestions for the layout manager.

camickra at 2007-7-12 10:03:52 > top of Java-index,Desktop,Core GUI APIs...