- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
How can we set the foreground and background color to JComboBox items in Java?
A JComboBox is a subclass of 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 an ActionListener, ChangeListener and ItemListener interfaces when the user actions on a combo box. We can also set the foreground and background color to JComboBox items by using setForeground() and setBackground() methods of a JComboBox class.
Example
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
- Related Articles
- How can we set the border to JComboBox items in Java?
- How can we set the background/foreground color for individual column of a JTable in Java?
- How can we change the background and foreground color of a JTooltip in Java?
- How to change JLabel background and foreground color in Java?
- How can we set the background color to a JPanel in Java?
- How can we set a background color to JSplitPane in Java?
- Customize the tooltip font, color , background and foreground color in Java
- How can we sort the items of a JComboBox in Java?
- How can we implement editable JComboBox in Java?
- How to change the background and foreground colors of tab in Java?
- How to display the items in a JComboBox in Java
- How to set default background color for JTextPane in Java?
- How to set background color in jQuery?
- How to set background color in HTML?
- How to center align the items of a JComboBox in Java?

Advertisements