Solved

Correct usage of component size hints

Posted on 2004-09-14
16
146 Views
Last Modified: 2010-03-31
Hi all,

This question is about the correct way to use the setPreferredSize, setMaximumSize and setMinimumSize functions.

I'm teaching myself Java by designing an application.  I'm particularly keen to get it looking aesthetically pleasing but I also don't want to break all the rules regarding Java's multi-platform support.

Bearing these two things in mind, could people tell me what I'm doing right/wrong in the following:

My application is a bit like an install program - several pages connected by "forward" and "back" buttons.  Each page has a panel containing data at the top, and a thin panel containing the buttons at the bottom.  There are two main things I want regarding size of components:

1) When the vertical window size is changed by the user I want the bottom panel containing the buttons to stay a constant height, and the top pannel to expand.  To do this currently I use

lowerpanel.setMaximumSize(new Dimension(999999, 40);  

I've set width to be 999999 because I don't care how wide it gets - I just want it to expand to fill the full width.   However, this doesn't seem likely to be the best way to do it to me! (?)

2)  I want the 'forward' and 'back' buttons to be the same size, and to take up a fair amount of the bottom panel, but leave a gap of blank space between them, which expands when the window is expanded horizontally.

To do this I use

button.setMaximumSize(250, 40);
button.setMinimumSize(250, 40);

Again this just doesn't seem right to me.  Is there a better way?

And in general should I be using all these set max and min size methods?  Is this bad java programming because it might screw up the way it looks on other platforms?
0
Comment
Question by:agrees
  • 5
  • 4
  • 4
  • +1
16 Comments
 
LVL 86

Expert Comment

by:CEHJ
ID: 12055257
You should really try to use the various layout managers:

http://java.sun.com/docs/books/tutorial/uiswing/layout/using.html
0
 
LVL 37

Expert Comment

by:zzynx
ID: 12055287
Looks like you need a thorough read of http://java.sun.com/docs/books/tutorial/uiswing/layout/
You have to learn yourself how the different layout managers work
0
 

Author Comment

by:agrees
ID: 12055384
Sorry I forgot to say, the layout is two boxlayouts with the top and lower panels oriented in a Y-axis and the buttons and blank space within the lower panel in an X-axis.

I played around with other layouts (including the dreaded gridbag) but boxlayout seemed the best for this - is it not?
0
 
LVL 37

Expert Comment

by:zzynx
ID: 12055399
What about this:

/*
 * Demo.java
 *
 */

import javax.swing.*;

public class Demo extends javax.swing.JPanel {
   
    public Demo() {
        initComponents();
    }
   
    private void initComponents() {
        innerPanel = new javax.swing.JPanel();
        southPanel = new javax.swing.JPanel();
        buttonPanel = new javax.swing.JPanel();
        button1 = new javax.swing.JButton();
        button2 = new javax.swing.JButton();

        setLayout(new java.awt.BorderLayout());

        innerPanel.setBorder(new javax.swing.border.TitledBorder("This is the inner panel"));
        add(innerPanel, java.awt.BorderLayout.CENTER);

        southPanel.setLayout(new java.awt.FlowLayout(java.awt.FlowLayout.RIGHT));

        buttonPanel.setLayout(new java.awt.GridLayout(1, 0, 6, 0));

        button1.setText("Button 1");
        buttonPanel.add(button1);

        button2.setText("Button 2");
        buttonPanel.add(button2);

        southPanel.add(buttonPanel);

        add(southPanel, java.awt.BorderLayout.SOUTH);

    }
   
    private javax.swing.JButton button1;
    private javax.swing.JButton button2;
    private javax.swing.JPanel buttonPanel;
    private javax.swing.JPanel innerPanel;
    private javax.swing.JPanel southPanel;
   
    public static void main(String[] args) {
        JFrame frame = new JFrame("Size me...");
        frame.getContentPane().add(new Demo());
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setSize(400, 400);
        frame.setLocationRelativeTo(null); // center on screen
        frame.show();
    }
}
0
 

Author Comment

by:agrees
ID: 12055865
Ah yes a borderlayout seems like a really good way to sort out the horizontal part of it!

One problem with the buttons panel though - I'd like the buttons to be arranged at either end of the panel, so that button1 is flush with the left hand edge, and button2 with the right.  I can sort of get this effect by getting rid of 'southpanel' and just adding 'buttonpanel' directly to the contentpane

ie:

add(buttonpanel , java.awt.BorderLayout.SOUTH);

Is this ok to do?  

Even doing this there still isn't a gap between the buttons which I would like, but I guess I can get this by adding some sort of invisible component between the buttons?
0
 

Author Comment

by:agrees
ID: 12058442
Hmmm well I've almost got there by putting a borderlayout into the bottom panel.  The only problem is that if the buttons have different text in them they have different sizes to each other, which I was hoping to avoid.  Is the only way to make them the same size to use a boxlayout?

Here it is so far:

import javax.swing.*;
import java.awt.BorderLayout;

public class Ecology extends javax.swing.JPanel {

    public Ecology() {
        initComponents();
    }

    private void initComponents() {
        innerPanel = new javax.swing.JPanel();
        //southPanel = new javax.swing.JPanel();
        buttonPanel = new javax.swing.JPanel();
        button1 = new javax.swing.JButton();
        button2 = new javax.swing.JButton();

        setLayout(new java.awt.BorderLayout());

        innerPanel.setBorder(new javax.swing.border.TitledBorder("This is the inner panel"));
        add(innerPanel, java.awt.BorderLayout.CENTER);

        //southPanel.setLayout(new java.awt.FlowLayout(java.awt.FlowLayout.RIGHT));

//        buttonPanel.setLayout(new java.awt.GridLayout(1, 0, 6, 0));
        buttonPanel.setLayout(new java.awt.BorderLayout());
       

        button1.setText("<--- BACK plus some text!");
        buttonPanel.add(button1, BorderLayout.WEST);
       
        button2.setText("NEXT --->");
        buttonPanel.add(button2, BorderLayout.EAST);

        //southPanel.add(buttonPanel);

        //add(southPanel, java.awt.BorderLayout.SOUTH);
        add(buttonPanel, java.awt.BorderLayout.SOUTH);

    }

    private javax.swing.JButton button1;
    private javax.swing.JButton button2;
    private javax.swing.JPanel buttonPanel;
    private javax.swing.JPanel innerPanel;
    private javax.swing.JPanel southPanel;

    public static void main(String[] args) {
        JFrame frame = new JFrame("Size me...");
        frame.getContentPane().add(new Ecology());
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setSize(400, 400);
        frame.setLocationRelativeTo(null); // center on screen
        frame.show();
    }
}
0
 
LVL 86

Assisted Solution

by:CEHJ
CEHJ earned 20 total points
ID: 12058467
>>The only problem is that if the buttons have different text in them they have different sizes to each other

Certain layout managers will handle that, but you can kludge it by space-padding the button text so that the text *is* the same length
0
 

Author Comment

by:agrees
ID: 12058802
>> Certain layout managers will handle that, but you can kludge it by space-padding the button text so that the text *is* the same length

What would you say was the worst kludge - spaces padding like that or using a boxLayout and using setMinimumSize and setMaximumSize?
0
How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

 
LVL 86

Expert Comment

by:CEHJ
ID: 12059244
Well i don't know about worse or better, but the former certainly seems easier ;-)
0
 
LVL 92

Expert Comment

by:objects
ID: 12060419
Just set their preferred size as required.
0
 
LVL 37

Accepted Solution

by:
zzynx earned 105 total points
ID: 12061873
>>add(buttonpanel , java.awt.BorderLayout.SOUTH);
>>Is this ok to do?
Yes, but in this case you could even work without buttonPanel:

>> Is the only way to make them the same size to use a boxlayout?
No. But without setting sizes, yes.

/*
 * Demo.java
 *
 */
import javax.swing.*;

public class Demo extends javax.swing.JPanel {
   
    public Demo() {
        initComponents();
    }
   
    private void initComponents() {
        innerPanel = new javax.swing.JPanel();
        southPanel = new javax.swing.JPanel();
        button1 = new javax.swing.JButton();
        button2 = new javax.swing.JButton();

        setLayout(new java.awt.BorderLayout());

        innerPanel.setBorder(new javax.swing.border.TitledBorder("This is the inner panel"));
        add(innerPanel, java.awt.BorderLayout.CENTER);

        southPanel.setLayout(new java.awt.BorderLayout());

        southPanel.setBorder(new javax.swing.border.EmptyBorder(new java.awt.Insets(1, 1, 1, 1)));
        button1.setText("Verly long text");
        button1.setPreferredSize(new java.awt.Dimension(120, 23));
        southPanel.add(button1, java.awt.BorderLayout.WEST);

        button2.setText("Shorty");
        button2.setPreferredSize(new java.awt.Dimension(120, 23));
        southPanel.add(button2, java.awt.BorderLayout.EAST);

        add(southPanel, java.awt.BorderLayout.SOUTH);

    }
   
    private javax.swing.JButton button1;
    private javax.swing.JButton button2;
    private javax.swing.JPanel innerPanel;
    private javax.swing.JPanel southPanel;
   
    public static void main(String[] args) {
        JFrame frame = new JFrame("Size me...");
        frame.getContentPane().add(new Demo());
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setSize(400, 400);
        frame.setLocationRelativeTo(null); // center on screen
        frame.show();
    }
}
0
 
LVL 92

Expert Comment

by:objects
ID: 12061904
I already mentioned those changes :)
0
 

Author Comment

by:agrees
ID: 12073470
Thanks, I think that's got it sorted :)
0
 
LVL 37

Expert Comment

by:zzynx
ID: 12073491
Thanks for accepting
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 12073753
8-)
0
 
LVL 92

Expert Comment

by:objects
ID: 12080180
Good to see my suggestion solved your problem :)
0

Featured Post

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
count8 challlenge 13 86
json format text only 4 65
backtracking recursion  code 19 40
Java - Why doesn't this JFrame work 3 18
By the end of 1980s, object oriented programming using languages like C++, Simula69 and ObjectPascal gained momentum. It looked like programmers finally found the perfect language. C++ successfully combined the object oriented principles of Simula w…
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 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 the regular for loop in Java and how to use it. Definition: Break the for loop down into 3 parts: Syntax when using for loops: Example using a for loop:

760 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

17 Experts available now in Live!

Get 1:1 Help Now