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
Solved

Correct usage of component size hints

Posted on 2004-09-14
16
149 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
Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

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

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
more than one jdk and one jre 1 50
Maven Project: Hibernate Dependencies Conflict 10 37
arguments to jar 5 33
Why my table column Id is not passed to java object? 4 44
After being asked a question last year, I went into one of my moods where I did some research and code just for the fun and learning of it all.  Subsequently, from this journey, I put together this article on "Range Searching Using Visual Basic.NET …
Are you developing a Java application and want to create Excel Spreadsheets? You have come to the right place, this article will describe how you can create Excel Spreadsheets from a Java Application. For the purposes of this article, I will be u…
Viewers learn about the third conditional statement “else if” and use it in an example program. Then additional information about conditional statements is provided, covering the topic thoroughly. Viewers learn about the third conditional statement …
Viewers will learn about the different types of variables in Java and how to declare them. Decide the type of variable desired: Put the keyword corresponding to the type of variable in front of the variable name: Use the equal sign to assign a v…

791 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