How to highlight text in a JEditorPane which is an HTML display?

Hello everyone!

In my program I use a JEditorPane and HTML to display a set of records retrieved from a database in a Web-like enviroment, similar to a google result page. I want to let the user search text in the result page so that he or she can locate a certain result more quickly.

To do so i extract the text from the JEditorPane and permor a search in it with the indexOf function of the String class once the location of the String is returned I try to use the Highlighter of the JEditorPane to highlight the text but it doesn't work and I don't understand why.

I have been looking around the web but most of the examples I have found are using JEditorPane as a plain text or rich text none is using the JEditorPane as an HTML display. What I was wondering is...Is it possible to highlight text in a JEditorPane that is displaying HTML? and How do you do it?

Here's my code:

publicvoid findInPage(JEditorPane page, String sText)

{

int offset;

int startPos;

int sTextLenght;

Highlighter hliter;

int pageTextLength;

String pageText =null;

if(page ==null || sText ==null)return;

sTextLenght = sText.length();

try

{

pageTextLength = page.getDocument().getLength();

if(pageTextLength < 1)return;

pageText = page.getDocument().getText(0, pageTextLength);

}

catch(BadLocationException ex)

{

// Bad location?

System.out.println(ex);

return;

}

if(pageText ==null)return;

startPos = 0;

if(lastSearchPage == page && lastSearchText.equals(sText))

if(lastLocateIndex >= 0)

startPos = lastLocateIndex + 1;

System.out.println(sText +" starting at: " + startPos);

offset = pageText.indexOf(sText, startPos);

if(offset < 0)

{

JOptionPane.showMessageDialog(null,"Text not found","Find in results", JOptionPane.INFORMATION_MESSAGE);

lastSearchPage = page;

lastSearchText = sText;

lastLocateIndex = -1;

}

else

{

hliter = page.getHighlighter();

hliter.removeAllHighlights();

try

{

hliter.addHighlight(offset, sTextLenght, myHighlightPainter);

}

catch(Exception ex)

{

System.out.println(ex);

}

lastSearchPage = page;

lastSearchText = sText;

lastLocateIndex = offset;

}

}

// An instance of the private subclass of the default highlight painter

Highlighter.HighlightPainter myHighlightPainter =new MyHighlightPainter(Color.red);

// A private subclass of the default highlight painter

class MyHighlightPainterextends DefaultHighlighter.DefaultHighlightPainter{

public MyHighlightPainter(Color color){

super(color);

}

}

The last few lines are from an example i found on the internet.Am I doing something wrong?

Thanks in advance.

[4597 byte] By [sergio.bobilliera] at [2007-11-27 6:05:16]
# 1

[nobr]First of all you don't need to create a class to create a highlighter painter. You just use:

new DefaultHighlighter.DefaultHighlightPainter( Color.red );

Your code isn't executable so I don't know why it doesn't work, so post executable demo code the next time so we can see what you are doing. Here is a simple example. Highlight some text and then click the button to make the highlighting permanent. Seems to work fine the only thing to note is that the offset is off by one. When you highlight the first line I would expect the starting offset to be 0, but instead it appears to be 1.

import java.awt.*;

import java.awt.event.*;

import javax.swing.*;

import javax.swing.text.*;

public class EditorPaneHighlight extends JFrame

{

JEditorPane htmlPane;

public EditorPaneHighlight()

throws Exception

{

String text = "<html>one<br>two<br>three<br>four<br>five<br>six</html>";

htmlPane = new JEditorPane("text/html", text);

JScrollPane scrollPane = new JScrollPane( htmlPane );

scrollPane.setPreferredSize( new Dimension(200, 100) );

getContentPane().add( scrollPane );

JButton selected = new JButton("Highlight Selected Text");

selected.addActionListener( new ActionListener()

{

public void actionPerformed(ActionEvent e)

{

try

{

int start = htmlPane.getSelectionStart();

int end = htmlPane.getSelectionEnd();

System.out.println(start + " : " + end);

htmlPane.getHighlighter().addHighlight(start, end, DefaultHighlighter.DefaultPainter);

}

catch(BadLocationException ble) {}

}

});

getContentPane().add(selected, BorderLayout.SOUTH);

}

public static void main(String[] args)

throws Exception

{

EditorPaneHighlight frame = new EditorPaneHighlight();

frame.setDefaultCloseOperation( EXIT_ON_CLOSE );

frame.pack();

frame.setLocationRelativeTo( null );

frame.setVisible(true);

}

}

[/nobr]

camickra at 2007-7-12 16:50:57 > top of Java-index,Desktop,Core GUI APIs...
# 2

Thanks for your answer it was really useful. I was mistaken, I thought that the second argument for addHighlight was the length of the text that you wanted to select but it抯 the end position, so that was the problem.

Well anyway thanks for helping me realizing that error.

Sincerely

Sergio.

sergio.bobilliera at 2007-7-12 16:50:57 > top of Java-index,Desktop,Core GUI APIs...