Refreshing highlighting and sleeping
Hello, i need some help please.
I have a JFrame with one textBox (or JTextField or how is it called in java) and with one text pane and one button. User should enter search pattern to textbox and textpane contains searched text. I am trying to show how some String searching alghoritms work. When user clicks button, search alghorithm begins to execute. Inside of algorithm method at some points i use highlighter to highlight some parts of the text, then call thread.sleep(), so user can see the highlighted text. After sleep ends, algorithm continues.
Problem is that it seems that jframe needs some kind of refresh, redraw, focus or something, because visible is only the last highlighet text and it becomes visible after end of executing of search alghorithm.
What should i do please?
[814 byte] By [
bobiseka] at [2007-11-27 4:54:47]

# 1
>
> What should i do please?
You need to learn how to write multi-threaded Swing applications. Here is a good place to start:
http://java.sun.com/products/jfc/tsc/articles/threads/threads1.html
In your case, you can run the search algorithm in a background thread. When you find a match you need to notify the UI which will then apply the highlighting. As explained in the article I linked to, there are certain rules you must follow when you need to update your UI from a separate thread. Once the UI has been updated, you can let the background thread sleep for a while before continuing the search.
# 2
Use SwingUtilities.invokeAndWait() calls to update your UI from a different thread.
E.g. you calculate something in your own thread and have to update a JLabel's text after some points. Call yourLabe.setText() in the invokeAndWait. It stops your thread and pass control to EDT to perform GUI update.
Regards,
Stas