Results 1 to 5 of 5
  1. #1
    LittleRave is offline Member
    Join Date
    Dec 2009
    Location
    germany
    Posts
    15
    Rep Power
    0

    Default changing the border of a JComboBox

    I'm not talking about the normal border of the combo box but about the border of the selection menu. When I click at the JComboBox, a JList pops up like a drop down selection menu. But it is always surrounded by a thin black line border. Is it possible to change the color of the border? Or even remove it?
    Changing any setting of the JList or the JComboBox didn't do anything concerning that issue, didn't find any setting in the cell renderer either. I don't even know where that border comes from ... I know it's not coming from the JList, when I add one to my panel it has no border, I know though that the combo box overrides some color settings of the list so I guess it's something originating from the combo box doing some secret setting that is not to be found by anyone ... or does anybody know?

  2. #2
    zweibieren is offline Senior Member
    Join Date
    Aug 2009
    Location
    Pittsburgh, PA
    Posts
    284
    Rep Power
    6

    Default

    To change the user interface, you have to change the UI. (Yeah sure, but how?)

    I adapted an earlier JComboBox demo to show how. It is attached.

    Only two steps were needed.

    step 1. Define class MyComboBoxUI:

    Java Code:
    	//// to set border of popup
    	static class MyComboBoxUI extends WindowsComboBoxUI {
    		protected ComboPopup createPopup() {
    	        BasicComboPopup bcp = (BasicComboPopup) super.createPopup();
    
    			// set the border around the popup
    			bcp.setBorder(BorderFactory.createLineBorder(Color.red, 3));
    
    			//there is an inner border around the list, insdie the scroller
    			//    it can be set thus:
    			// JList list = bcp.getList();
    			// list.setBorder(BorderFactory.createLineBorder(Color.green, 2));
    
    			return bcp;
    		}
    	}


    step 2. Use MyComboBoxUI. Add one line after constructing the JComboBox:

    Java Code:
            // Create the combo box.
            JComboBox colorList = new JComboBox();
    	colorList.setUI(new MyComboBoxUI());   //// to set border of popup

    There is a big problem: We are overriding the ComboBox for the Windows look-and-feel.
    And we are installing that look-and-feel for this combobox no matter what platform the application is running on.
    The point of look-and-feel is to have an interface that is consistent across all applications.
    Changing the popup border means that your application is different.
    This is not a good thing for the user.

    As far as I can see, there is no way to change the popup border
    so that it changes for all look-and-feels.
    Attached Files Attached Files
    Last edited by zweibieren; 01-12-2010 at 02:52 AM.

  3. #3
    LittleRave is offline Member
    Join Date
    Dec 2009
    Location
    germany
    Posts
    15
    Rep Power
    0

    Default

    Well, I'm using the metal L&F which works on windows as well ...
    I've also written an own Theme for the metal L&F and best would be if I was able to set the border color within the theme so its always automatically set when I add a combo box anywhere.
    But anyways, thanks for the response. Guess I have to set a new UI in the theme, had hoped it was just a single value.
    Last edited by LittleRave; 01-15-2010 at 11:40 AM.

  4. #4
    zweibieren is offline Senior Member
    Join Date
    Aug 2009
    Location
    Pittsburgh, PA
    Posts
    284
    Rep Power
    6

    Default

    As you have probably guessed, you can apply the above code
    to the Metal theme just by changing "WindowsComboBoxUI" to "MetalComboBoxUI".
    Making your own offshoot theme probably requires much more effort.

    It's hard to see the upside of making that effort.
    But then, I'm one to talk. It's hard to see the upside of posting responses here.
    Not much more than the satisfaction of having helped.
    Thank you for giving me that satisfaction.

  5. #5
    LittleRave is offline Member
    Join Date
    Dec 2009
    Location
    germany
    Posts
    15
    Rep Power
    0

    Default

    Oh well, I've figured out, that one can set the UI as a default value in the UIDefaults table within the customEntriesToTable function of the theme.
    The Problem is, that not any values you add there take effect, or take a wrong effect, e.g. changing the background color of a disabled JTextField, there's three different background colors you can change, at least I've read so, the normal which takes effect when enabled or disabled, as long as there's no other background set, the disabled background which takes always effect, no matter if the field is enabled or disabled :mad: and which overrides the normal background which kinda seems like a bug to me, and the inactive background which actually does nothing, so I'm not sure if that value even exists ...
    Well, I might play with that a little more, when I have time for it

Similar Threads

  1. Replies: 2
    Last Post: 12-10-2009, 01:58 PM
  2. how can create border
    By yuen in forum AWT / Swing
    Replies: 6
    Last Post: 11-08-2009, 11:42 AM
  3. Border with LestCellRenderer
    By Gajesh Tripathi in forum AWT / Swing
    Replies: 5
    Last Post: 06-02-2009, 06:59 AM
  4. [SOLVED] Changing a JButton's border
    By a_elalaily in forum AWT / Swing
    Replies: 5
    Last Post: 05-11-2009, 12:07 AM
  5. How to remove border in JComboBox
    By tiro in forum AWT / Swing
    Replies: 2
    Last Post: 03-08-2009, 05:45 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •