Customizing an Editable JComboBox

A previous tip explained how to change the appearance of the ‘list selection’ cells of a JComboBox by setting the attributes of the JComboBox object’s ListCellRenderer.

However, the ListCellRenderer controls the item area for a non-editable JcomboBox. A ComboBoxEditor controls the item area for an editable JcomboBox (which, in turn, contains a component that handles the visual aspect of the JComboBox.) Here is how you might change the background color of the editable area of a JComboBox:

 ComboBoxEditor cbe = cb.getEditor();Component c = cbe.getEditorComponent();c.setBackground(Color.red);


When the JComboBox is not editable (which is the default) then the standard method ‘cb.setBackground(…)’ will work, but it has the side effect of also changing the background colors of the arrow button and the individual unselected cells in the selection list area. Setting the color on the default ListCellRenderer has no effect because the renderer’s colors are adjusted during repainting (remember

Share the Post:
Share on facebook
Share on twitter
Share on linkedin

Overview

The Latest

microsoft careers

Top Careers at Microsoft

Microsoft has gained its position as one of the top companies in the world, and Microsoft careers are flourishing. This multinational company is efficiently developing popular software and computers with other consumer electronics. It is a dream come true for so many people to acquire a high paid, high-prestige job

your company's audio

4 Areas of Your Company Where Your Audio Really Matters

Your company probably relies on audio more than you realize. Whether you’re creating a spoken text message to a colleague or giving a speech, you want your audio to shine. Otherwise, you could cause avoidable friction points and potentially hurt your brand reputation. For example, let’s say you create a

chrome os developer mode

How to Turn on Chrome OS Developer Mode

Google’s Chrome OS is a popular operating system that is widely used on Chromebooks and other devices. While it is designed to be simple and user-friendly, there are times when users may want to access additional features and functionality. One way to do this is by turning on Chrome OS