Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8215187
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 7, 20262026-06-07T11:42:01+00:00 2026-06-07T11:42:01+00:00

I’m using a custom BasicComboBoxRenderer for a JComboBox and I’ve changed the appearance of

  • 0

I’m using a custom BasicComboBoxRenderer for a JComboBox and I’ve changed the appearance of the items of the drop-down list. However these changes also apply to the single top item that shows in the combobox (don’t know how to call it).

I want the top item to be independent of the other items in the list, if possible. I would also like to get rid of the top item’s blue color when it is focused (setFocusable(false) is not what I want).

I’ve tried to use the “renderer index” (-1) to affect the top item but it doesn’t seem to help.

Any ideas?

P.S Unfortunately I couldn’t add images to be more clear (no reputation).

EDIT: When I say that I want the top item to be independent from all the other items of the drop-down list I mean to always look different from the rest of them. For example in my custom BasicComboBoxRenderer I’ve set the selected item to have a different background, but this background also applies to the top item (since the selected item becomes the top item of the combobox).

EDIT 2: top item = I meant the combobox display area, so I want to affect the item that is shown at the display area and not the first item in the drop-down list. I managed to do this by using setBackground on the combobox itself AND setFocusable(false) (which is not very helpful because I want to keep the focus mechanism). But the problem is (except the focus issue) that if for example I set a border on each item in the list through a custom BasicComboBoxRenderer or ListCellRenderer class, this same border appears on the item that is shown in the display area. So there are 2 questions here:

–Is there any way to differentiate the layout of the items in the drop-down list and the single item in the display area?

–Is there any way to disable the focus color of the combobox without disabling the focus mechanism, just like when we use setFocusPainted(false) on buttons? (I’ve also tried to add a custom FocusListener on the combobox but any change made of the background through focusGained() affects only the button and not the item shown in the display area).

Sorry for the confusion and the multiple edits…

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-07T11:42:02+00:00Added an answer on June 7, 2026 at 11:42 am
    • have look at Combo Box Prompt by @camickr,

    • defined prompt can’t returns any value from JComboBox.getSelectedXxx

    EDIT

    BasicComboBoxRenderer or ListCellRenderer can do that this way

    import java.awt.*;
    import javax.swing.*;
    
    public class TestHighLightRow {
    
        public void makeUI() {
            Object[] data = {"One", "Two", "Three"};
            JComboBox comboBox = new JComboBox(data);
            comboBox.setPreferredSize(comboBox.getPreferredSize());
            comboBox.setRenderer(new HighLightRowRenderer(comboBox.getRenderer()));
            JFrame frame = new JFrame();
            frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
            frame.getContentPane().add(comboBox);
            frame.pack();
            frame.setLocationRelativeTo(null);
            frame.setVisible(true);
        }
    
        public static void main(String[] args) {
            SwingUtilities.invokeLater(new Runnable() {
    
                @Override
                public void run() {
                    new TestHighLightRow().makeUI();
                }
            });
        }
    
        public class HighLightRowRenderer implements ListCellRenderer {
    
            private final ListCellRenderer delegate;
            private int height = -1;
    
            public HighLightRowRenderer(ListCellRenderer delegate) {
                this.delegate = delegate;
            }
    
            @Override
            public Component getListCellRendererComponent(JList list, Object value, int index, boolean isSelected, boolean cellHasFocus) {
                Component component = delegate.getListCellRendererComponent(list, value, index, isSelected, cellHasFocus);
                Dimension size = component.getPreferredSize();
                if (index == 0) {
                    component.setBackground(Color.red);
                    if (component instanceof JLabel) {
                        ((JLabel) component).setHorizontalTextPosition(JLabel.CENTER);
                    }
                }
                return component;
            }
        }
    }
    

    EDIT2

    JComboBox has two states

    • editable

    • non_editable

    basically all values could be accesible from UIManager, shortcuts

    import java.awt.*;
    import java.util.Vector;
    import javax.swing.*;
    import javax.swing.UIManager;
    import javax.swing.plaf.ColorUIResource;
    import javax.swing.plaf.metal.MetalComboBoxButton;
    
    public class MyComboBox {
    
        private Vector<String> listSomeString = new Vector<String>();
        private JComboBox someComboBox = new JComboBox(listSomeString);
        private JComboBox editableComboBox = new JComboBox(listSomeString);
        private JComboBox non_EditableComboBox = new JComboBox(listSomeString);
        private JFrame frame;
    
        public MyComboBox() {
            listSomeString.add("-");
            listSomeString.add("Snowboarding");
            listSomeString.add("Rowing");
            listSomeString.add("Knitting");
            listSomeString.add("Speed reading");
    //
            someComboBox.setPrototypeDisplayValue("xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx");
            someComboBox.setFont(new Font("Serif", Font.BOLD, 16));
            someComboBox.setEditable(true);
            someComboBox.getEditor().getEditorComponent().setBackground(Color.YELLOW);
            ((JTextField) someComboBox.getEditor().getEditorComponent()).setBackground(Color.YELLOW);
    //
            editableComboBox.setPrototypeDisplayValue("xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx");
            editableComboBox.setFont(new Font("Serif", Font.BOLD, 16));
            editableComboBox.setEditable(true);
            JTextField text = ((JTextField) editableComboBox.getEditor().getEditorComponent());
            text.setBackground(Color.YELLOW);
            JComboBox coloredArrowsCombo = editableComboBox;
            Component[] comp = coloredArrowsCombo.getComponents();
            for (int i = 0; i < comp.length; i++) {
                if (comp[i] instanceof MetalComboBoxButton) {
                    MetalComboBoxButton coloredArrowsButton = (MetalComboBoxButton) comp[i];
                    coloredArrowsButton.setBackground(null);
                    break;
                }
            }
    //
            non_EditableComboBox.setPrototypeDisplayValue("xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx");
            non_EditableComboBox.setFont(new Font("Serif", Font.BOLD, 16));
    //
            frame = new JFrame();
            frame.setLayout(new GridLayout(0, 1, 10, 10));
            frame.add(someComboBox);
            frame.add(editableComboBox);
            frame.add(non_EditableComboBox);
            frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            frame.setLocation(100, 100);
            frame.pack();
            frame.setVisible(true);
        }
    
        public static void main(String[] args) {
            UIManager.put("ComboBox.background", new ColorUIResource(Color.yellow));
            UIManager.put("JTextField.background", new ColorUIResource(Color.yellow));
            UIManager.put("ComboBox.selectionBackground", new ColorUIResource(Color.magenta));
            UIManager.put("ComboBox.selectionForeground", new ColorUIResource(Color.blue));
            SwingUtilities.invokeLater(new Runnable() {
    
                @Override
                public void run() {
                    MyComboBox aCTF = new MyComboBox();
                }
            });
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm new to using the Perl treebuilder module for HTML parsing and can't figure
That's pretty much it. I'm using Nokogiri to scrape a web page what has
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I am reading a book about Javascript and jQuery and using one of the
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I'm using v2.0 of ClassTextile.php, with the following call: $testimonial_text = $textile->TextileRestricted($_POST['testimonial']); ... and
We're building an app, our first using Rails 3, and we're having to build
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
We are using XSLT to translate a RIXML file to XML. Our RIXML contains
I have thousands of HTML files to process using Groovy/Java and I need to

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.