Solved

Swing Layout management - how to specify size?

Posted on 2003-10-23
5
554 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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

Salesforce Made Easy to Use

On-screen guidance at the moment of need enables you & your employees to focus on the core, you can now boost your adoption rates swiftly and simply with one easy tool.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
mysql jsp example issue 32 75
How to install a font on WIN2003SBS/IIS 6 & test 17 54
jmeter usage 4 38
How to change fonts in windows live mail 3 22
This was posted to the Netbeans forum a Feb, 2010 and I also sent it to Verisign. Who didn't help much in my struggles to get my application signed. ------------------------- Start The idea here is to target your cell phones with the correct…
Introduction This article is the second of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers the basic installation and configuration of the test automation tools used by…
Viewers will learn about arithmetic and Boolean expressions in Java and the logical operators used to create Boolean expressions. We will cover the symbols used for arithmetic expressions and define each logical operator and how to use them in Boole…
Viewers will learn one way to get user input in Java. Introduce the Scanner object: Declare the variable that stores the user input: An example prompting the user for input: Methods you need to invoke in order to properly get  user input:

726 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