JDK-4515838 : Can't change the border of a JComboBox
  • Type: Bug
  • Component: client-libs
  • Sub-Component: javax.swing
  • Affected Version: 1.3.1
  • Priority: P4
  • Status: Closed
  • Resolution: Won't Fix
  • OS: windows_2000
  • CPU: x86
  • Submitted: 2001-10-17
  • Updated: 2011-01-26
  • Resolved: 2011-01-26
Description
Name: gm110360			Date: 10/17/2001


java version "1.3.1"
Java(TM) 2 Runtime Environment, Standard Edition (build 1.3.1-b24)
Java HotSpot(TM) Client VM (build 1.3.1-b24, mixed mode)

I add a JComboBox in a toolbar (inside a JFrame), and I'dlike to remove the border of the JComboBox. I can remove the border of the JButton of the toolbar, but I can't change the border of the JComboBox.
Setting a new border in it just adds the border to the default border of the JComboBox, but does not replace it.

This is a piece of code which reproduce the problem :

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;

public class JComboProblem extends JFrame
{

	public JComboProblem()
	{
		super();

		addWindowListener(new WindowAdapter() {
			public void windowClosing(WindowEvent ev) {
				System.exit(0);
			}
		});

		JPanel panel = new JPanel(new BorderLayout());
		getContentPane().add(panel);

		JToolBar tb = new JToolBar();
		panel.add(tb, BorderLayout.NORTH);

		JButton button = new JButton("Do nothing");
		button.setBorderPainted(false);
		tb.add(button);

		JComboBox cb = new JComboBox();

		cb.addItem("25%");
		cb.addItem("50%");
		cb.addItem("100%");
		cb.addItem("200%");

		cb.setMaximumSize(cb.getMinimumSize());
		tb.add(cb);

//		cb.setBorder(null); // Do nothing
		cb.setBorder(BorderFactory.createEmptyBorder(0, 0, 0, 0)); // Do nothing
//		cb.setBorder(BorderFactory.createEtchedBorder()); // Adds the border to the existing border but does not replace it

		setSize(400, 300);
		setVisible(true);
	}

	public static void main(String argv[])
	{
		new JComboProblem();
	}
}

I could be useful to have a JComboBox.setBorderPainted (and JComboBox.setFocusPainted) method(s).
(Review ID: 133889) 
======================================================================

Comments
CONVERTED DATA BugTraq+ Release Management Values COMMIT TO FIX: mustang
25-09-2004

WORK AROUND Name: gm110360 Date: 10/17/2001 I have found this workaround : for (int i=0; i<combo.getComponentCount(); i++) { if (combo.getComponent(i) instanceof AbstractButton) { ((AbstractButton)combo.getComponent(i)).setBorderPainted(false); } } ======================================================================
25-09-2004

EVALUATION The problem is that the the ComboBox is actually using a JButton in MetalComboBoxEditor to render the visible (non-popup) portion of the JComboBox. This JButton maintains it's own border so that JComponent.paintBorder()/paintComponent() has no awareness of the JComboBox border. The solution is to set the ComboBoxEditor border to the JComboBox border before painting or when the state changes. On a related note: MetalComboBoxButton should register itself as a PropertyChangeListener or create an inner class PCL onto JComboBox to listen for changes in colors, enabled state and borders. This doesn't seem to be a problem for windows. This seems to be a fairly low risk fix. Should try to see the effect of rollover effects in the toolbar. ###@###.### 2002-02-20
20-02-2002