Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Getting fonts, sizes, bold,...etc

Tags:

java

fonts

swing

I'm having trouble finding stuff on accessing Windows fonts or predefined fonts, and sizes. So for my java program I have a JComboBox with fonts, sizes, and colors. The problem is that I need to pre-Enter the fonts, sizes and colors. How would I be able to get the predefined fonts, colors, and sizes? So far this is what I have for this font but its not in the correct way.

               if (font.equals("Arial")) {

                if (size.equals("8")) {
                    setSize = 8;
                } else if (size.equals("10")) {
                    setSize = 10;
                } else if (size.equals("12")) {
                    setSize = 12;
                }

                if (color.equals("Black")) {
                    setColor = Color.BLACK;
                } else if (color.equals("Blue")) {
                    setColor = Color.BLUE;
                } else if (color.equals("Red")) {
                    setColor = Color.red;
                }

                Font font = new Font("Arial", setAttribute, setSize);
                Writer.setFont(font);
                Writer.setForeground(setColor);
like image 944
Jazzy Avatar asked Aug 06 '11 05:08

Jazzy


People also ask

What are the different font sizes?

Some of these dimensions include xx-small, x-small, small, medium, large, x-large, and xx-large. Font size may also be specified using Point, Pica, Inch, Centimeter, Millimeter, em, and percentage.


1 Answers

GraphicsEnvironment ge = GraphicsEnvironment.getLocalGraphicsEnvironment();
String[] fonts = ge.getAvailableFontFamilyNames();

The sizes and styles can be set at run-time.

E.G.

Font Chooser

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

public class ShowFonts {

    public static void main(String[] args) {
        SwingUtilities.invokeLater(() -> {
            GraphicsEnvironment ge = GraphicsEnvironment.getLocalGraphicsEnvironment();
            String[] fonts = ge.getAvailableFontFamilyNames();
            JComboBox fontChooser = new JComboBox(fonts);
            fontChooser.setRenderer(new FontCellRenderer());
            JOptionPane.showMessageDialog(null, fontChooser);
        });
    }
}

class FontCellRenderer extends DefaultListCellRenderer {

    public Component getListCellRendererComponent(
            JList list,
            Object value,
            int index,
            boolean isSelected,
            boolean cellHasFocus) {
        JLabel label = (JLabel)super.getListCellRendererComponent(
                list,value,index,isSelected,cellHasFocus);
        Font font = new Font(value.toString(), Font.PLAIN, 20);
        label.setFont(font);
        return label;
    }
}

JavaDoc

The JDoc for GraphicsEnvironment.getAvailableFontFamilyNames() state in part..

Returns an array containing the names of all font families in this GraphicsEnvironment localized for the default locale, as returned by Locale.getDefault()..

See also:

getAllFonts()..

like image 57
Andrew Thompson Avatar answered Oct 13 '22 00:10

Andrew Thompson