Hi bos,
First off, everyone wish me Happy Birthday! My address for presents will be given by request.
![[bigsmile] [bigsmile] [bigsmile]](/data/assets/smilies/bigsmile.gif)
For what you want to do as far as highlighting, Swing is the way to go. All
s can be given a
object that will highlight specific areas of the the text in the
.
is an interface, but there is a
object that we can use.
Like many Swing objects,
s require an object to show how to render things, which implements the interface
, an inner interface in
. There is also a
Code:
DefaultHighlighterPainter
, which is an inner class of
.
First you have to set the
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] [bdaycandle] [bdaycandle]](/data/assets/smilies/bdaycandle.gif)
MarsChelios