Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

java, swing, awt, remove focus from all objects

Tags:

java

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

like image 891
kralco626 Avatar asked Dec 09 '10 23:12

kralco626


People also ask

What does setFocusable do in Java?

You can use setFocusable(boolean n) , it´s mainly used to activate or deactivate the focus event (component of the graphical user interface that is selected to receive the input) of the view, both in the tactile / mouse mode, and in the keyboard (cursor) mode.

What is requestFocus in Java?

requestFocus() makes a request that the given Component gets set to a focused state. This method requires that the component is displayable, focusable, visible and have all it's ancestors be visible too.

What is a JTextField in Java?

JTextField is a lightweight component that allows the editing of a single line of text. For information on and examples of using text fields, see How to Use Text Fields in The Java Tutorial. JTextField is intended to be source-compatible with java.

What frame receives focus by default?

By default, every Frame and Dialog is focusable. Every Window which is not a Frame or Dialog, but whose nearest owning Frame or Dialog is showing on the screen, and which has at least one Component in its focus traversal cycle, is also focusable by default.


2 Answers

button.setFocusable(false);
like image 150
camickr Avatar answered Oct 27 '22 22:10

camickr


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.

like image 43
brimborium Avatar answered Oct 27 '22 22:10

brimborium