Do the strings have any commonalities? If they do, you could try using a combination of the java.util.scanner or/and java.util.regex.Matcher. Make sure to get the right regex for what you need. Once you have found a string get the indexOf the first letter and set the caret position to it.
Set caret position on JScrollPane
Pergunta
I have a JEditoPane inside a JScrollPane. I have some text content that contain some pre-defined tokens. I'm storing the location of these tokens in the database. When I set the text content into the JEditorPane, I embed the tokens with HTML. I also add HTML break lines to format the content.
Now problem comes when I want to scroll to one of the highlighted tokens. It seems that the start position of the tokens, which I stored in database, do not match when using the setCaretPosition(int). I know it's probably because my content in JEditorPane Document is mixed with HTML.
So is there a way to search for a String in the JEditorPane content, then somehow get the caret position where the string was found?
Solução 2
Outras dicas
That's how you do it (ignore not using best practices ;) ) -
public static void main( String[] args ) {
final JEditorPane jEditorPane = new JEditorPane( "text/html", "<h1>This is some header</h1>After this text would be the CARRET<br>This is some more text<br>And more" );
final JScrollPane jScrollPane = new JScrollPane( jEditorPane );
final JFrame jframe = new JFrame( "HHHHHH" );
jframe.add( jScrollPane );
jframe.setSize( new Dimension( 200, 200 ) );
jframe.setVisible( true );
final String text = jEditorPane.getText();
final int index = text.indexOf( "T" );
jEditorPane.setCaretPosition( index + 1 );
while ( true ) {
try {
Thread.sleep( 1000000 );
} catch ( InterruptedException e ) {
e.printStackTrace();
}
}
}
And that's the result:
You should store the result of indexof
in the DB.