JButton : Java Glossary

go to home page J words local find full screen, hide local find menu Google search web for more information on this topic jump to foot of page translate this page with Babelfish 2008-02-24 by Roedy Green ©1996-2008 Canadian Mind Products
index page for letter ⇒ punctuation 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 (all)
JButton
labeled push-button e.g. an OK button. In AWT you use the Button class and in Swing the JButton.
Introduction Simulating a Button Click
Creating and Configuring a JButton Gotchas
JButton.setActionCommand Learning More
Hitting Enter Links
Focus Box

Introduction

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

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 );

Simulating a Button Click

Here are four methods so simulate the user clicking a JButton.
  1. Invoke the same appication method your JButton’s ActionListener does. This is the simplest if you know at compile time what the method is.
  2. Use JButton.doClick().
  3. Use Robot.keyPress.
  4. Create a synthetic event and enqueue it.

Gotchas

Learning More

Sun’s Javadoc on the JButton class : available:
Sun’s Javadoc on Robot.keyPress : available:


CMP homejump to top
CMP logo
feedback Please email your feedback for publication, errors, omissions, broken/redirected link reports
and suggestions to improve this page to Roedy Green : feedback email
made with CSS
HTML Checked!
ICRA ratings logo
mindprod.com IP:[65.110.21.43]
Your face IP:[38.103.63.61] The information on this page is for non-military use only.
You are visitor number 23,827. Military use includes use by defence contractors.
You can get a fresh copy of this page from: or possibly from your local J: drive (Java virtual drive/mindprod.com website mirror)
http://mindprod.com/jgloss/jbutton.html J:\mindprod\jgloss\jbutton.html