JTextArea default font very small in Windows
Asked Answered
F

6

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?

Ferous answered 23/6, 2011 at 22:46 Comment(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?Untwist
A
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
Affusion answered 8/8, 2011 at 3:39 Comment(0)
K
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.

Kirwan answered 23/8, 2012 at 11:14 Comment(0)
V
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.

Vinic answered 24/6, 2011 at 2:19 Comment(2)
on Windows it looks very different to OS windows. I really prefer "system" look and feelFerous
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.Affusion
A
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();
        }
    }

}
Ambo answered 24/6, 2011 at 8:46 Comment(2)
I don't want to specify the font, I only want to specify the size of the font :) Default font should be usedFerous
jTextArea doesn't have getStyle() method. setFont method doesn't accept 3 arguments.Ferous
F
1

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

textArea = new JTextArea();
textArea.setFont(UIManager.getFont("TextField.font"));
Ferous answered 24/6, 2011 at 13:12 Comment(0)
C
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.

Cameraman answered 8/8, 2011 at 3:27 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.