Default button

Default button

How can I implement a “default” button in a window?

You can explicitly request the focus using requestFocus(), but thebutton can still lose the focus. Therefore you have to extend yourcontaining component and trap all keyboard events, passing them toyour default button. At least that’s what you had to do before theSwing classes came along. If you are using the Swing classes (highlyrecommended), all you have to do is call setDefaultButton() in JRootPaneto set the default button for a window. In other words, get the root pane of a JWindow with getRootPane(), and then call setDefaultButton()with your button as the argument.


Share the Post: