0
public void highlightThisText(){

    int delay = 1000;
    ActionListener tp = new ActionListener(){

        public void actionPerformed(ActionEvent ae){
            try{
                System.out.println();
                if(name.equals("Correct")){
                    thisPanel.getHighlighter().addHighlight(startIndex, endIndex, hl);
                }
                else{
                    anotherPanel.getHighlighter().addHighlight(startIndex, endIndex, hl);
                }

            } catch (BadLocationException ex) {
                ex.printStackTrace();
            }
        }
    };
    Timer t = new Timer(delay, tp);
    t.setRepeats(false);
    t.start();      
}

I need my program to enter into the highlightThisText method, executed the timer code, and after the delay leave the method. I understand that I cannot use Thread.sleep(1000); as this will block the EDT, but I cannot find any other examples on here with a similar problem. startIndex and endIndex will be incremented after the method is left which means the the correct line will not be highlighted.

Any help will be highly appreciated.

Tommot15
  • 1
  • 1
  • 1
    You don't need to use a `Timer` if you want to wait. You could put `Thread.sleep(1000)` to have the same effect. But waiting is wrong anyways. You'll want to initiate the action that is supposed to happen after the task from the `actionPerfomed` method. – zapl Apr 18 '16 at 21:07
  • @zapl using 'Thread.sleep(1000)' causes the interface to crash as it's blocking the EDT right? And initiating the highlighting code outside of the 'actionPerformed' method doesn't wait for the timer to be up, it gets run straight away. – Tommot15 Apr 18 '16 at 21:19
  • 1
    Why? A better solution would be to pass in some callback to `highlightThisTest`, which when the timer ticks, you can then call. If you "really" can't see a way around this, then you might consider using something like [FoxTrot](http://foxtrot.sourceforge.net/docs/introduction.php), in 16 years I've only ever had one instance where it was simply impossible to design a solution around the problem, which FoxTrot helped solved – MadProgrammer Apr 18 '16 at 21:44
  • @MadProgrammer I'm unsure what you mean by pass in a callback to the method, could you please elaborate? I'd prefer not to use a 3rd party API also, but thanks for the suggestion. – Tommot15 Apr 18 '16 at 22:02
  • You're `ActionListener` is a callback, you pass an instance of it to your `Timer` and the `Timer` and some point calls the `actionPerformed` method. this is an example of an [Observer Pattern](http://www.oodesign.com/observer-pattern.html) – MadProgrammer Apr 18 '16 at 22:44
  • @MadProgrammer applying the 'ActionListener' to the timer waits for the timer to finish and then runs the If Then statements. But it leaves the highlightThisText method straight away. Is there anyway to wait until the 'ActionListener' finished its action and then proceed? such as 'while(!tp.isDone()){ }'. I have also tried implementing a swing worker this way, and used a while loop to check whether the worker has finished, but this stops the EDT as well. – Tommot15 Apr 19 '16 at 10:11
  • 1
    You need to change the way you're thinking. What ever functionality you want to execute "after" the `highlightThisText` method needs to called from the `Timer` – MadProgrammer Apr 19 '16 at 10:48
  • @MadProgrammer I think I do, the only problem is that it's called during a recursive process and the values for index's in the highlight change, meaning that once the code is called, it won't be highlighting the correct line. A big rethink is in order i'm guessing. – Tommot15 Apr 19 '16 at 10:55

0 Answers0