views:

710

answers:

3

I have a simple form like this:

Username: ..........

Password: ..........

When the form is re-sized, I would expect JTextField (illustrated by .........) to be re-sized horizontally to fit the new width but not vertically (the same height for JTextField). Do we have any way to control this?

Thanks!

+1  A: 

That depends on the layout manager you have used.

The default layout (FlowLayout)will not resize the components at all.

If you want fine grained control over how your components resize, look into GridBagLayout.

The easy way out though, is to use setResizable(false).
For something as simple as a login window, that is probably the way you want to go.

If you are interested in using different layouts, you may want to look into the Java Tutorials. They have a great section on laying out components.

jjnguy
btw: FlowLayout is the default for (j)panels; BorderLayout is the default for (j)frames
Scott Stanchfield
Scott Stanchfield
If you make sure to call pack(), the GUI will resize to a good size, then you use steResizable() to make sure it stays that size.
jjnguy
pack() may help, but it tends to leave things a little tight. I understand why you're suggesting it (layout managers definitely aren't trivial), but it makes for a less friendly UI. My name being somewhat long, I like to resize dialogs so I can see it all at once
Scott Stanchfield
+1  A: 
VonC
3rd party layout managers feel like cheating. But that manager looks really cool.
jjnguy
@jjnguy; I agree ;) I only use this one now: it replaces all the others.
VonC
+2  A: 

To answer your layout with the standard layout managers (except GridBag), nest layouts as follows:

BorderLayout
    NORTH=BorderLayout   // keep everything at the top
        WEST=GridLayout(0,1) // the labels
            Label "UserName")
            Label "Password")
        CENTER=GridLayout(0,1) // the fields
            Text Field
            Password Field

Code would look something like

JPanel outer = new JPanel(new BorderLayout());
JPanel top = new JPanel(new BorderLayout());
JPanel labels = new JPanel(new GridLayout(0,1,3,3));
JPanel fields = new JPanel(new GridLayout(0,1,3,3));
outer.add(top, BorderLayout.NORTH);
top.add(labels, BorderLayout.WEST);
top.add(fields, BorderLayout.CENTER);
labels.add(new JLabel("Username"));
labels.add(new JLabel("Password"));
fields.add(new JTextField());
fields.add(new JPasswordField());

See http://developer.java.sun.com/developer/onlineTraining/GUI/AWTLayoutMgr/ for my (very old) explanation of how to nest layout managers like this.

GridBag is evil incarnate; just about impossible to figure out what the code is doing if you have more than a few components. To show how this would be done, though:

Insets i = new Insets(0,0,0,0);
p.setLayout(new GridBagLayout());
p.add(new JLabel("Username"), 
  new GridBagConstraints(0, 0, 1, 1, 0, 0, 
    GridBagConstraints.WEST, GridBagConstraints.NONE, i, 0, 0));
p.add(new JLabel("Password"), 
  new GridBagConstraints(0, 1, 1, 1, 0, 1, 
    GridBagConstraints.NORTHWEST, GridBagConstraints.NONE, i, 0, 0));
p.add(new JTextField(), 
  new GridBagConstraints(1, 0, 1, 1, 1, 0, 
    GridBagConstraints.CENTER, GridBagConstraints.HORIZONTAL, i, 0, 0));
p.add(new JPasswordField(), 
  new GridBagConstraints(1, 1, 1, 1, 1, 1, 
    GridBagConstraints.NORTH, GridBagConstraints.HORIZONTAL, i, 0, 0));

It works, but it's hard to "see" the layout in the code...

Scott Stanchfield