Java - Fonts Selector with Combobox

How to Create a Font Chooser ComboBox In Java Netbeans

How to Create a Font Chooser ComboBox In Java Netbeans


In this Java Tutorial we will see How To Create a graphical user interface for selecting and displaying different fonts using a JComboBox In Java Using Netbeans.
We Will Retrieves all the available font family names from the local graphics environment, and Creates a JComboBox (fontsComboBox) with the array of font family names.
When a font family is selected from the combo box, a JLabel in the frame is updated to use the selected font family.

What We Are Gonna Use In This Project:

- Java Programming Language.
- NetBeans Editor.





Project Source Code:


package new_tutorials;

import java.awt.BorderLayout;
import java.awt.Component;
import java.awt.Font;
import java.awt.GraphicsEnvironment;
import javax.swing.DefaultListCellRenderer;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JList;
import javax.swing.JPanel;

/**
 *
 * @author 1BestCsharp
 */
public class FontsComboBox extends JFrame{
    
    private final JLabel label;
    
    
    public FontsComboBox(){
        
        setTitle("Fonts Combobox");
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        // Create an array of font family names
        String[] fontFamilies = GraphicsEnvironment.getLocalGraphicsEnvironment().getAvailableFontFamilyNames();
        // Create a JComboBox with a custom renderer
        JComboBox<String> fontsComboBox = new JComboBox<>(fontFamilies);
        fontsComboBox.setRenderer(new FontComboBoxRenderer());
        
        fontsComboBox.addActionListener((e) -> {
           
            String selectedFontFamiliy = (String)fontsComboBox.getSelectedItem();
            updateLabelFont(selectedFontFamiliy);
            
        });
        
        label = new JLabel("Simple Text");
        label.setHorizontalAlignment(JLabel.CENTER);
        
        JPanel panel = new JPanel(new BorderLayout());
        panel.add(fontsComboBox, BorderLayout.NORTH);
        panel.add(label, BorderLayout.CENTER);
        getContentPane().add(panel);
        setSize(400,300);
        setLocationRelativeTo(null);
        
    }
    
    
    private void updateLabelFont(String fontFamily){
        // Update the font of the label based on the selected font family
        Font font = new Font(fontFamily, Font.PLAIN, 18);
        label.setFont(font);
    }
    
    
    private static class FontComboBoxRenderer extends DefaultListCellRenderer{
        
        @Override
        public Component getListCellRendererComponent(JList<?> list, Object value, int index, boolean isSelected, boolean hasFocus){
            // Use the default rendering for the item text
            JLabel label = (JLabel) super.getListCellRendererComponent(list, value, index, isSelected, hasFocus);
            
            // Set the font for the item text based on the font family
            String fontFamily = (String) value;
            Font font = new Font(fontFamily, Font.PLAIN, 24);
            label.setFont(font);
            return label;
        }
        
    }
    

    public static void main(String[] args) {
        
        FontsComboBox frame = new FontsComboBox();
        frame.setVisible(true);
        
    }
    
}


The Final Result:

Java - Fonts Selector with Combobox

How To Select And display Different Fonts Using a JComboBox In Java Using Netbeans

Select And display Different Fonts Using a JComboBox In Java Using Netbeans

How To Select And display Different Fonts Using a JComboBox In Java

Select And display Different Fonts Using a JComboBox In Java










Share this

Related Posts

Latest
Previous
Next Post »