Solved

Swing Layout management - how to specify size?

Posted on 2003-10-23
5
549 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
Comment Utility
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
Comment Utility
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
Comment Utility
> 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
Comment Utility
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
Comment Utility
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

How to improve team productivity

Quip adds documents, spreadsheets, and tasklists to your Slack experience
- Elevate ideas to Quip docs
- Share Quip docs in Slack
- Get notified of changes to your docs
- Available on iOS/Android/Desktop/Web
- Online/Offline

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
powerN  challenge 3 47
Books that can get me started on JAVA 2 52
mapShare challenge 13 68
Image decoding from Camera 3 42
INTRODUCTION Working with files is a moderately common task in Java.  For most projects hard coding the file names, using parameters in configuration files, or using command-line arguments is sufficient.   However, when your application has vi…
For beginner Java programmers or at least those new to the Eclipse IDE, the following tutorial will show some (four) ways in which you can import your Java projects to your Eclipse workbench. Introduction While learning Java can be done with…
Video by: Michael
Viewers learn about how to reduce the potential repetitiveness of coding in main by developing methods to perform specific tasks for their program. Additionally, objects are introduced for the purpose of learning how to call methods in Java. Define …
This tutorial will introduce the viewer to VisualVM for the Java platform application. This video explains an example program and covers the Overview, Monitor, and Heap Dump tabs.

743 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

Need Help in Real-Time?

Connect with top rated Experts

16 Experts available now in Live!

Get 1:1 Help Now