Set Foreground and Background Color to JComboBox Items in Java



In this article, we will learn set the foreground and background color to JComboBox items in Java. Setting basic foreground and background colors for the combo box helps to create interactive Swing-based applications.

JComboBox

A JComboBox is a subclass of the JComponent class, and it is a combination of a text field and a drop-down list from which the user can choose a value. A JComboBox can generate the ActionListener, ChangeListener, and ItemListener interfaces when the user actions with a combo box.

setForeground() Method

The setForeground() method can be used to set the foreground color of JComboBox items in Java. This method takes "Color c" as a parameter, which defines the color for the foreground text.

Method Declaration:

public void setForeground(Color c)

setBackground() Method

The setBackground() method can be used to set the background color of JComboBox items in Java. This method takes "Color c" as a parameter, which defines the color for the background of the combo box.

Method Declaration:

public void setBackground(Color c)

Setting the Foreground and Background Color

We can also set the foreground and background color to JComboBox items by using the setForeground() and setBackground() methods of the JComboBox class.

The following are the steps for setting the foreground and background color of JComboBox items in Java:

ComboBox Data Initialization

Creates a string array named "countries" containing country names, and initializes the JComboBox with these country names as items.

String[] countries = {"India", "Australia", "England", "South Africa", "Newzealand"};
jcb = new JComboBox(countries);

Adding Color

Sets the text color foreground to blue using the setForeground() method, and sets the background color of the JComboBox to white using the setBackground() method.

jcb.setForeground(Color.blue);
jcb.setBackground(Color.white);

Positioning the JComboBox

Adds the JComboBox to the top of the JFrame using BorderLayout.

add(jcb, BorderLayout.NORTH);

Example

Below is an example of setting the foreground and background color of JComboBox items in Java:

import java.awt.*;
import javax.swing.*;
public class JComboBoxItemColorTest extends JFrame{
   private JComboBox jcb;
   public JComboBoxItemColorTest() {
      setTitle("JComboBoxItemColor Test");
      String[] countries = {"India", "Australia", "England", "South Africa", "Newzealand"};
      jcb = new JComboBox(countries);
      jcb.setForeground(Color.blue);
      jcb.setBackground(Color.white);
      add(jcb, BorderLayout.NORTH);
      setSize(500,300);
      setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
      setLocationRelativeTo(null);
      setVisible(true);
   }
   public static void main(String[]args) {
      new JComboBoxItemColorTest();
   }
}

Output

Alshifa Hasnain
Alshifa Hasnain

Converting Code to Clarity

Updated on: 2025-05-15T11:25:42+05:30

2K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements