Java Glossary : JButton

CMP home Java glossary home Menu no menu Last updated 2004-06-28 by Roedy Green ©1996-2004 Canadian Mind Products

Java definitions: 0-9 A B C D E F G H I J K L M N O P Q R S T U V W X Y Z

You are here : home : Java Glossary : J words : JButton.

JButton
labeled push-button e.g. an OK button. In AWT you use the Button class and in Swing the JButton.

Putting \n in the Button's label text won't give you a multiline label. You must use a javax.swing.JButton. You feed it HTML containing a <br>. Similarly if you want to decorate your buttons with images, or you want non-rectangular buttons, you must use JButton.

Even when you use a round button image, it still lives on a rectangular transparent background. This means if the user clicks in the transparent corners, it still counts as clicking the button. It also counts for the rollover button highlighting.

If you did a setLayout ( null ); you could absolutely position your buttons so they overlapped slightly in the transparent corners. This would let you pack the buttons more closely than the surrounding rectangles would naturally allow.

Creating and Configuring a JButton


view

Typical createImage Method


view

JButton.setActionCommand

The JButton.setActionCommand does not set up an ActionListener. It simply records a string you may retrieve later with getActionCommand. It just helps you remember what to do in the ActionListener when that button is pressed.

Hitting Enter

Windows users are used to hitting Enter instead of clicking a button, if the button has focus. This does not always work. If you are using the Metal look and feel, you need to press space instead.

Focus Box

Swing paints a thin box on a button if it has focus. You can suppress that with:

theButton.setFocusPainted( false );


CMP logo
CMP_home
home
Canadian Mind Products CSS
HTML Checked!
ICRA ratings logo
mindprod.com IP:[24.87.56.253]
Your IP:[80.134.30.163]
You are visitor number 1118.
Please send errors, omissions and suggestions
to improve this page to Roedy Green.
You can get a fresh copy of this page from: or possibly from your local J: drive mirror:
http://mindprod.com/jgloss/jbutton.html J:\mindprod\jgloss\jbutton.html