Solved

Swing Layout management - how to specify size?

Posted on 2003-10-23
5
556 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

Transaction Monitoring Vs. Real User Monitoring

Synthetic Transaction Monitoring Vs. Real User Monitoring: When To Use Each Approach? In this article, we will discuss two major monitoring approaches: Synthetic Transaction and Real User Monitoring.

Question has a verified solution.

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

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…
Basic understanding on "OO- Object Orientation" is needed for designing a logical solution to solve a problem. Basic OOAD is a prerequisite for a coder to ensure that they follow the basic design of OO. This would help developers to understand the b…
Viewers learn about the “for” loop and how it works in Java. By comparing it to the while loop learned before, viewers can make the transition easily. You will learn about the formatting of the for loop as we write a program that prints even numbers…
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…

729 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