Results 1 to 15 of 15
  1. #1
    RichersooN is offline Senior Member
    Join Date
    Jan 2011
    Location
    Rizal Province, Philippiines
    Posts
    167
    Rep Power
    0

    Default how can i change color of disabled jcombox

    Hi is there any way to change default color when JComboBox is setEnabled false?


    I used Nimbus Look and feel.. It seems that it doesnt fit my design the default color of grey when it is disabled... how can I change the default color of disabled JComboBox?

  2. #2
    ra4king's Avatar
    ra4king is offline Senior Member
    Join Date
    Apr 2011
    Location
    Atlanta, Georgia, US
    Posts
    396
    Rep Power
    4

    Default

    Try setBackground(Color). If that doesn't work, then you can't :/

  3. #3
    RichersooN is offline Senior Member
    Join Date
    Jan 2011
    Location
    Rizal Province, Philippiines
    Posts
    167
    Rep Power
    0

    Default

    Quote Originally Posted by ra4king View Post
    Try setBackground(Color). If that doesn't work, then you can't :/
    setBackground(Color) didn't work.. It's just a simple color manipulation, why java can't:mad:

  4. #4
    ra4king's Avatar
    ra4king is offline Senior Member
    Join Date
    Apr 2011
    Location
    Atlanta, Georgia, US
    Posts
    396
    Rep Power
    4

    Default

    Java can but that requires you to specify your own look and feel in Swing. If you really need to change this color, I suggest subclassing the appropriate Nimbus ComboBoxUI implementation and using your own painting. But I have never done this so I'm not sure which class exactly to subclass and what methods to use and override. :/

  5. #5
    RichersooN is offline Senior Member
    Join Date
    Jan 2011
    Location
    Rizal Province, Philippiines
    Posts
    167
    Rep Power
    0

    Default

    Do you know how to override the JComboBox when it is click it doesn't anything
    Last edited by RichersooN; 04-24-2011 at 10:45 AM.

  6. #6
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    Have you tried using the UIManager to change the default colors? Something like:

    Java Code:
    Color newColor = new Color(250, 50, 50);  // or whatever color you want
    UIManager.put("ComboBox.disabledBackground", newColor);

    For more on this, please see this section of Rob Camick's excellent blog: uimanager-defaults

  7. #7
    ra4king's Avatar
    ra4king is offline Senior Member
    Join Date
    Apr 2011
    Location
    Atlanta, Georgia, US
    Posts
    396
    Rep Power
    4

    Default

    @Fubarable
    That's pretty useful.

  8. #8
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,680
    Blog Entries
    7
    Rep Power
    21

    Default

    Did you try this method: public void enableInputMethods(boolean enable)? It's a method in the Component class (A JComboBox extends from that class).

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  9. #9
    ra4king's Avatar
    ra4king is offline Senior Member
    Join Date
    Apr 2011
    Location
    Atlanta, Georgia, US
    Posts
    396
    Rep Power
    4

    Default

    How would that allow the changing of the color of JComboBox?

  10. #10
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,680
    Blog Entries
    7
    Rep Power
    21

    Default

    Quote Originally Posted by ra4king View Post
    How would that allow the changing of the color of JComboBox?
    The component isn't completely disabled so you are able to set its foreground and background colours (as if it were completely enabled).

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  11. #11
    ra4king's Avatar
    ra4king is offline Senior Member
    Join Date
    Apr 2011
    Location
    Atlanta, Georgia, US
    Posts
    396
    Rep Power
    4

    Default

    Quote Originally Posted by JosAH View Post
    The component isn't completely disabled so you are able to set its foreground and background colours (as if it were completely enabled).

    kind regards,

    Jos
    It won't let you change the color if it is disabled?? :eek:

  12. #12
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,680
    Blog Entries
    7
    Rep Power
    21

    Default

    Quote Originally Posted by ra4king View Post
    It won't let you change the color if it is disabled?? :eek:
    Well, according to the OP and the L&F, nope; so my suggestion was to make the component act like it was disabled while it was still enabled (so the colours could be changed).

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  13. #13
    ra4king's Avatar
    ra4king is offline Senior Member
    Join Date
    Apr 2011
    Location
    Atlanta, Georgia, US
    Posts
    396
    Rep Power
    4

    Default

    I just played around with JComboBox; it changes the background color normally enabled and disabled.

    This works for me.
    Java Code:
    box.setBackground(Color.blue);
    box.setEnabled(false);

  14. #14
    Join Date
    May 2011
    Location
    Munich
    Posts
    15
    Rep Power
    0

    Default

    The combo is made up of a textfield (or label) and a button. You need to find out how to access the textfield inside the combo.

    Have you tried accessing the components inside the combo box? Changing the editor or renderer?

    I refuse to believe it is not possible to do without Look and Feel.

  15. #15
    lisamai is offline Member
    Join Date
    Jul 2012
    Posts
    1
    Rep Power
    0

    Default Re: how can i change color of disabled jcombox

    Quote Originally Posted by ra4king View Post
    I just played around with JComboBox; it changes the background color normally enabled and disabled.

    This works for me.
    Java Code:
    box.setBackground(Color.blue);
    box.setEnabled(false);
    this does not work and I'm suprised that it seems to be working for you, are you sure the background wasn't already blue?
    I searched many forums for an answer to this as I had no luck with standard setForeground methods etc and changing the UImanager either as .put or getDefaults().put did not work.
    the only solution I found that worked - and it's quite simple was

    ((JTextField) comboBox.getEditor().getEditorComponent()).setDisa bledTextColor(Color.#####);

    apparently this is only supposed to work for enabled boxes but it worked for my disabled boxes perfectly and can be done befoer or after the box enabled is set to false

    i can see from the time stamps that you most likely don't need or care about this anymore but thought I'd post it to make it easier for anyone else searching for a solution

Similar Threads

  1. Replies: 4
    Last Post: 03-21-2009, 06:48 PM
  2. Color Change of data
    By Java.child in forum AWT / Swing
    Replies: 20
    Last Post: 02-12-2009, 07:51 AM
  3. Text color in disabled componentes
    By saul_2110 in forum AWT / Swing
    Replies: 8
    Last Post: 12-13-2008, 06:37 AM
  4. Disabled JButton font color
    By p900128 in forum Advanced Java
    Replies: 1
    Last Post: 07-24-2008, 09:16 AM
  5. How to change the foreground color of a disabled control
    By arunkumarsimhadri in forum New To Java
    Replies: 0
    Last Post: 07-09-2008, 05:30 PM

Tags for this Thread

Posting Permissions

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