Swing Layout management - how to specify size?

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);
    }
}
RonanLAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

zzynxSoftware engineerCommented:
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
RonanLAuthor Commented:
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
zzynxSoftware engineerCommented:
> 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

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
amswainCommented:
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
zzynxSoftware engineerCommented:
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
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Fonts Typography

From novice to tech pro — start learning today.