0

Is there a way to improve font rendering in Swing JTextFields? Here's what I'm getting right now: enter image description here

As you can see, it looks pretty jagged. Is there a way I can improve that? I'm using the GTKLookAndFeel, just in case anyone needs to know.

(I looked at this question, but it didn't help much.)

SSCCE:

public class foo extends JFrame{

  foo(){
    add(new JTextField);
    setVisible(true);
  }

}

I'm on Linux, so that might have something to do with it. I'm using Infinality in linux for better fonts.

Community
  • 1
  • 1
celloplayer
  • 31
  • 1
  • 8

3 Answers3

2

Using the sscce below, I see the following appearance with the GTK+ L&F.

Addendum: As a workaround, you might try setting a UI delegate property, for example,

JTextField tf = new JTextField();
UIManager.put("TextField.font", tf.getFont().deriveFont(Font.BOLD));

image plain image bold

import component.Laf;
import java.awt.EventQueue;
import java.awt.GridLayout;
import javax.swing.JFrame;
import javax.swing.JPasswordField;
import javax.swing.JTextField;

/** @see http://stackoverflow.com/a/18969361/230513 */
public class Test {

    private void display() {
        JFrame f = new JFrame("Test");
        f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        f.setLayout(new GridLayout(0, 1));
        // http://stackoverflow.com/a/11949899/230513
        f.add(Laf.createToolBar(f));
        f.add(new JTextField("bla@foo.com"));
        f.add(new JPasswordField("*****"));
        f.pack();
        f.setLocationRelativeTo(null);
        f.setVisible(true);
    }

    public static void main(String[] args) {
        EventQueue.invokeLater(new Runnable() {

            @Override
            public void run() {
                new Test().display();
            }
        });
    }
}
trashgod
  • 203,806
  • 29
  • 246
  • 1,045
1

To get a smother text, you should enable anti-aliasing:

$ java -jar lib/application.jar -Dswing.aatext=true
Moritz Petersen
  • 12,902
  • 3
  • 38
  • 45
  • I thought they stopped using this parameter somewhere better Java 5 & 6? – MadProgrammer Sep 23 '13 at 20:06
  • Surprisingly passing similar parameter to netbeans, made it render fonts perfectly, however, when I use it from command line to run my app (just like you have shown), I see no effect on my Swing UI... :( – Dimitry K Jan 24 '14 at 13:49
-2

Using this code you can change the font and font color in a JTextArea.

We have created a JTextArea called txt. With a few simple lines of code you can change its font, color & size settings:

Font font = new Font("Verdana", Font.BOLD, 12);
txt.setFont(font);
txt.setForeground(Color.BLUE);

There are several font settings in the Font class including PLAIN, BOLD, ITALIC and 13 different colors in the Color class (listed below).

  • BLACK
  • BLUE
  • CYAN
  • DARK_GRAY
  • GRAY GREEN
  • LIGHT_GRAY
  • MAGENTA
  • ORANGE
  • PINK
  • RED
  • WHITE
  • YELLOW
Dony Kha
  • 5
  • 1