Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I force one specific JTextArea to not use anti-aliasing, while keeping it on for the rest of my app?

Tags:

java

swing

I'm using Java 6 on Mac OS X 10.6. So are my users. I'm trying to force one specific JTextArea not to use anti-aliasing.

Any ideas?

Here's my test code as is:

public static void main(String[] args) {

    JTextArea jTextArea1 = new JTextArea("This is some text which should be anti-aliased");
    jTextArea1.setFont(new Font("Lucida Grande", Font.PLAIN, 14));

    JTextArea jTextArea2 = new JTextArea("Please no anti-aliasing for this text");
    jTextArea2.setFont(new Font("Monaco", Font.PLAIN, 10));

    final JFrame frame = new JFrame();
    frame.getContentPane().add(new JScrollPane(jTextArea1), BorderLayout.NORTH);
    frame.getContentPane().add(new JScrollPane(jTextArea2), BorderLayout.SOUTH);

    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frame.pack();
    frame.setLocationRelativeTo(null);
    frame.setVisible(true);
}
like image 970
Steve McLeod Avatar asked Nov 05 '22 03:11

Steve McLeod


2 Answers

In Java > 5, you don't need to override paint methods. You can set a client property like this:

jTextArea2.putClientProperty(sun.swing.SwingUtilities2.AA_TEXT_PROPERTY_KEY, null);

Note that SwingUtilities2 is a sun class, so this may not work in other jvms.

like image 25
dogbane Avatar answered Nov 14 '22 00:11

dogbane


I didn't test it, but you can try to override the paintComponent method of your textarea:

public void drawComponent(Graphics g)
{
   Graphics2D g2d = (Graphics2D) g;
   g2d.setRenderingHint(RenderingHints.KEY_TEXT_ANTIALIASING, RenderingHints.VALUE_TEXT_ANTIALIAS_OFF);
   super.drawComponent(g2d);
}
like image 191
Martijn Courteaux Avatar answered Nov 14 '22 01:11

Martijn Courteaux