Can't render background color in JComboBox, after selection.

I don't understand why this problem is so difficult. It is driving me nuts.
If I create a renderer for a Jcombobox, it will correctly render TEXT and COLOR if it is displaying it in the pop up list, but as soon as it has been selected, the combo will render TEXT correctly but will ignore COLOR. I have tried playing around with opacity and with the editors, but it just seems as if JComboBox renderers do not work 100% as expected.
Here is my text code. All I want is for the color of X or Y to be reflected in the selected element :
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Component;
import javax.swing.DefaultListCellRenderer;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JList;
import javax.swing.JPanel;
public class CustomComboBoxDemo extends JPanel {
    public CustomComboBoxDemo() {
        super(new BorderLayout());
        String[] s = { "1", "2", "3", "4", "5" };
        JComboBox c = new JComboBox(s);
        JComboBox c2 = new JComboBox(s);
        c.setOpaque(true);
        ComboBoxRenderer renderer= new ComboBoxRenderer();
        c.setRenderer(renderer);
        c2.setRenderer(renderer);
        JPanel p = new JPanel();
        p.add(c);
        p.add(c2);
        add(p);
    class ComboBoxRenderer extends DefaultListCellRenderer {
        public ComboBoxRenderer() {
            setOpaque(true);
        public Component getListCellRendererComponent(
                                           JList list,
                                           Object value,
                                           int index,
                                           boolean isSelected,
                                           boolean cellHasFocus) {
             JLabel c = (JLabel)super.getListCellRendererComponent(list, value, index, isSelected, cellHasFocus);
             if (value.toString().equals("3")) {
                 c.setText("X");
                 c.setBackground(Color.RED);
             } else {
                 c.setText("Y");
                 c.setBackground(Color.GREEN);
             return this;
    public static void main(String[] args) {
        javax.swing.SwingUtilities.invokeLater(new Runnable() {
            public void run() {
                createAndShowGUI();
    private static void createAndShowGUI() {
        //Create and set up the window.
        JFrame frame = new JFrame("CustomComboBoxDemo");
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setContentPane(new CustomComboBoxDemo());
        //Display the window.
        frame.pack();
        frame.setVisible(true);
}

I think the problem has to do with the Alloy look and feel. If I use the Systsem look and feel then the setBackground() method does not colour in the button (just the textfield)
This has fixed my initial problem :
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Component;
import javax.swing.DefaultListCellRenderer;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JList;
import javax.swing.JPanel;
import javax.swing.UIManager;
import javax.swing.plaf.basic.BasicComboBoxEditor;
public class CustomComboBoxDemo extends JPanel {
    public CustomComboBoxDemo() {
        super(new BorderLayout());
          try {
               UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
          } catch (Exception e) {
               e.printStackTrace();
        String[] s = { "1", "2", "3", "4", "5" };
        JComboBox c = new JComboBox(s);
        JComboBox c2 = new JComboBox(s);
//        c.setRenderer(aRenderer)
//        c.getEditor().getEditorComponent().setBackground(Color.GREEN);
        c.setBackground(Color.pink);
        c2.setBackground(Color.pink);
//        c2.setBackground(Color.black);
        c.setOpaque(true);
        c2.setOpaque(false);
        ComboBoxRenderer renderer= new ComboBoxRenderer();
        c.setRenderer(renderer);
        c2.setRenderer(renderer);
        JPanel p = new JPanel();
        p.add(c);
        p.add(c2);
        add(p);
    private class MyComboBoxEditor extends BasicComboBoxEditor{
         public Component getEditorComponent(){
              Component comp = super.getEditorComponent();
              comp.setBackground(Color.red);
              return comp;
    class ComboBoxRenderer extends DefaultListCellRenderer {
        public ComboBoxRenderer() {
            setOpaque(true);
        public Component getListCellRendererComponent(
                                           JList list,
                                           Object value,
                                           int index,
                                           boolean isSelected,
                                           boolean cellHasFocus) {
             JLabel c = (JLabel)super.getListCellRendererComponent(list, value, index, isSelected, cellHasFocus);
//             setForeground(Color.green);
//             setBackground(Color.black);
//             list.setForeground(Color.green);
//             list.setBackground(Color.black);
             if (value.toString().equals("3")) {
                 c.setText("X");
                 c.setBackground(Color.RED);
             } else {
                 c.setText("Y");
                 c.setBackground(Color.GREEN);
             return this;
    public static void main(String[] args) {
        javax.swing.SwingUtilities.invokeLater(new Runnable() {
            public void run() {
                createAndShowGUI();
    private static void createAndShowGUI() {
        //Create and set up the window.
        JFrame frame = new JFrame("CustomComboBoxDemo");
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setContentPane(new CustomComboBoxDemo());
        //Display the window.
        frame.pack();
        frame.setVisible(true);
}

Similar Messages

  • [SOLVED]Can't set background color for Knotes

    Today I'm testing KDE 4.9.5 and the app Knotes start well, can add new notes, save them, etc, but it is not changing the background color of the notes...
    Here's the app log:
    ╒══[ /home/jairo ]══════╕
    └> knotes
    QDBusConnection: session D-Bus connection created before QCoreApplication. Application may misbehave.
    QDBusConnection: session D-Bus connection created before QCoreApplication. Application may misbehave.
    Fontconfig warning: "/etc/fonts/conf.d/50-user.conf", line 9: reading configurations from ~/.fonts.conf is deprecated.
    knotes(5896) ResourceLocal::ResourceLocal: ResourceLocal::ResourceLocal()
    knotes(5896) KNotesResourceManager::load: "Opening resource Notes"
    knotes(5896) KNotesLegacy::convertKNotes1Config: The file " "/home/jairo/.kde4/share/apps/knotes/notes/libkcal-122400669.545" " lacks version information but is not a valid KNotes 1 config file either!
    ╒══[ /home/jairo ]══════╕
    └> knotes(5896)/kdecore (KLibrary) kde4Factory: The library "/usr/lib/kde4/kcm_knote.so" does not offer a qt_plugin_instance function.
    knotes(5896)/kdecore (KLibrary) kde3Factory: The library "/usr/lib/kde4/kcm_knote.so" does not offer an "init_kcm_knote" function.
    knotes(5896)/kdecore (KLibrary) kde4Factory: The library "/usr/lib/kde4/kcm_knote.so" does not offer a qt_plugin_instance function.
    knotes(5896)/kdecore (KLibrary) kde3Factory: The library "/usr/lib/kde4/kcm_knote.so" does not offer an "init_kcm_knote" function.
    knotes(5896)/kdecore (KLibrary) kde4Factory: The library "/usr/lib/kde4/kcm_knote.so" does not offer a qt_plugin_instance function.
    knotes(5896)/kdecore (KLibrary) kde3Factory: The library "/usr/lib/kde4/kcm_knote.so" does not offer an "init_kcm_knote" function.
    knotes(5896)/kdecore (KLibrary) kde4Factory: The library "/usr/lib/kde4/kcm_knote.so" does not offer a qt_plugin_instance function.
    knotes(5896)/kdecore (KLibrary) kde3Factory: The library "/usr/lib/kde4/kcm_knote.so" does not offer an "init_kcm_knote" function.
    knotes(5896)/kdecore (KLibrary) kde4Factory: The library "/usr/lib/kde4/kcm_knote.so" does not offer a qt_plugin_instance function.
    knotes(5896)/kdecore (KLibrary) kde3Factory: The library "/usr/lib/kde4/kcm_knote.so" does not offer an "init_kcm_knote" function.
    knotes(5896)/kdecore (KLibrary) kde4Factory: The library "/usr/lib/kde4/kcm_knote.so" does not offer a qt_plugin_instance function.
    knotes(5896)/kdecore (KLibrary) kde3Factory: The library "/usr/lib/kde4/kcm_knote.so" does not offer an "init_kcm_knote" function.
    knotes(5896)/kdecore (KLibrary) kde4Factory: The library "/usr/lib/kde4/kcm_knote.so" does not offer a qt_plugin_instance function.
    knotes(5896)/kdecore (KLibrary) kde3Factory: The library "/usr/lib/kde4/kcm_knote.so" does not offer an "init_kcm_knote" function.
    knotes(5896)/kdecore (KLibrary) kde4Factory: The library "/usr/lib/kde4/kcm_knote.so" does not offer a qt_plugin_instance function.
    knotes(5896)/kdecore (KLibrary) kde3Factory: The library "/usr/lib/kde4/kcm_knote.so" does not offer an "init_kcm_knote" function.
    knotes(5896)/kdecore (KLibrary) kde4Factory: The library "/usr/lib/kde4/kcm_knote.so" does not offer a qt_plugin_instance function.
    knotes(5896)/kdecore (KLibrary) kde3Factory: The library "/usr/lib/kde4/kcm_knote.so" does not offer an "init_kcm_knote" function.
    knotes(5896)/kdecore (KLibrary) kde4Factory: The library "/usr/lib/kde4/kcm_knote.so" does not offer a qt_plugin_instance function.
    knotes(5896)/kdecore (KLibrary) kde3Factory: The library "/usr/lib/kde4/kcm_knote.so" does not offer an "init_kcm_knote" function.
    I'm googling  but can not find anything concrete
    Any help?
    Thank in advance
    EDIT:
    It was my mistake, the truth is that this application can change the background color, but does so in a cumbersome way.
    Each note can change the background color (individually) and to make it globally, you must remove all existing notes and the new notes then take the chosen color :S ...anyway.... issue solved
    Last edited by JohnnyDeacon (2013-02-01 16:02:47)

    bmishoe wrote:But all to no avail...I can click on the control and it changes from red to green, like I originally defined in the control.  Why can't I programatically change the color?
    It is probably more likely that your imported pictures are just covering the background of the button.
    A picture ring is the better way to go anyways.
    There are only two ways to tell somebody thanks: Kudos and Marked Solutions
    Unofficial Forum Rules and Guidelines

  • How can a text background color be saved?

    How can a text background color be saved in TextEdit?

    Care to share which OS you are using?

  • How can i set background color of child window in jdk 1.6

    Hi friends i hv devoloped simple java app using javax.swing.JFrame in which on click button event i open new child window using JFrame to draw some image.
    i set childs background color as setBackground(Color.lightGray);
    but the problem is,child window takse its background color same as its parent window.(i.e. main window's color or desktop ).i get this problem in jdk 1.6.but it is working fine in jdk 1.5.
    how can i set background color of child window in jdk 1.6 .
    plz solve my problem.
    thanks in advanced.

    Mo,
    Call me old fassioned, but I simply refuse to demangle SMS speak.
    You've got a whole keyboard, so learn how to use it.
    Keith.

  • How can i control background colors with items (registry.dat)

    Hello Evry one.....
    plzzzzzzzzz help.
    how can i control background colors with display items
    in registry.dat
    like app.ui.requiredFieldvABGCOLOR=255.0.0
    how can i control background colors with others items ????
    thanks.

    The iPhone has a "remote" app that lets you control Keynote on a Mac, but not on another iOS device (iPhone/iPad/iPod Touch).
    There are some apps that let you control limited content on another iOS device - but the content has to be within the app. You might check out SyncPad - perhaps you can import the presentation into it and then control it on the iPad.

  • How can I edit imported audio track? After selecting and deleting region and joining remaining parts, it keeps playing the track with the deleted part! What am I doing wrong?

    How can I edit imported audio track? After selecting and deleting region, and joining remaining parts, it keeps playing the track with the deleted region!
    What am I doing wrong?

    After selecting and deleting region, and joining remaining parts, it keeps playing the track with the deleted region!
    What am I doing wrong?
    How exactly are you doing this?
    When I cut out a part of an audio region, then drag the remaining parts together, so they are touching, the track will skip the cut out section when playing the file.  Also, if I select the remaining parts of the region and use the command "Edit > join regions". This will create a new audio file with the cut out part deleted.
    You should see this prompt, when you are joining the regions:

  • LV Bug? - Blink Background color only red after build

    It seems no matter what I choose for a Tools/Options/Environment/Blink Background color during debug (which works perfectly), the color will always revert back to the default of red after I build the application. I realize I can choose the colors but I would like to know why the blink color reverts back to red when the application is built. I am running LV2011 but I have noticed this in many of the previous versions as well. (I have tried changing the color from the project level as well as the debug level.)
    Solved!
    Go to Solution.

    j.masse wrote:
    To smercurio_fc - correct me if I am wrong but there are many things in the Tools\Options catagories that are built into the executable either directly or through the .ini file. It appears to me that the blinking colors are the only thing that doesn't make it into the build or the .ini file.
    That would be incorrect. There are LOTS of things that are in the labview.ini file that are not in the application's default .ini file. Most (if not all) of the Tools->Options settings are saved in the labview.ini file.
    Also, what is this 'build spec' that I may specify the color?
    I was referring to the "Advanced" page where you can specify a custom .ini file to include. Please read the documentation on the application builder.

  • How can I correct background color and graphics in Firefox?

    For some reason some background color images and fonts do not appear in Firefox. Other browsers are mostly fine. I've tried most common fixes to no avail. I'm running Windows 7 Ultimate.
    I've attached two screenshots. One is Firefox viewing Google. The other one may be relevant because I can't change the color of fonts in Word and other Office docs.
    Anyway, I'm stumped on the Firefox side. The browser has been uninstalled, re installed, and refreshed.

    Make sure that you haven't enabled a High Contrast theme in the Windows/Mac Accessibility settings.
    *http://windows.microsoft.com/en-us/windows7/turn-on-high-contrast
    Make sure that you allow pages to choose their own colors.
    *Tools > Options > Content : Fonts & Colors > Colors : [X] "Allow pages to choose their own colors, instead of my selections above"
    Note that these settings affect background images.
    See also:
    *http://kb.mozillazine.org/Website_colors_are_wrong

  • Pages 5 can't change background color in fullscreen

    There's no option to change background color when in fullscreen mode anymore.
    Did I miss something or did apple just...get rid of it?????why???

    Hi, Mark,
    Under the Format menu select Advanced and the click on Make Master Objects Selectable. Now click on the red area. You'll notice little faint x's at the corners. In the Arrange menu click on Unlock. Now you can edit the color. (In this case a gradient.) When you finish, go back and Lock it again and deselect Make Master Object Selectable.
    Walt

  • Background colors became different after deployment

    I wrote a Swing app which contains a bunch of JPanels. I set the background color of them to darkGray. Everything worked fine while I was working on it and running it in JDeveloper. But after I deployed it using a jar, the colors of the JPanels turned from darkGray to lightGray. Also I had set the row height of the JTables to a specific integer and that formatting dissapeared after deployment as well. Anyone have any ideas why this happened.
    Thanks in advance.

    Could be that you are using different versions of java in you development and deployment environments

  • Numbers for iPad.Can i change background color?

    When using spreadsheets in numbers for iPad,can I change the background color of the screen while leaving the table white.I want to have a few tables on one spreadsheet and leave the the tables white.Can I do this and change the screen color surrounding the table?

    (Numbers '09 answer, Numbers for iOS procedure may be similar.)
    In Numbers '09 the Sheet is white (period), You can, however, place a coloured shape on the Sheet, and apply your desired fill colou to that shap, then ruse the Arrange menu to Move it to the Back, then resize it and position it as needed to fill the background.
    Regards,
    Barry

  • Can you change background color in new itunes

    can you change the background color in the new itunes

    It's a big no. It has less customizable views options than the previous versions. In my opinion, it's a change for the worst.

  • How can I add background color to NSView?

    In NSView how can I add a background color?

    A Google search for nsview background color turned up many results, including the following Stack Overflow questions:
    Setting the background color of an NSView
    Best way to change the background color for an NSView

  • How can I change background color for a text field

    Hi I tried this
    style="BACKGROUND-COLOR:#00FFFF"
    working fine in IE
    but not working in Netscape.. any other way to set the background color?

    Netscape 4.x does not support this feature. Only Netscape 6.x does.

  • Menu background color has changed after applying Forms Patch3 (10.1.2.3.0)

    Hi,
    we have applied Patch3 on our Forms Environment.
    Now the menu background color has changed from green to gray (colorScheme is "teal" in formsweb.cfg).
    This behavior isn't documented and our customer want's to get the green menubar back...
    Any hints?
    Greetings, Florian.

    removed because of obsoleteness...
    Edited by: W1zard on Feb 2, 2009 4:57 PM

Maybe you are looking for

  • Request timed out when purchasing music. Fresh install of Win7.

    For the past couple months I have have not been able to purchase music off iTunes without first going into Add/Remove programs and doing a "Repair" to iTunes. Windows Firewall wasn't blocking anything from Apple and even with the firewall totally dis

  • File Recovery

    How can I repair the file bthusb from the recovery partition (Windows 7)?

  • Jrockit crashes due to outofmemory and illegal memory acces

    Hello, We have been using jrmc-3.1.2-1.6.0 and lately we are seeing JVM crashes after every couple days. Note: We have been seeing these crashes recently , for last 1 year we did not see such crashes. Following are different issues that we have seen

  • How t o get  the sunject in reciever mail using mail adapter

    hi all. i had created a file to mail scenario . in archive mapping program it was defined to which mailids of reciever side should be mailed and the body of the mail. but i just want to know if there are any settings under reciever mail adapter to ge

  • Lenovo Ideapad s205 ACPI Problem?

    I have installed Arch linux in to subject notebook. Wile installing I skip efi support and boot with syslinux (not grub). This is first time I meet motherboard with efi support, so I'm not quite sure I have to reinstall distro. My installation works