Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Editing Text Areas

Status
Not open for further replies.

bos

Technical User
Oct 1, 2002
50
US
Hello,

I'm working on a program which has a text area. I'd like to be able to hightlight a portion of the text area and edit whatever is highlighted. For example, search out the word "the" and change it to "there".

My first question is how to search for words in a text area?

Second, how can a process like a search be done in just a highlighted area? Is there a function already for that?

Please let me know. Thanks.
 
Hi bos,
First off, everyone wish me Happy Birthday! My address for presents will be given by request. [bigsmile]
For what you want to do as far as highlighting, Swing is the way to go. All
Code:
JTextComponent
s can be given a
Code:
Highlighter
object that will highlight specific areas of the the text in the
Code:
JTextComponent
.
Code:
Highlighter
is an interface, but there is a
Code:
DefaultHighlighter
object that we can use.
Like many Swing objects,
Code:
Highlighter
s require an object to show how to render things, which implements the interface
Code:
HighlighterPainter
, an inner interface in
Code:
Highlighter
. There is also a
Code:
DefaultHighlighterPainter
, which is an inner class of
Code:
DefaultHighlighter
.
First you have to set the
Code:
Highlighter
object with whichever text object you are using. Then, simply give the start and end indices for the selection to the highlighter.
I took some code I already had that used highlighters and heavily modified it. It highlights based on user input and can highlight matching searches. It does not limit searches to highlighted areas, though with a little work that should be easy enough. Here it is, enjoy:
[/code]
import java.awt.*;
import java.awt.event.*;
import java.awt.geom.*;

import javax.swing.*;
import javax.swing.event.*;
import javax.swing.text.*;

import java.io.*;
import java.util.*;
import java.util.regex.*;

public class HighlightJTextArea extends JTextArea {

private DefaultHighlighter highlighter = new DefaultHighlighter ();

private DefaultHighlighter.DefaultHighlightPainter highlighterPainter = new DefaultHighlighter.DefaultHighlightPainter (new Color (198, 214, 253));

private Object highlight;

public HighlightJTextArea (int rows, int cols) {
super (rows, cols);
setHighlighter (highlighter);
}

public HighlightJTextArea (String text) {
super (text);
setHighlighter (highlighter);
}

public void removeHighlights () {
highlighter.removeAllHighlights ();
}

private void addHighlight (int start, int stop) {
try {
highlight = highlighter.addHighlight (start, stop, highlighterPainter);
}
catch (BadLocationException ex) {
ex.printStackTrace ();
}
}

public static void main (String [] args) {
JFrame frame = new JFrame ("HighlightJTextArea Demo");
frame.addWindowListener (new WindowAdapter () {
public void windowClosing (WindowEvent event) {
System.exit (1);
}
});

//HighlightJTextArea object, set to 10 rows, 5 cols
final HighlightJTextArea highlightArea = new HighlightJTextArea (10, 5);
//Allow line wrap
highlightArea.setLineWrap (true);

//Allow highlightArea to scroll if needed
JScrollPane scrollPane = new JScrollPane (highlightArea);

final JTextField searchField = new JTextField (10);

final JTextField startHighlightField = new JTextField (5);
final JTextField stopHighlightField = new JTextField (5);

JButton highlightButton = new JButton ("Highlight");

Container contentPane = frame.getContentPane ();
contentPane.setLayout (new BorderLayout ());

JPanel panel;

//searchField setup
panel = new JPanel ();
panel.setLayout (new FlowLayout (FlowLayout.LEFT, 5, 5));
panel.add (new JLabel ("Search Text:"));
panel.add (searchField);

/**
* Gets search string from searchField, performs a query using Regex, highlights areas
* that match.
*/
searchField.addActionListener (new ActionListener () {
public void actionPerformed (ActionEvent event) {

String text = highlightArea.getText ();

if (text.equals ("")) {
return;
}

String search = searchField.getText ();
if (search.equals ("")) {
return;
}

/**
* Use the search text as our pattern. Note, some text might not work entered in like this.
* The '.', for example, allows for any character, and would accept everything.
* To specify a '.', use "\.".
*/
Pattern pattern = Pattern.compile (search);

/**
* Use the text from our highlightArea to serach on.
*/
Matcher matcher = pattern.matcher (text);

/**
* Remove all highlights so far done and search. While searches are found,
* highlight those matches.
*/
highlightArea.removeHighlights ();
while (matcher.find ()) {
highlightArea.addHighlight (matcher.start (), matcher.end ());
}
}
});

contentPane.add (panel, BorderLayout.NORTH);

//highlightArea setup
contentPane.add (scrollPane, BorderLayout.CENTER);

panel = new JPanel ();
panel.setLayout (new FlowLayout (FlowLayout.LEFT, 5, 5));
panel.add (new JLabel ("Highlight Start:"));
panel.add (startHighlightField);
panel.add (new JLabel ("Highlight Stop:"));
panel.add (stopHighlightField);
panel.add (highlightButton);

/**
* Parses through the startHighlightField and stopHighlightField for their positions.
* If either is formatted incorrectly, show that by setting background to Color.RED and return.
* If both are formatted correctly, set highlight.
*/
highlightButton.addActionListener (new ActionListener () {
public void actionPerformed (ActionEvent event) {
int start, stop;

try {
start = Integer.parseInt (startHighlightField.getText ());
}
catch (Exception exception) {
startHighlightField.setBackground (Color.RED);
return;
}
startHighlightField.setBackground (Color.WHITE);

try {
stop = Integer.parseInt (stopHighlightField.getText ());
}
catch (Exception exception) {
stopHighlightField.setBackground (Color.RED);
return;
}
stopHighlightField.setBackground (Color.WHITE);

highlightArea.removeHighlights ();
highlightArea.addHighlight (start, stop);
}
});

contentPane.add (panel, BorderLayout.SOUTH);

frame.pack ();
frame.show ();
}
}
[/code]


Happy Birthday to me, [bdaycandle]
MarsChelios
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top