Skip to Main Content

Java SE (Java Platform, Standard Edition)

Announcement

For appeals, questions and feedback about Oracle Forums, please email oracle-forums-moderators_us@oracle.com. Technical questions should be asked in the appropriate category. Thank you!

JDialog sizing problems

843805Dec 13 2006 — edited Mar 10 2008
First of all, just let me say I have read everything there is available on layout managers and how pack() and validate() work.

My problem is simply that my JDialog appears far longer than its components, and I can't see why.

I've created screenshots of the desired appearance and the actual appearance:

http://www.komododave.co.uk/gallery/main.php?g2_itemId=167

The first 'undesired' screenshot shows what the JDialog looks like upon instantiation. The second shows what it looks like once you manually shrink it in the Y direction by more than about 10 pixels.

I don't understand how the maximum size can exceed the Dimension I set with 'dialog.setMaximumSize(...', since I'm using BoxLayout for the JDialog and that respects a given maximum size.

The constructor creating the JDialog is shown here:
		public AbstractSelectionDialog(Vector<String> argVector, String title,
				String message) {
			Vector<String> vector = argVector;
			// create new modal JDialog
			final JDialog dialog = new JDialog(Silk.getFrame(), title, true);
			// set boxlayout for dialog frame
			dialog.getContentPane().setLayout(new BoxLayout(dialog.getContentPane(),
					BoxLayout.Y_AXIS));
			// set default close operation
			dialog.setDefaultCloseOperation(JDialog.DISPOSE_ON_CLOSE);

			// create ButtonGroup to enforce exclusive collection choice
			final ButtonGroup buttonGroup = new ButtonGroup();
			// initialise other components
			// use fake space for label rather than use another HorizontalGlue
			JLabel label = new JLabel("   " + message);
			// panel containing collection selection
			JPanel selectionPanel = new JPanel();

			// run initialiser methods
			JPanel buttonPanel = (JPanel) createButtons(buttonGroup, dialog);
			AbstractButton selection[] = createSelection(vector, buttonGroup,
					selectionPanel);

			// scrollPane containing collection panel
			JScrollPane scrollPane = new JScrollPane(selectionPanel,
					JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED,
					JScrollPane.HORIZONTAL_SCROLLBAR_NEVER);

			// panel containing scroll pane
			JPanel scrollPanel = new JPanel();
			scrollPanel.setLayout(new BoxLayout(scrollPanel, BoxLayout.X_AXIS));
			selectionPanel.setLayout(new GridLayout(vector.size(), 1));

			// assemble component structure
			scrollPanel.add(Box.createHorizontalGlue());
			scrollPanel.add(scrollPane);
			scrollPanel.add(Box.createHorizontalGlue());
			dialog.add(label);
			dialog.add(scrollPanel);
			dialog.add(buttonPanel);

			// set all components to be left-justified
			label.setAlignmentX(0.0F);
			scrollPanel.setAlignmentX(0.0F);
			selectionPanel.setAlignmentX(0.0F);
			buttonPanel.setAlignmentX(0.0F);
			
			// set higher-level component sizes
			Silk.setSizeAttributes(label, 300, 30, true);
			selectionPanel.setMaximumSize(new Dimension(250, 400));
			scrollPane.setMinimumSize(new Dimension(250, 50));
			scrollPane.setMaximumSize(new Dimension(250, 400));
			dialog.setMinimumSize(new Dimension(300, 200));
			dialog.setMaximumSize(new Dimension(300, 500));
			dialog.pack();

			// set components to be opaque
			label.setOpaque(true);
			scrollPane.setOpaque(true);
			selectionPanel.setOpaque(true);

			// display dialog frame
			//dialog.setResizable(false);
			dialog.setVisible(true);

		}
The 'createButtons' method it uses is here:
	public JPanel createButtons(ButtonGroup bg, JDialog jd) {

			final ButtonGroup buttonGroup = bg;
			final JDialog dialog = jd;

			// confirmation buttons
			JButton okBtn = new JButton();
			JButton cancelBtn = new JButton();
			JButton newBtn = new JButton();

			// panel containing OK and CANCEL buttons
			JPanel bothPanel = new JPanel();
			// panel containing NEW COLLECTION button
			JPanel newPanel = new JPanel();
			// panel to contain all other button panels
			JPanel confPanel = new JPanel();

			confPanel.setLayout(new BoxLayout(confPanel, BoxLayout.X_AXIS));
			newPanel.setLayout(new BoxLayout(newPanel, BoxLayout.X_AXIS));
			bothPanel.setLayout(new BoxLayout(bothPanel, BoxLayout.Y_AXIS));

			// definition of button Actions
			okBtn.setAction(new AbstractAction() {
				public void actionPerformed(ActionEvent ae) {
					Actions.addScript(buttonGroup.getSelection()
							.getActionCommand(), null);
					dialog.dispose();
				}
			});
			cancelBtn.setAction(new AbstractAction() {
				public void actionPerformed(ActionEvent ae) {
					dialog.dispose();
				}
			});
			newBtn.setAction(new AbstractAction() {
				public void actionPerformed(ActionEvent ae) {
					dialog.dispose();
					new Actions.NewCollectionAction().actionPerformed(ae);
					new Actions.NewScriptAction().actionPerformed(ae);
				}
			});

			// override action-set button text
			okBtn.setText("Ok");
			cancelBtn.setText("Cancel");
			newBtn.setText("Add New Collection");

			// set button sizes
			Silk.setSizeAttributes(okBtn, 115, 30, true);
			Silk.setSizeAttributes(cancelBtn, 115, 30, true);
			Silk.setSizeAttributes(newBtn, 250, 35, true);

			// set opaque buttons
			confPanel.setOpaque(true);
			okBtn.setOpaque(true);
			cancelBtn.setOpaque(true);

			// assemble components
			confPanel.add(Box.createHorizontalGlue());
			confPanel.add(okBtn);
			confPanel.add(Box.createRigidArea(new Dimension(20, 0)));
			confPanel.add(cancelBtn);
			confPanel.add(Box.createHorizontalGlue());
			newPanel.add(Box.createHorizontalGlue());
			newPanel.add(newBtn);
			newPanel.add(Box.createHorizontalGlue());
			bothPanel.add(Box.createRigidArea(new Dimension(0, 10)));
			bothPanel.add(confPanel);
			bothPanel.add(Box.createRigidArea(new Dimension(0, 5)));
			bothPanel.add(newPanel);
			bothPanel.add(Box.createRigidArea(new Dimension(0, 10)));

			bothPanel.setMaximumSize(new Dimension(300, 100));
			return bothPanel;
		}
And the createSelection() method it uses is defined here:
	public AbstractButton[] createSelection(Vector<String> argVector,
				ButtonGroup buttonGroup, JPanel selectionPanel) {
			Vector<String> vector = argVector;
			JRadioButton selection[] = new JRadioButton[vector.size()];
			for (int i = 0; i < vector.size(); i++)
				selection[i] = new JRadioButton(vector.get(i));

			AbstractButton current;
			for (byte i = 0; i < selection.length; i++) {
				current = selection;
// set action command string to later identify button that's
// selected
current.setActionCommand(vector.get(i));
// fix button's size
Silk.setSizeAttributes(current, 250, 30, true);
// set button's colour
current.setBackground(Color.WHITE);
// add button to buttongroup
buttonGroup.add(current);
// add button to appropriate JPanel
selectionPanel.add(current);
}

// ensure one button is selected to prevent pressing OK with no
// selection
selection[0].setSelected(true);
return selection;
}


Please help if you can.

Thank you.

- Dave
Comments
Locked Post
New comments cannot be posted to this locked post.
Post Details
Locked on Apr 7 2008
Added on Dec 13 2006
16 comments
33,221 views