2

Is it possible to change the color of a text in a text field?I am trying to build an interpreter, so I was wondering on how would you change the color of the text in real time. For example the word I enter in the text field is:

printf("hi");

The word printf becomes green after a few seconds.

Is it possible?

Andrew Thompson
  • 168,117
  • 40
  • 217
  • 433
marchemike
  • 3,179
  • 13
  • 53
  • 96

3 Answers3

14

BlinkColorTextField

package test;

import java.awt.Color;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JOptionPane;
import javax.swing.JTextField;
import javax.swing.SwingUtilities;
import javax.swing.Timer;

public class BlinkColorTextField {

    BlinkColorTextField() {
        final JTextField blinkingText = new JTextField("Red & Blue");
        ActionListener blinker = new ActionListener() {
            boolean isRed = true;
            public void actionPerformed(ActionEvent ae) {
                if (isRed) {
                    blinkingText.setForeground(Color.BLUE);
                } else {
                    blinkingText.setForeground(Color.RED);
                }
                isRed = !isRed;
            }
        };
        Timer timer = new Timer(1000, blinker);
        timer.start();
        JOptionPane.showMessageDialog(null, blinkingText);
    }

    public static void main(String[] args) {
        SwingUtilities.invokeLater(new Runnable(){
            public void run() {
                new BlinkColorTextField();
            }
        });
    }

}
Andrew Thompson
  • 168,117
  • 40
  • 217
  • 433
  • The `setForeground` call indeed changes the text color, but of all the text in the textfield. According to the question, he only wants part of the text in a different color, which is nicely illustrated by Stefan Lindenberg's answer – Robin Mar 21 '12 at 12:20
  • Eh.. (shrugs) mine was showing an implementation of the `Timer` though of course the OP would want a non-repeating instance. – Andrew Thompson Mar 21 '12 at 12:24
  • Good point. I overlooked the "after a few seconds" part in the question. A +1 for this answer for the nice code and the picture – Robin Mar 21 '12 at 12:26
  • Actually, I was about to down-vote the other answers, but thought I'd make an example 1st. It was only after posting it that I noticed the subtlety in the question that specified only *part* of the text was to be green. But by then, I had code and an animated GIF, so I thought I'd leave it (and not down-vote the other answers). ;) – Andrew Thompson Mar 21 '12 at 12:31
  • +1, I never thought, that Timer can be used for creating such wonderful effects. LOL, that's amazing :-) – nIcE cOw Mar 21 '12 at 13:25
5

Try this:

HighlightPainter greenPainter = new DefaultHighlighter.DefaultHighlightPainter(Color.GREEN);

//in a thread...    
Highlighter h = tf.getHighlighter();
h.addHighlight(offset, offset+length, greenPainter); 
Stefan
  • 12,108
  • 5
  • 47
  • 66
4

You have to use JEditorPane / JTextPane instead of JTextField and also you can draw the text/string by overriding the paintComponent method.

KV Prajapati
  • 93,659
  • 19
  • 148
  • 186