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

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?

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.

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.

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

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

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.