Java Swing Font Chooser.

A Simple Java Swing Font Chooser Program Example

  • Java
  • 3 mins read

A simple Java Swing font chooser program example to give the user an option to select the font and that will apply to a text label.

Java Swing Font Chooser Example

FontChooser.java

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.event.*;

public class FontChooser extends JPanel {

  protected JList<String> nameList;
  protected JComboBox<Integer> sizeBox;
  protected JCheckBox boldBox;
  protected JCheckBox italicBox;

  protected FontFrame frame;

  public final static int[] fontSizes = {10, 12, 14, 18, 24, 32, 48, 64};

  public FontChooser(FontFrame stf) {
    super();
    frame = stf;
    createComponents();
    buildLayout();
  }

  protected void buildLayout() {
    JLabel label;
    GridBagConstraints constraints = new GridBagConstraints();
    GridBagLayout layout = new GridBagLayout();
    setLayout(layout);

    constraints.anchor = GridBagConstraints.WEST;
    constraints.insets = new Insets(5, 10, 5, 10);

    constraints.gridx = 0;
    label = new JLabel("Name:", JLabel.LEFT);
    layout.setConstraints(label, constraints);
    add(label);
    label = new JLabel("Size:", JLabel.LEFT);
    layout.setConstraints(label, constraints);
    add(label);
    layout.setConstraints(boldBox, constraints);
    add(boldBox);

    constraints.gridx++;
    nameList.setVisibleRowCount(3);
    JScrollPane jsp = new JScrollPane(nameList);
    layout.setConstraints(jsp, constraints);
    add(jsp);
    layout.setConstraints(sizeBox, constraints);
    add(sizeBox);
    layout.setConstraints(italicBox, constraints);
    add(italicBox);
  }

  protected void createComponents() {
    GraphicsEnvironment ge =
        GraphicsEnvironment.getLocalGraphicsEnvironment();
    String[] names = ge.getAvailableFontFamilyNames();
    nameList = new JList<String>(names);
    nameList.setSelectedIndex(0);
    nameList.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
    nameList.addListSelectionListener(new ListSelectionListener() {
      public void valueChanged(ListSelectionEvent event) {
        handleFontPropertyChange();
      }
    }
    );
    Integer sizes[] = new Integer[fontSizes.length];
    for (int i = 0; i < sizes.length; i++) {
      sizes[i] = new Integer(fontSizes[i]);
    }
    sizeBox = new JComboBox<Integer>(sizes);
    sizeBox.addActionListener(new ActionListener() {
      public void actionPerformed(ActionEvent event) {
        handleFontPropertyChange();
      }
    }
    );
    boldBox = new JCheckBox("Bold");
    boldBox.addActionListener(new ActionListener() {
      public void actionPerformed(ActionEvent event) {
        handleFontPropertyChange();
      }
    }
    );
    italicBox = new JCheckBox("Italic");
    italicBox.addActionListener(new ActionListener() {
      public void actionPerformed(ActionEvent event) {
        handleFontPropertyChange();
      }
    }
    );
  }

  protected void handleFontPropertyChange() {
    frame.refreshDisplayFont();
  }

  public String getSelectedFontName() {
    return (String)(nameList.getSelectedValue());
  }

  public int getSelectedFontSize() {
    return ((Integer)(sizeBox.getSelectedItem())).intValue();
  }

  public boolean isBoldSelected() {
    return boldBox.isSelected();
  }

  public boolean isItalicSelected() {
    return italicBox.isSelected();
  }

}

FontFrame.java

import java.awt.*;
import javax.swing.*;
import javax.swing.border.*;
import javax.swing.event.*;
import javax.swing.text.*;

public class FontFrame extends JFrame {

  protected FontChooser propertiesPanel;
  protected JTextField sampleText;
  protected JLabel displayArea;

  public static void main(String[] args) {
    FontFrame stf = new FontFrame();
    stf.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    stf.setVisible(true);
  }

  public FontFrame() {
    super();
    createComponents();
    createDocumentListener();
    buildLayout();
    refreshDisplayFont();
    pack();
  }

  protected void createComponents() {
    propertiesPanel = new FontChooser(this);
    sampleText = new JTextField(20);
    displayArea = new JLabel("");
    displayArea.setPreferredSize(new Dimension(200, 75));
    displayArea.setMinimumSize(new Dimension(200, 75));
  }

  protected void createDocumentListener() {
    Document document = sampleText.getDocument();
    document.addDocumentListener(new DocumentListener() {
      public void changedUpdate(DocumentEvent event) {
        handleDocumentUpdate();
      }

      public void insertUpdate(DocumentEvent event) {
        handleDocumentUpdate();
      }

      public void removeUpdate(DocumentEvent event) {
        handleDocumentUpdate();
      }
    }
    );
  }

  protected void buildLayout() {
    Container pane = getContentPane();
    GridBagConstraints constraints = new GridBagConstraints();
    GridBagLayout layout = new GridBagLayout();
    pane.setLayout(layout);

    constraints.insets = new Insets(5, 10, 5, 10);
    constraints.fill = GridBagConstraints.HORIZONTAL;
    constraints.weightx = 1;

    constraints.gridx = 0;
    BevelBorder bb = new BevelBorder(BevelBorder.RAISED);
    TitledBorder tb = new TitledBorder(bb, "Font");
    propertiesPanel.setBorder(tb);
    layout.setConstraints(propertiesPanel, constraints);
    pane.add(propertiesPanel);

    layout.setConstraints(sampleText, constraints);
    pane.add(sampleText);

    layout.setConstraints(displayArea, constraints);
    pane.add(displayArea);
  }

  protected void handleDocumentUpdate() {
    displayArea.setText(sampleText.getText());
  }

  public void refreshDisplayFont() {
    displayArea.setFont(getSelectedFont());
  }

  public Font getSelectedFont() {
    String name = propertiesPanel.getSelectedFontName();
    int style = 0;
    style += (propertiesPanel.isBoldSelected() ? Font.BOLD : 0);
    style += (propertiesPanel.isItalicSelected() ? Font.ITALIC : 0);
    int size = propertiesPanel.getSelectedFontSize();
    return new Font(name, style, size);
  }

}

Now run the program FontFrame.java, and the output would be as shown below:

Java Swing Font Chooser.
Illustrating font chooser dialog developed in Java. Image by FoxInfotech.

See also: