954,153 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

JPanel's size and JButton

When I add buttons to a panel, the buttons seem to take a huge amount of space. How do I get rid of the space? For example:

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

public class Tester
{
	public static void main(String[] args)
	{
		JButton dayButton = new JButton("Day");
		JButton weekButton = new JButton("Week");
		JButton monthButton = new JButton("Month");
		JButton leftButton = new JButton("<<");
		JButton rightButton = new JButton(">>");

		JPanel topButtonPanel = new JPanel();
		topButtonPanel.add(dayButton);
		topButtonPanel.add(weekButton);
		topButtonPanel.add(monthButton);

		JPanel bottomButtonPanel = new JPanel();
		bottomButtonPanel.add(leftButton);
		bottomButtonPanel.add(rightButton);

		JPanel buttonPanel = new JPanel();
		buttonPanel.setLayout(new BoxLayout(buttonPanel, BoxLayout.Y_AXIS));
		buttonPanel.add(topButtonPanel);
		buttonPanel.add(bottomButtonPanel);

		JFrame frame = new JFrame();

		frame.add(buttonPanel);

		frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		frame.setSize(FRAME_WIDTH, FRAME_HEIGHT);
		frame.setVisible(true);
	}
	private static final int FRAME_WIDTH = 1000;
	private static final int FRAME_HEIGHT = 1000;
}
degamer106
Junior Poster
131 posts since Mar 2006
Reputation Points: 10
Solved Threads: 0
 
Ezzaral
Posting Genius
Moderator
15,985 posts since May 2007
Reputation Points: 3,250
Solved Threads: 847
 

Ok I got it.

A question related to layout managers though: I know BoxLayout tends to "stretch" the components to make them fit inside a panel. How do I modify the panel's dimensions?

degamer106
Junior Poster
131 posts since Mar 2006
Reputation Points: 10
Solved Threads: 0
 

You can setPreferredSize() and some layout managers will honor it, but it's not guaranteed. It depends on the layout manager how component sizing is handled. You would need to be more specific about the placement and sizing behavior you are wanting to give a more direct answer.

Ezzaral
Posting Genius
Moderator
15,985 posts since May 2007
Reputation Points: 3,250
Solved Threads: 847
 

I have a bunch of text and combo boxes. For example,

Day: [Day Combo Box]
Date: [Date Combo Box]
AMPM: [AMPM Combo Box]

I used BoxLayout.Y_AXIS to align them vertically but the boxes seem to stretch out. Also, the sizes of the boxes and the text alignment get kinda crooked just like what I typed in.

degamer106
Junior Poster
131 posts since Mar 2006
Reputation Points: 10
Solved Threads: 0
 

Those are the things you have to consider when choosing which layouts to use. Each has it's own purpose and constraints. What you are describing above would be easily tamed with a GridBagLayout, which is highly flexible but trickier to get used to. Here's an example

JLabel lblDay = new JLabel("Day");
        JLabel lblWeek = new JLabel("Week");
        JLabel lblMonth = new JLabel("Month");

        Dimension comboDimension = new Dimension(120, 18);
        JComboBox cboDay = new JComboBox();
        cboDay.setPreferredSize(comboDimension);
        JComboBox cboWeek = new JComboBox();
        cboWeek.setPreferredSize(comboDimension);
        JComboBox cboMonth = new JComboBox();
        cboMonth.setPreferredSize(comboDimension);

        JButton leftButton = new JButton("<<");
        JButton rightButton = new JButton(">>");

        JPanel topButtonPanel = new JPanel();
        topButtonPanel.setLayout(new GridBagLayout());
        GridBagConstraints constraints = new GridBagConstraints();
        constraints.gridx = 0;
        constraints.anchor = GridBagConstraints.EAST;
        constraints.insets = new Insets(2, 4, 2, 4);
        topButtonPanel.add(lblDay, constraints);
        topButtonPanel.add(lblWeek, constraints);
        topButtonPanel.add(lblMonth, constraints);

        constraints.gridx = 1;
        constraints.anchor = GridBagConstraints.WEST;
        constraints.insets = new Insets(2, 4, 2, 4);
        topButtonPanel.add(cboDay, constraints);
        topButtonPanel.add(cboWeek, constraints);
        topButtonPanel.add(cboMonth, constraints);

        JPanel bottomButtonPanel = new JPanel();
        bottomButtonPanel.add(leftButton);
        bottomButtonPanel.add(rightButton);

        JPanel buttonPanel = new JPanel();
        buttonPanel.setLayout(new BorderLayout());
        buttonPanel.add(topButtonPanel, BorderLayout.NORTH);
        buttonPanel.add(bottomButtonPanel, BorderLayout.SOUTH);

        JFrame frame = new JFrame();

        frame.add(buttonPanel);

        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setSize(FRAME_WIDTH, FRAME_HEIGHT);
        frame.setVisible(true);
Ezzaral
Posting Genius
Moderator
15,985 posts since May 2007
Reputation Points: 3,250
Solved Threads: 847
 

BoxLayout will stretch to the size of the largest component in it.
All layout managers generally behave like that (or similar).

jwenting
duckman
Team Colleague
8,392 posts since Nov 2004
Reputation Points: 1,662
Solved Threads: 337
 

This article has been dead for over three months

Post: Markdown Syntax: Formatting Help
You