0

I'm making a program where I need to be able to edit the highlighting feature of a JTextArea via my code, but I dont want the user to be able to highlight via the mouse. That, or I need some way of being able to paint over the JTextArea manually (to look like highlighting).

I don't really have anything going code wise for this yet because I have no idea how to implement it.

Edit: I know how to paint over the text, but I need the painting to be transparent like highlighting.

Agustin Meriles
  • 4,866
  • 3
  • 29
  • 44
  • 1
    [maybe usage of JTextPane and Highlighter will better....](http://stackoverflow.com/a/9022901/714968) – mKorbel Mar 18 '13 at 22:02
  • also see [Highlight one specific row/line in JTextArea](http://stackoverflow.com/q/10191723/1048330) – tenorsax Mar 18 '13 at 22:03

3 Answers3

3

You could start by this code. It finds a word given by the user in String findstr and highlights this word in entire text area. You could use it to find and highlight a certain word multiple times in a text area till it reaches the end of text area content.

String findstr = findTextField.getText().toUpperCase(); // User Input Word to find
int findstrLength = findstr.length();                   
String findtextarea = textarea.getText().toUpperCase(); // TextArea Content
Highlighter h = textarea.getHighlighter();
h.removeAllHighlights();
try
    {
        int index=0;
         while(index>=0) {
        index = findtextarea.indexOf(findstr,index);
        if (index > 0) {
           h.addHighlight(index,index+findstrLength, DefaultHighlighter.DefaultPainter);
        }
        index++; // try adding this to allow you to look for the next index.
    }
    }
Sharad Tank
  • 209
  • 1
  • 3
  • 10
3

I dont want the user to be able to highlight via the mouse

That is referred to as "selection". You can disable this on a text component by using a custom Caret:

import java.awt.*;
import javax.swing.*;
import javax.swing.text.*;

public class NoTextSelectionCaret extends DefaultCaret
{
    public NoTextSelectionCaret(JTextComponent textComponent)
    {
        setBlinkRate( textComponent.getCaret().getBlinkRate() );
        textComponent.setHighlighter( null );
    }

    @Override
    public int getMark()
    {
        return getDot();
    }

    private static void createAndShowUI()
    {
        JTextField textField1 = new JTextField("No Text Selection Allowed");
        textField1.setCaret( new NoTextSelectionCaret( textField1 ) );
        textField1.setEditable(false);

        JTextField textField2 = new JTextField("Text Selection Allowed");

        JFrame frame = new JFrame("No Text Selection Caret");
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.add(textField1, BorderLayout.NORTH);
        frame.add(textField2, BorderLayout.SOUTH);
        frame.pack();
        frame.setLocationByPlatform( true );
        frame.setVisible( true );
    }

    public static void main(String[] args)
    {
        EventQueue.invokeLater(new Runnable()
        {
            public void run()
            {
                createAndShowUI();
            }
        });
    }
}
camickr
  • 321,443
  • 19
  • 166
  • 288
1

See the Highlighter.HighlightPainter interface, that easily (questionable), allows you to change the appearance of the highlight.

There are some concrete implementations, but you can define your own.

Mordechai
  • 15,437
  • 2
  • 41
  • 82