/** Lays out the container in the specified panel. */ public void layoutContainer(Container target) { synchronized (target.getTreeLock()) { Insets insets = target.getInsets(); Dimension dim = target.getSize(); int fullHeight = dim.height; int bottom = dim.height - insets.bottom - 1; int top = 0 + insets.top; int left = insets.left; int maxPosition = dim.width - (insets.left + insets.right) - hgap; int right = dim.width - insets.right; maxPosition = right; int height = bottom - top; int size = actions.size(); int w, h; if ((grip != null) && grip.isVisible()) { Dimension d = grip.getPreferredSize(); grip.setBounds(left, top + vgap, d.width, bottom - top - 2 * vgap); left += d.width; left += hgap; } for (int i = 0; i < size; i++) { left += hgap; Component comp = (Component) actions.elementAt(i); Dimension d; Dimension minSize = comp.getMinimumSize(); if (i == size - 1) d = comp.getMaximumSize(); else d = comp.getPreferredSize(); w = d.width; h = Math.min(height, d.height); if ((left < maxPosition) && (left + w > maxPosition)) { if (maxPosition - left >= minSize.width) w = maxPosition - left; } comp.setBounds(left, (fullHeight - d.height) / 2, w, h); // if (i == size - 1) // d = comp.getMaximumSize(); // else // d = comp.getPreferredSize(); // if ((left + d.width) > right) // w = right - left; // else // w = d.width; // h = Math.min (height, d.height); // comp.setBounds (left, (fullHeight - d.height)/2, w, h); left += d.width; } } }
/** * Creates the appropriate object to represent each of the objects in <code>buttons</code> and * adds it to <code>container</code>. This differs from addMessageComponents in that it will * recurse on <code>buttons</code> and that if button is not a Component it will create an * instance of JButton. */ protected void addButtonComponents(Container container, Object[] buttons, int initialIndex) { if (buttons != null && buttons.length > 0) { boolean sizeButtonsToSame = getSizeButtonsToSameWidth(); boolean createdAll = true; int numButtons = buttons.length; JButton[] createdButtons = null; int maxWidth = 0; if (sizeButtonsToSame) { createdButtons = new JButton[numButtons]; } for (int counter = 0; counter < numButtons; counter++) { Object button = buttons[counter]; Component newComponent; if (button instanceof Component) { createdAll = false; newComponent = (Component) button; container.add(newComponent); hasCustomComponents = true; } else { JButton aButton; if (button instanceof ButtonFactory) { aButton = ((ButtonFactory) button).createButton(); } else if (button instanceof Icon) aButton = new JButton((Icon) button); else aButton = new JButton(button.toString()); aButton.setName("OptionPane.button"); aButton.setMultiClickThreshhold( DefaultLookup.getInt(optionPane, this, "OptionPane.buttonClickThreshhold", 0)); configureButton(aButton); container.add(aButton); ActionListener buttonListener = createButtonActionListener(counter); if (buttonListener != null) { aButton.addActionListener(buttonListener); } newComponent = aButton; } if (sizeButtonsToSame && createdAll && (newComponent instanceof JButton)) { createdButtons[counter] = (JButton) newComponent; maxWidth = Math.max(maxWidth, newComponent.getMinimumSize().width); } if (counter == initialIndex) { initialFocusComponent = newComponent; if (initialFocusComponent instanceof JButton) { JButton defaultB = (JButton) initialFocusComponent; defaultB.addHierarchyListener( new HierarchyListener() { public void hierarchyChanged(HierarchyEvent e) { if ((e.getChangeFlags() & HierarchyEvent.PARENT_CHANGED) != 0) { JButton defaultButton = (JButton) e.getComponent(); JRootPane root = SwingUtilities.getRootPane(defaultButton); if (root != null) { root.setDefaultButton(defaultButton); } } } }); } } } ((ButtonAreaLayout) container.getLayout()) .setSyncAllWidths((sizeButtonsToSame && createdAll)); /* Set the padding, windows seems to use 8 if <= 2 components, otherwise 4 is used. It may actually just be the size of the buttons is always the same, not sure. */ if (DefaultLookup.getBoolean(optionPane, this, "OptionPane.setButtonMargin", true) && sizeButtonsToSame && createdAll) { JButton aButton; int padSize; padSize = (numButtons <= 2 ? 8 : 4); for (int counter = 0; counter < numButtons; counter++) { aButton = createdButtons[counter]; aButton.setMargin(new Insets(2, padSize, 2, padSize)); } } } }