Ezzaral
Posting Genius
15,985 posts since May 2007
Reputation Points: 3,250
Solved Threads: 847
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
15,985 posts since May 2007
Reputation Points: 3,250
Solved Threads: 847
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
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
8,392 posts since Nov 2004
Reputation Points: 1,662
Solved Threads: 337