15

I'm using platform look-and-fell and on Linux my JTextArea is pretty readable But on Windows it uses "Monospaced 9" and the text is very small.

Why and what is the best way to fix that?

Why default Windows look-and-fell uses such small font in JTextArea?

jzd
  • 23,473
  • 9
  • 54
  • 76
Oleg Vazhnev
  • 23,239
  • 54
  • 171
  • 305
  • 1
    hmm, cant reproduce: on my system (Vista) the font is monospaced 16. Typically, WindowLookAndFeel uses the OS font settings - so maybe your system settings are so small? What's your win version? – kleopatra Aug 08 '11 at 09:37

6 Answers6

29

Instead of creating new font, it is better to derive existing font, because this way you'll save the font set by platform look and feel, and it may also avoid problems with unicode characters:

textArea.setFont(textArea.getFont().deriveFont(12f)); // will only change size to 12pt
Denis Tulskiy
  • 19,012
  • 6
  • 50
  • 68
8

Here's a solution that you can use to change all JTextAreas at once instead of using setFont() every time you add new text area:

UIManager.getDefaults().put("TextArea.font", UIManager.getFont("TextField.font"));

Call this on start of your application, after setting the Look and Feel.

Most L&Fs use the same font for JTextArea and JTextField, it's strange that Windows doesn't.

Speedstone
  • 383
  • 3
  • 5
2

If you want a consistent look then use the Nimbus or Metal look and feel instead of the OS default. That will also allow you to tweak any settings. Plus I personally I think the Nimbus Look and Feel is much smoother looking than the others.

jzd
  • 23,473
  • 9
  • 54
  • 76
  • on Windows it looks very different to OS windows. I really prefer "system" look and feel – Oleg Vazhnev Jun 24 '11 at 13:04
  • nevertheless, Nimbus and Metal look alien on any platform and some users really don't like it. I hope that time for fancy-looking skinned apps is gone. – Denis Tulskiy Aug 08 '11 at 03:43
1

You can use the JTextArea1.setFont(Font(String name, int style, int size)) method to specify the specific type of font for a JTextArea component. As an example

jTextArea1.setFont(new Font("Arial Black", Font.BOLD, 8));


import java.awt.BorderLayout;
import java.awt.Font;
import javax.swing.JTextArea;
import javax.swing.WindowConstants;
import javax.swing.SwingUtilities;

public class NewJFrame extends javax.swing.JFrame {

    private JTextArea jTextArea1;
    private JTextArea jTextArea2;

    public static void main(String[] args) {
        SwingUtilities.invokeLater(new Runnable() {
            public void run() {
                NewJFrame inst = new NewJFrame();
                inst.setLocationRelativeTo(null);
                inst.setVisible(true);
            }
        });
    }

    public NewJFrame() {
        super();
        initGUI();
    }

    private void initGUI() {
        try {
            setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE);
            {
                jTextArea1 = new JTextArea();
                getContentPane().add(jTextArea1, BorderLayout.NORTH);
                jTextArea1.setText("This is a fox running slow");
                jTextArea1.setFont(new Font("Arial Black", Font.BOLD, 8));
                jTextArea1.setPreferredSize(new java.awt.Dimension(164, 114));
            }
            {
                jTextArea2 = new JTextArea();
                getContentPane().add(jTextArea2, BorderLayout.SOUTH);
                jTextArea2.setText("This is a fox running slow");
                jTextArea2.setFont(new Font("Book Antiqua", Font.ITALIC, 12));
                jTextArea2.setPreferredSize(new java.awt.Dimension(384, 129));
            }
            pack();
            setSize(400, 300);
        } catch (Exception e) {
            //add your error handling code here
            e.printStackTrace();
        }
    }

}
Candamir
  • 586
  • 9
  • 22
Rakesh
  • 4,264
  • 4
  • 32
  • 58
1

I've just used TextField font in TextArea...

textArea = new JTextArea();
textArea.setFont(UIManager.getFont("TextField.font"));
Oleg Vazhnev
  • 23,239
  • 54
  • 171
  • 305
0

Just do

textArea.setFont(new Font("Arial", Font.PLAIN, 16));

That changes all of the text inside of the textarea to the same size font.