-1

I need to keep focus on JTextField. Application uses Swing library. I need set focus on that field from time to time in order to avoid user mistakes that would change focus to other comonents. I suppose I need to use SwingWorker. Set focus is an operation on Swing component so it should be invoked in EDT. My question is how to write SwingWorker to do that? I know that method done() pass tasks to be invoked in EDT but I need this task to be invoked every let's sey 2 seconds. Method done() is called one time. So maybe sth like this will be ok?

public class myWorker extends SwingWorker<Void, Void> {

@Override
protected Void doInBackground() throws Exception {
SwingUtilities.invokeLater(new Runnable() {
  @Override
  public void run() {
    //here set focus on JTextField
    return null;
  }
});
}}

Edit:

I noticed that method process() that is a part of SwingWorker may be appropriate beacuse it is invoked in EDT. I'm not sure but this method is probably invoked always when I call publish() metod. So could you tell me if this code is valid to do this task?

private class KeepFocusWorker extends SwingWorker<Void, Void>
{
    @Override
    protected Void doInBackground() throws Exception
    {
        while(true)
        {
            publish();
        }
    }

    @Override
    protected void process(List<Void> chunks)
    {
        codeBar.requestFocusInWindow();
    }
}
user2374573
  • 59
  • 1
  • 3
  • 11
  • *"So maybe sth.."* ..Sith? South? Sloth? Type all letters of words rather than make people guess. – Andrew Thompson Jul 19 '14 at 17:54
  • 1
    Focus is a guide to the user. Rather than speculate about a bad solution, explain what you want to accomplish and what you've tried. See also [*How to Use Swing Timers*](http://docs.oracle.com/javase/tutorial/uiswing/misc/timer.html). – trashgod Jul 19 '14 at 17:56
  • Ok. I edited my post to be more clear. – user2374573 Jul 20 '14 at 13:48

2 Answers2

1

Use javax.swing.Timer instead of SwingWorker. In this case actionPerformed will be executed in EDT. Also to set focus in a component, you need to call requestFocus. As the name suggests, it is a request only and not guaranteed. So you may change you approach.

Timer timer = new Timer(2000, new ActionListener()
{
    @Override
    public void actionPerformed(ActionEvent e)
    {
        codeBar.requestFocus();
    }
});

timer.setRepeats(true);
timer.start();
spgodara
  • 984
  • 9
  • 10
  • Thanks for advice. It's good to know about this solution bu finally I decided this task should be done as frequently as possible(so rather Thread or SwingWorker). – user2374573 Jul 20 '14 at 14:00
  • 1
    `SwingWorker` is not for repeatative tasks. It is to execute time consuming tasks in worker thread. To execute code repeatedly in EDT, use `javax.swing.Timer`. I have updated code above for that. – spgodara Jul 24 '14 at 15:10
1

Surely it's better to limit the user's ability to take focus away from the textfield in the first place? Personally I don't see why it's an issue but I suppose it's better to keep focus in the one component rather than letting the user shift focus only for it to be shifted back every few seconds.

Therefore you could add a FocusListener to the component, override the focusLost method and basically requestFocus() again.

codeBar.addFocusListener(new FocusAdapter() {
    public void focusGained(FocusEvent e) {
    }
    public void focusLost(FocusEvent e) {
        codeBar.requestFocus();
    }
});

NB I've not actually tried this myself but can't see why it wouldn't work.

Alternatively you can use an InputVerifier which always returns false to prevent focus being taken away.

andyroberts
  • 3,458
  • 2
  • 37
  • 40
  • Thanks, for advice. The reason was that I wanted to stop that thread when the focus was required for only 2 other fields. I have a lot of textfields in my panel but focus is only acceptable in 2 of them. But as we know there is a lot of other methods to loose focus so thread would preserve from that. Finally I decided to use FocusListeners and set setFocusable(false) on any others component – user2374573 Jul 25 '14 at 14:39