JButton : Java Glossary

JButton
labeled push-button e.g. An OK button. In AWT (Advanced Windowing Toolkit) 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 (Hypertext Markup Language) 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. It helps you sort of which button was pressed, and just what you are supposed to do about it. Alternatively, you can hide information with setName, though I normally use that for debugging field names, automatic label generation, or prompting information. To hide binary data, put a 16-bit unsignd number in the first char of the String.

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

Oracle’s Javadoc on JButton class : available:
Oracle’s Javadoc on Robot.keyPress : available:


CMP homejump to top You can get the freshest 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
logo
Please email your , letters to the editor, errors, omissions, typos, formatting errors, ambiguities, unclear wording, broken/redirected link reports, suggestions to improve this page or comments to Roedy Green : feedback email. If you want your message, your name or email kept confidential, not considered for public posting, please explicitly specify that. Unless you state otherwise, I will treat your message as a letter to the editor that I may or may not publish in the feedback section. After that, it will be too late to retract it. If you disagree with something I said, please quote it and cite the web page where you found it, tell me why you think it is wrong, and, if possible, provide some supporting evidence. Threatening to kill me or spouting obscenities has yet to persuade me to change my mind.
mindprod.com IP:[65.110.21.43]
view BlogYour face IP:[38.107.179.214]
You are visitor number 48,183.