6

I'm doing this project for school and for some reason one of by Buttons in one of my Panels has focus(i can change it with the tab button) Well whatever button has the focus is acting weird.

Is there a way that I can have no button have focus? ie. have nothing selected by the tab button?

notice the Rectangle button has a dotted line around it. I want to make that go away.

Thanks! alt text

kralco626
  • 8,456
  • 38
  • 112
  • 169

4 Answers4

14
button.setFocusable(false);
camickr
  • 321,443
  • 19
  • 166
  • 288
  • nice! is there any way to make everything on the frame 'setFocusable(false)' other than applying that to every button? – kralco626 Dec 10 '10 at 00:00
6

If you just want it to not show the dotted line, you can also use

button.setFocusPainted(false);

The button will still be focusable, but you won't see the dotted line.

brimborium
  • 9,362
  • 9
  • 48
  • 76
2

If you don't want anything to get focus, you can use:

button.getRootPane().requestFocus();

Using setFocusable is going to move focus to the next focusable component.

0
public void removeFocusFromAllObjects(Container container) {
    container.setFocusable(false);
    for (Component child : container.getComponents()) {
        if (child instanceof Container) {
            removeFocusFromAllObjects((Container) child);
        } else {
            child.setFocusable(false);
        }
    }
}

I have written above code to remove focus from all the components recursively inside a parent component. Hope it might be useful to someone visiting this post.

Yogesh Kumar Gupta
  • 1,009
  • 1
  • 10
  • 10