Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

Correct usage of component size hints

Posted on 2004-09-14
16
Medium Priority
?
155 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
[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
  • 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
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

 
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 80 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
 
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 420 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

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

Question has a verified solution.

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

An old method to applying the Singleton pattern in your Java code is to check if a static instance, defined in the same class that needs to be instantiated once and only once, is null and then create a new instance; otherwise, the pre-existing insta…
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…
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 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:
Suggested Courses

618 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