- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
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 to display the different font items inside a JComboBox 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 display the different font styles inside a JComboBox by implementing the ListCellRenderer interface
Example
import java.awt.*; import javax.swing.*; public class JComboBoxFontTest extends JFrame { private JComboBox fontComboBox; private String fontName[]; private Integer array[]; public JComboBoxFontTest() { setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); GraphicsEnvironment ge = GraphicsEnvironment.getLocalGraphicsEnvironment(); fontName = ge.getAvailableFontFamilyNames(); array = new Integer[fontName.length]; for(int i=1;i<=fontName.length;i++) { array[i-1] = i; } fontComboBox = new JComboBox(array); ComboBoxRenderar renderar = new ComboBoxRenderar(); fontComboBox.setRenderer(renderar); setLayout(new FlowLayout()); add(fontComboBox); setSize(400, 300); setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); setLocationRelativeTo(null); setVisible(true); } private class ComboBoxRenderar extends JLabel implements ListCellRenderer { @Override public Component getListCellRendererComponent(JList list, Object value, int index, boolean isSelected, boolean cellHasFocus) { int offset = ((Integer)value).intValue() - 1; String name = fontName[offset]; setText(name); setFont(new Font(name,Font.PLAIN,20)); return this; } } public static void main(String args[]) { new JComboBoxFontTest(); } }
Output
- Related Articles
- How to display the items in a JComboBox in Java
- How to center align the items of a JComboBox in Java?
- How to display the first element in a JComboBox in Java
- How to add items in a JComboBox on runtime in Java
- How can we sort the items of a JComboBox in Java?
- How can we set the border to JComboBox items in Java?
- How can we add different font style items to JList in Java?\n
- How to hide and display JCombobox with a JCheckBox in Java?
- How can we set the foreground and background color to JComboBox items in Java?
- How to display a bold text inside the JTextArea in Java?\n
- Display text inside an element in a smaller font size with Bootstrap
- How to display a string in the fixed-pitch font?
- How can we display the line numbers inside a JTextArea in Java?
- How to handle action event for JComboBox in Java?
- How to display different list commands in JShell in Java 9?

Advertisements