Solved

Swing Layout management - how to specify size?

Posted on 2003-10-23
5
551 Views
Last Modified: 2013-12-03
Hello.

I am developing a standalone Java app for the first time so I am new to Swing. I have a form that I want to display a few text fields and a save button at the bottom. I would like to make the form look well, and stay the same size. So far I have been able to make the items appear in roughly the correct order, but when I resize the window they get massive etc. Having looked around, the GridBagLayout manager seems to be the most flexible but I can't get to grips with it at all. I have cut the important code out and pasted it below. This results in my labels, fields and button all crammed in the middle of the screen. Could someone help me in specifying the size, location of these objects? I also want them to stay the correct size, even if the window is expanded. Please help me because me head is wrecked with this stuff!

Thanks,

Ronan

Here's the code:

public class ConfigurationPanel extends JPanel
{
    private ConfigurationSingleton configSingleton = ConfigurationSingleton.getConfigurationSingleton();
    private JLabel logDirLabel = new JLabel("Log Directory: ");
    private JLabel reportDirLabel = new JLabel("Report Directory: ");
    private JLabel logFileLabel = new JLabel("Log File Name: ");
    private JTextField logDirField = new JTextField(configSingleton.getLog());
    private JTextField reportDirField = new JTextField(configSingleton.getReport());
    private JTextField logFileField = new JTextField(configSingleton.getLogFileName());
    protected JButton saveButton;
   
    /** Creates a new instance of Configuration */
    public ConfigurationPanel()
    {      
        GridBagLayout gridbag = new GridBagLayout();
        GridBagConstraints c = new GridBagConstraints();
        setLayout(gridbag);
       
        c.fill = GridBagConstraints.HORIZONTAL;
        c.gridx = 0;
        c.gridy = 0;
        gridbag.setConstraints(logDirLabel, c);
        add(logDirLabel);
       
        c.gridx = 1;
        c.gridy = 0;
        gridbag.setConstraints(logDirField, c);
        add(logDirField);
       
        c.gridx = 0;
        c.gridy = 1;
        gridbag.setConstraints(reportDirLabel, c);
        add(reportDirLabel);
       
        c.gridx = 1;
        c.gridy = 1;
        gridbag.setConstraints(reportDirField, c);
        add(reportDirField);
       
        c.gridx = 0;
        c.gridy = 2;
        gridbag.setConstraints(logFileLabel, c);
        add(logFileLabel);
       
        c.gridx = 1;
        c.gridy = 2;
        gridbag.setConstraints(logFileField, c);
        add(logFileField);
       
        saveButton = new JButton();
        c.gridx = 0;
        c.gridy = 3;
        gridbag.setConstraints(saveButton, c);
        add(saveButton);
    }
}
0
Comment
Question by:RonanL
  • 3
5 Comments
 
LVL 37

Expert Comment

by:zzynx
ID: 9606992
I think this is what you want:

    public ConfigurationPanel()
    {      
        GridBagLayout gridbag = new GridBagLayout();
        GridBagConstraints c = new GridBagConstraints();
       
        setLayout(new BorderLayout(0,0));
       
        JPanel mainPanel = new JPanel();
        mainPanel.setLayout(new FlowLayout(FlowLayout.LEFT));
        add(mainPanel, BorderLayout.CENTER);
       
        JPanel centerPnl = new JPanel();
        centerPnl.setLayout(gridbag);
        mainPanel.add(centerPnl);
       
        c.fill = GridBagConstraints.HORIZONTAL;
        c.gridx = 0;
        c.gridy = 0;
        gridbag.setConstraints(logDirLabel, c);
        centerPnl.add(logDirLabel);
       
        c.gridx = 1;
        c.gridy = 0;
        gridbag.setConstraints(logDirField, c);
        centerPnl.add(logDirField);
       
        c.gridx = 0;
        c.gridy = 1;
        gridbag.setConstraints(reportDirLabel, c);
        centerPnl.add(reportDirLabel);
       
        c.gridx = 1;
        c.gridy = 1;
        gridbag.setConstraints(reportDirField, c);
        centerPnl.add(reportDirField);
       
        c.gridx = 0;
        c.gridy = 2;
        gridbag.setConstraints(logFileLabel, c);
        centerPnl.add(logFileLabel);
       
        c.gridx = 1;
        c.gridy = 2;
        gridbag.setConstraints(logFileField, c);
        centerPnl.add(logFileField);
       
        JPanel southPanel = new JPanel();
        add(southPanel, BorderLayout.SOUTH);
       
        JPanel buttonPanel = new JPanel();
        buttonPanel.setLayout(new GridLayout(1, 0, 0, 0));
        buttonPanel.add(new JButton("Save"));
       
        southPanel.add(buttonPanel);
    }

The bottom line I learned: whenever you experience positioning problems don't be sparing with JPanels.
0
 

Author Comment

by:RonanL
ID: 9607185
Thanks for going to so much trouble. I put in your code and it moved the labels and fields into the top left corner, but the save button is way down the bottom on the left.

Also, while the most of the stuff is in the correct area (on the left), they are all too small. The labels and fields only seem to grow to the size of what they contain. Is there any way to play with their size, alignment. I have tried using setSize methods all over the place since I started looking at this swing stuff and it never seems to make any difference.
0
 
LVL 37

Accepted Solution

by:
zzynx earned 50 total points
ID: 9607444
> I put in your code and it moved the labels and fields into the top left corner, but the save > button is way down the bottom on the left.

That is exactly where I expected/wanted them to be.
Where do you want to be what?

If you want all three textfields to have the size of the longest entry then this is the way:
(I also added 3x
                                   c.anchor = GridBagConstraints.WEST;
 to have a nicer alignment of the labels. I forgot it.)

        c.fill = GridBagConstraints.HORIZONTAL;
        c.gridx = 0;
        c.gridy = 0;
        c.fill = GridBagConstraints.NONE;
        c.anchor = GridBagConstraints.WEST;
        gridbag.setConstraints(logDirLabel, c);
        centerPnl.add(logDirLabel);
       
        c.gridx = 1;
        c.gridy = 0;
        c.fill = GridBagConstraints.HORIZONTAL;
        gridbag.setConstraints(logDirField, c);
        centerPnl.add(logDirField);
       
        c.gridx = 0;
        c.gridy = 1;
        c.fill = GridBagConstraints.NONE;
        c.anchor = GridBagConstraints.WEST;
        gridbag.setConstraints(reportDirLabel, c);
        centerPnl.add(reportDirLabel);
       
        c.gridx = 1;
        c.gridy = 1;
        c.fill = GridBagConstraints.HORIZONTAL;
        gridbag.setConstraints(reportDirField, c);
        centerPnl.add(reportDirField);
       
        c.gridx = 0;
        c.gridy = 2;
        c.fill = GridBagConstraints.NONE;
        c.anchor = GridBagConstraints.WEST;
        gridbag.setConstraints(logFileLabel, c);
        centerPnl.add(logFileLabel);
       
        c.gridx = 1;
        c.gridy = 2;
        c.fill = GridBagConstraints.HORIZONTAL;
        gridbag.setConstraints(logFileField, c);
        centerPnl.add(logFileField);

But I think you want the three fields all to have a certain fixed width, don't you?
Well, since the layout managers in Java are made to always have nice screen whatever the size of the window is, I don't think setting a fixed length is a good idea.

What you could do is making your layout that way that the three fields always grow and take all the available space on the right. If you want this you need to place them in a BorderLayout. This layout does that (growing the content as much as possible) per definition. Then you need to set the size of your dialog and the fields will take all "available" space.

I don't have the time left for the moment to produce the code, but that's the idea.
Success.
0
 
LVL 1

Expert Comment

by:amswain
ID: 9607477
use setPreferredSize() and setMinimumSize() on your text fields and labels etc to make them bigger.  setSize() won't work because the layout manager decides on the size of each component!
0
 
LVL 37

Expert Comment

by:zzynx
ID: 9607616
Good point of amswain.
Keep the code I provided but start with

public ConfigurationPanel()
{
    reportDirField.setPreferredSize(new Dimension(250, 20));
    ...
}

and you've got your fixed width. But as I told already....

Thanks for your acceptance.
Bye.
0

Featured Post

Is Your AD Toolbox Looking More Like a Toybox?

Managing Active Directory can get complicated.  Often, the native tools for managing AD are just not up to the task.  The largest Active Directory installations in the world have relied on one tool to manage their day-to-day administration tasks: Hyena. Start your trial today.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
jar file executable 12 51
servlet  URL Rewriting 1 36
oracle 11g 23 80
maven module vs maven project 3 23
Java had always been an easily readable and understandable language.  Some relatively recent changes in the language seem to be changing this pretty fast, and anyone that had not seen any Java code for the last 5 years will possibly have issues unde…
Introduction Java can be integrated with native programs using an interface called JNI(Java Native Interface). Native programs are programs which can directly run on the processor. JNI is simply a naming and calling convention so that the JVM (Java…
This theoretical tutorial explains exceptions, reasons for exceptions, different categories of exception and exception hierarchy.
This video teaches viewers about errors in exception handling.

770 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question