Display Different Font Items in JComboBox in Java



In this article, we will learn to display the different font items inside a JComboBox in Java. The Swing framework provides a powerful JComboBox component that allows users to select an object from a drop-down list.

What is a 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 an ActionListener, ChangeListener, and ItemListener interfaces when the user interacts with a combo box.

Syntax

The following is the syntax:

JComboBox Box_Name = new JComboBox(array);

Problem statement

The standard JComboBox displays items as plain text. To show fonts visually, we need a custom renderer that renders each font item using that specific font. We can display the different font styles inside a JComboBox by implementing the ListCellRenderer interface.

Approach

Retrieving all font family names available in the current graphics environment:

GraphicsEnvironment ge = GraphicsEnvironment.getLocalGraphicsEnvironment();
fontName = ge.getAvailableFontFamilyNames();

Then we will create an array of indices that will map to the font names:

array = new Integer[fontName.length];
for(int i = 1; i <= fontName.length; i++) {
    array[i-1] = i;
}

The combo box is populated with the index array, and we set our custom renderer:

fontComboBox = new JComboBox(array);
ComboBoxRenderer renderer = new ComboBoxRenderer();
fontComboBox.setRenderer(renderer);

The ComboBoxRenderer class extends JLabel and implements ListCellRenderer. The key method is getListCellRendererComponent, which is called for each item in the combo box.

Example to Display Different Fonts

Below is an example to display the different font items inside a JComboBox in Java:

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

Alshifa Hasnain
Alshifa Hasnain

Converting Code to Clarity

Updated on: 2025-04-18T16:04:32+05:30

525 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements