1

I have a JTextArea in which I highlight some text using the addHighlight method of the Highlighter I get from the JTextArea. It highlights the text but it does not change the text color of the highlighted text to the selectedTextColor I have set.

Here is an example:

import java.awt.Color;

import javax.swing.JFrame;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.SwingUtilities;
import javax.swing.text.BadLocationException;
import javax.swing.text.DefaultHighlighter;
import javax.swing.text.Highlighter;
import javax.swing.text.Highlighter.HighlightPainter;

public class SSCCE {

    private JFrame frame;
    private JTextArea textArea;

    public SSCCE() {
        frame = new JFrame();
        frame.setTitle("Huge Text");
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

        textArea = new JTextArea("abcd abcd abcd");
        textArea.setBackground(Color.DARK_GRAY);
        textArea.setForeground(Color.LIGHT_GRAY);
        textArea.setSelectionColor(Color.LIGHT_GRAY);
        textArea.setSelectedTextColor(Color.DARK_GRAY);
        Highlighter highLighter = textArea.getHighlighter();
        HighlightPainter highLightPainter = new DefaultHighlighter.DefaultHighlightPainter(textArea.getSelectionColor());
        try {
            highLighter.addHighlight(0, 10, highLightPainter);
        } catch (BadLocationException e) {
            e.printStackTrace();
        }
        frame.add(new JScrollPane(textArea));

        frame.setSize(400, 350);
        frame.setLocationRelativeTo(null);
        frame.setVisible(true);
    }

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

}
usama8800
  • 893
  • 3
  • 10
  • 20

1 Answers1

0

Worth reading about Using Text Components

  • If you intend to use an unstyled text component then choose text field, password field, formatted text field, or text area.

  • If you intend to use a styled text component, see How to Use Editor Panes and Text Panes

enter image description here

JTextArea doesn't support this functionality to style a sub set of the entire content. It applies styles but across the entire content.

Find a sample code here change specific text color in java

Community
  • 1
  • 1
Braj
  • 46,415
  • 5
  • 60
  • 76
  • 1
    Actually, JTextArea does have highlighter support, as demonstrated [here](http://stackoverflow.com/questions/22676024/select-text-in-2-jtextarea-at-the-same-time/22676432#22676432) and [here](http://stackoverflow.com/questions/17985808/search-text-file-and-display-results-in-a-jpanel/17988175#17988175) – MadProgrammer May 30 '14 at 21:41
  • @MadProgrammer Can I change the color and font of the selected text only in JTextArea? In second sample code it just highlighted. It works fine if OP do a single change to `textArea.setForeground(Color.BLUE);` to differentiate it with selection color. – Braj May 30 '14 at 21:47
  • No, but the OP isn't doing this, they are are using a highlighter ;) – MadProgrammer May 30 '14 at 21:49
  • OP says *the text color of the highlighted text to the selectedTextColor* – Braj May 30 '14 at 21:49
  • foreground color is light gray and OP wants dark gray foreground color for selected text only. – Braj May 30 '14 at 21:51
  • See my previous edits in my post itself. I have answered same but then I noticed that OP is not looking into it hence I have updated my post. – Braj May 30 '14 at 21:53