I committed this patch to fix a problem with JComboBoxes that use a custom renderer.
I've updated the ComboBoxDemo class to show this working. And I added the
'List.font' default to the MetalLookAndFeel since it was missing:
2005-10-17 David Gilbert <[EMAIL PROTECTED]>
* examples/gnu/classpath/examples/swing/ComboBoxDemo.java:
(CustomCellRenderer): new inner class,
(comboState6): new field,
(combo11): new field,
(combo12): new field,
(createContent): add panel from createPanel6(),
(createPanel6): new method,
(actionPerformed): update state for new JComboBoxes,
* javax/swing/plaf/basic/BasicComboBoxUI.java
(installComponents): don't create arrowButton until after listBox is
created, set listBox to the JList created by the popup,
* javax/swing/plaf/metal/MetalComboBoxButton.java:
(MetalComboBoxButton(JComboBox, Icon, boolean, CellRendererPane,
JList)): set margins to zero,
(paintComponent): use list cell renderer to paint button content,
* javax/swing/plaf/metal/MetalLookAndFeel.java
(initComponentDefaults): add 'List.font' default.
Regards,
Dave
Index: examples/gnu/classpath/examples/swing/ComboBoxDemo.java
===================================================================
RCS file:
/cvsroot/classpath/classpath/examples/gnu/classpath/examples/swing/ComboBoxDemo.java,v
retrieving revision 1.2
diff -u -r1.2 ComboBoxDemo.java
--- examples/gnu/classpath/examples/swing/ComboBoxDemo.java 29 Sep 2005
22:15:21 -0000 1.2
+++ examples/gnu/classpath/examples/swing/ComboBoxDemo.java 17 Oct 2005
09:48:05 -0000
@@ -24,6 +24,7 @@
import java.awt.BorderLayout;
import java.awt.Color;
+import java.awt.Component;
import java.awt.Dimension;
import java.awt.Font;
import java.awt.GridLayout;
@@ -31,12 +32,16 @@
import java.awt.event.ActionListener;
import javax.swing.BorderFactory;
+import javax.swing.DefaultListCellRenderer;
+import javax.swing.Icon;
import javax.swing.JButton;
import javax.swing.JCheckBox;
import javax.swing.JComboBox;
import javax.swing.JFrame;
+import javax.swing.JList;
import javax.swing.JPanel;
import javax.swing.UIManager;
+import javax.swing.plaf.metal.MetalIconFactory;
/**
* A simple demo showing various combo boxes in different states.
@@ -46,6 +51,24 @@
implements ActionListener
{
+ class CustomCellRenderer extends DefaultListCellRenderer
+ {
+ public Component getListCellRendererComponent(JList list,
+ Object value,
+ int index,
+ boolean isSelected,
+ boolean cellHasFocus)
+ {
+ DefaultListCellRenderer result = (DefaultListCellRenderer)
+ super.getListCellRendererComponent(list, value, index, isSelected,
+ cellHasFocus);
+ Icon icon = (Icon) value;
+ result.setIcon(icon);
+ result.setText("Index = " + index);
+ return result;
+ }
+ }
+
private JCheckBox comboState1;
private JComboBox combo1;
private JComboBox combo2;
@@ -66,6 +89,10 @@
private JComboBox combo9;
private JComboBox combo10;
+ private JCheckBox comboState6;
+ private JComboBox combo11;
+ private JComboBox combo12;
+
/**
* Creates a new demo instance.
*
@@ -80,12 +107,13 @@
private JPanel createContent()
{
JPanel content = new JPanel(new BorderLayout());
- JPanel panel = new JPanel(new GridLayout(5, 1));
+ JPanel panel = new JPanel(new GridLayout(6, 1));
panel.add(createPanel1());
panel.add(createPanel2());
panel.add(createPanel3());
panel.add(createPanel4());
panel.add(createPanel5());
+ panel.add(createPanel6());
content.add(panel);
JPanel closePanel = new JPanel();
JButton closeButton = new JButton("Close");
@@ -234,6 +262,41 @@
return panel;
}
+ /**
+ * This panel contains combo boxes with a custom renderer.
+ *
+ * @return A panel.
+ */
+ private JPanel createPanel6()
+ {
+ JPanel panel = new JPanel(new BorderLayout());
+ this.comboState6 = new JCheckBox("Enabled", true);
+ this.comboState6.setActionCommand("COMBO_STATE6");
+ this.comboState6.addActionListener(this);
+ panel.add(this.comboState6, BorderLayout.EAST);
+
+ JPanel controlPanel = new JPanel();
+ controlPanel.setBorder(BorderFactory.createTitledBorder("Custom Renderer:
"));
+ this.combo11 = new JComboBox(new Object[] {
+ MetalIconFactory.getFileChooserHomeFolderIcon(),
+ MetalIconFactory.getFileChooserNewFolderIcon()});
+ this.combo11.setPreferredSize(new Dimension(100, 30));
+ this.combo11.setRenderer(new CustomCellRenderer());
+ this.combo12 = new JComboBox(new Object[] {
+ MetalIconFactory.getFileChooserHomeFolderIcon(),
+ MetalIconFactory.getFileChooserNewFolderIcon()});
+ this.combo12.setPreferredSize(new Dimension(100, 30));
+ this.combo12.setRenderer(new CustomCellRenderer());
+ this.combo12.setEditable(true);
+
+ controlPanel.add(combo11);
+ controlPanel.add(combo12);
+
+ panel.add(controlPanel);
+
+ return panel;
+ }
+
public void actionPerformed(ActionEvent e)
{
if (e.getActionCommand().equals("COMBO_STATE1"))
@@ -260,6 +323,11 @@
{
combo9.setEnabled(comboState5.isSelected());
combo10.setEnabled(comboState5.isSelected());
+ }
+ else if (e.getActionCommand().equals("COMBO_STATE6"))
+ {
+ combo11.setEnabled(comboState6.isSelected());
+ combo12.setEnabled(comboState6.isSelected());
}
else if (e.getActionCommand().equals("CLOSE"))
{
Index: javax/swing/plaf/basic/BasicComboBoxUI.java
===================================================================
RCS file:
/cvsroot/classpath/classpath/javax/swing/plaf/basic/BasicComboBoxUI.java,v
retrieving revision 1.20
diff -u -r1.20 BasicComboBoxUI.java
--- javax/swing/plaf/basic/BasicComboBoxUI.java 12 Oct 2005 12:09:59 -0000
1.20
+++ javax/swing/plaf/basic/BasicComboBoxUI.java 17 Oct 2005 09:48:16 -0000
@@ -441,25 +441,22 @@
*/
protected void installComponents()
{
- // create and install arrow button
- arrowButton = createArrowButton();
- configureArrowButton();
- comboBox.add(arrowButton);
-
- // Set list that will be used by BasicComboBoxRender
- // in order to determine the right colors when rendering
- listBox = new JList();
+ // create drop down list of items
+ popup = createPopup();
+ listBox = popup.getList();
// set editor and renderer for the combo box. Editor is used
// only if combo box becomes editable, otherwise renderer is used
// to paint the selected item; combobox is not editable by default.
comboBox.setRenderer(createRenderer());
+ // create and install arrow button
+ arrowButton = createArrowButton();
+ configureArrowButton();
+ comboBox.add(arrowButton);
+
comboBox.setEditor(createEditor());
editor = comboBox.getEditor().getEditorComponent();
-
- // create drop down list of items
- popup = createPopup();
comboBox.revalidate();
}
Index: javax/swing/plaf/metal/MetalComboBoxButton.java
===================================================================
RCS file:
/cvsroot/classpath/classpath/javax/swing/plaf/metal/MetalComboBoxButton.java,v
retrieving revision 1.4
diff -u -r1.4 MetalComboBoxButton.java
--- javax/swing/plaf/metal/MetalComboBoxButton.java 3 Oct 2005 09:45:23
-0000 1.4
+++ javax/swing/plaf/metal/MetalComboBoxButton.java 17 Oct 2005 09:48:17
-0000
@@ -38,7 +38,7 @@
package javax.swing.plaf.metal;
-import java.awt.FontMetrics;
+import java.awt.Component;
import java.awt.Graphics;
import java.awt.Insets;
import java.awt.Rectangle;
@@ -48,7 +48,6 @@
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JList;
-import javax.swing.SwingConstants;
import javax.swing.SwingUtilities;
/**
@@ -106,6 +105,7 @@
iconOnly = onlyIcon;
listBox = list;
rendererPane = pane;
+ setMargin(new Insets(0, 0, 0, 0));
}
/**
@@ -211,102 +211,30 @@
}
else
{
- String text = "";
Object selected = comboBox.getModel().getSelectedItem();
- if (selected != null)
- text = selected.toString();
+ if (selected == null)
+ selected = "";
Rectangle bounds = comboBox.getBounds();
Rectangle innerArea = SwingUtilities.calculateInnerArea(this, null);
+ Insets insets = comboBox.getInsets();
+ Rectangle renderArea = new Rectangle(innerArea.x, innerArea.y,
+ innerArea.width - comboIcon.getIconWidth() - 4, innerArea.height);
+ Component cellRenderer
+ = comboBox.getRenderer().getListCellRendererComponent(this.listBox,
+ selected, comboBox.getSelectedIndex(), false, false);
+ cellRenderer.setBackground(comboBox.getBackground());
+ cellRenderer.setEnabled(comboBox.isEnabled());
+ rendererPane.paintComponent(g, cellRenderer, this, renderArea);
if (comboBox.hasFocus())
{
g.setColor(MetalLookAndFeel.getFocusColor());
g.drawRect(innerArea.x, innerArea.y - 1, innerArea.width - 1,
innerArea.height);
}
- Insets insets = comboBox.getInsets();
int iconX = bounds.width - insets.right - comboIcon.getIconWidth() - 7;
int iconY = insets.top
+ (bounds.height - comboIcon.getIconHeight()) / 2;
comboIcon.paintIcon(comboBox, g, iconX, iconY);
- if (comboBox.isEnabled())
- g.setColor(MetalLookAndFeel.getBlack());
- else
- g.setColor(MetalLookAndFeel.getControlDisabled());
- Rectangle viewArea = new Rectangle(innerArea.x, innerArea.y,
- innerArea.width - comboIcon.getIconWidth() - 7,
- innerArea.height);
- FontMetrics fm = g.getFontMetrics(comboBox.getFont());
- Rectangle textR = new Rectangle();
- text = SwingUtilities.layoutCompoundLabel(fm, text, null,
- SwingConstants.CENTER, SwingConstants.LEFT,
- SwingConstants.CENTER, SwingConstants.RIGHT,
- viewArea, new Rectangle(), textR, 0);
- // FIXME: this truncation should be done within layoutCompoundLabel()
- text = truncateText(text,
- innerArea.width - comboIcon.getIconWidth() - 7, fm);
- int yAdj = fm.getDescent() + fm.getLeading();
- g.setFont(comboBox.getFont());
- g.drawString(text, textR.x, textR.y + textR.height - yAdj);
- }
- }
-
- /**
- * A utility method that checks the width of a string and, if necessary,
- * truncates it (adding a '...' suffix to indicate the truncation) to fit
- * within the specified width.
- *
- * FIXME: this method performs a function that needs to be incorporated into
- * the SwingUtilities.layoutCompoundLabel() code. But that method does some
- * multi-line calculations that I don't understand yet, so for now this code
- * is local.
- *
- * @param text the text.
- * @param width the available width.
- * @param fm the font metrics.
- *
- * @return The text, truncated if necessary.
- */
- private static String truncateText(String text, int width, FontMetrics fm)
- {
- if (text == null)
- return null;
- int textWidth = fm.stringWidth(text);
- if (width > 0 && width < textWidth)
- {
- int dotWidth = fm.stringWidth("...");
- int available = width - dotWidth;
- if (available > 0)
- {
- int lower = 0;
- int upper = text.length();
- while (upper > lower)
- {
- if (fm.stringWidth(text.substring(0, upper)) <= available)
- lower = upper; // we're finished
- else
- {
- if (lower == upper - 1)
- upper = lower; // we're finished
- else
- {
- int mid = lower + (upper - lower) / 2;
- if (fm.stringWidth(text.substring(0, mid)) <=
available)
- lower = mid;
- else
- upper = mid;
- }
- }
- }
- if (upper < text.length())
- text = text.substring(0, upper) + "...";
- }
- else
- {
- // there's not even enough space for the "..."
- text = "";
- }
}
- return text;
}
-
}
Index: javax/swing/plaf/metal/MetalLookAndFeel.java
===================================================================
RCS file:
/cvsroot/classpath/classpath/javax/swing/plaf/metal/MetalLookAndFeel.java,v
retrieving revision 1.65
diff -u -r1.65 MetalLookAndFeel.java
--- javax/swing/plaf/metal/MetalLookAndFeel.java 13 Oct 2005 11:19:27
-0000 1.65
+++ javax/swing/plaf/metal/MetalLookAndFeel.java 17 Oct 2005 09:48:19
-0000
@@ -890,6 +890,7 @@
"Label.font", getControlTextFont(),
"Label.foreground", getSystemTextColor(),
+ "List.font", getControlTextFont(),
"List.background", getWindowBackground(),
"List.foreground", getUserTextColor(),
"List.selectionBackground", getTextHighlightColor(),
_______________________________________________
Classpath-patches mailing list
Classpath-patches@gnu.org
http://lists.gnu.org/mailman/listinfo/classpath-patches