• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 158
  • Last Modified:

Correct usage of component size hints

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
agrees
Asked:
agrees
  • 5
  • 4
  • 4
  • +1
2 Solutions
 
CEHJCommented:
You should really try to use the various layout managers:

http://java.sun.com/docs/books/tutorial/uiswing/layout/using.html
0
 
zzynxSoftware engineerCommented:
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
 
agreesAuthor Commented:
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
The new generation of project management tools

With monday.com’s project management tool, you can see what everyone on your team is working in a single glance. Its intuitive dashboards are customizable, so you can create systems that work for you.

 
zzynxSoftware engineerCommented:
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
 
agreesAuthor Commented:
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
 
agreesAuthor Commented:
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
 
CEHJCommented:
>>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
 
agreesAuthor Commented:
>> 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
 
CEHJCommented:
Well i don't know about worse or better, but the former certainly seems easier ;-)
0
 
objectsCommented:
Just set their preferred size as required.
0
 
zzynxSoftware engineerCommented:
>>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
 
objectsCommented:
I already mentioned those changes :)
0
 
agreesAuthor Commented:
Thanks, I think that's got it sorted :)
0
 
zzynxSoftware engineerCommented:
Thanks for accepting
0
 
CEHJCommented:
8-)
0
 
objectsCommented:
Good to see my suggestion solved your problem :)
0

Featured Post

The new generation of project management tools

With monday.com’s project management tool, you can see what everyone on your team is working in a single glance. Its intuitive dashboards are customizable, so you can create systems that work for you.

  • 5
  • 4
  • 4
  • +1
Tackle projects and never again get stuck behind a technical roadblock.
Join Now