Sei sulla pagina 1di 2

Java tutorials Java Servlets

Swing introduction

Swing components

Events and listeners

Text fields/labels

Layouts

JPanel and Box

Search

Comment on this article

List of Java topics

Using JButton
A JButton generally represents a button that, when clicked by the user, carries out a particular action. A JButton has a so-called ActionListener attached to it, which in effect defines the task to be performed when the button is clicked. There are generally two patterns for using a JButton. The first is to create an individual action listener for each button in a given window, dialog box etc. The second is to use a single action listener for all the buttons possibly even making the window/dialog itself be the ActionListener. The first pattern usually looks as follows: JButton butt = new JButton("Click me!"); butt.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent evt) { // ... called when button clicked } });

Using this first pattern has the advantage that the grubby details of the button click code is completely hidden from outside callers. At its simplest, the second pattern looks something like this: public class MyFrame extends JFrame implements ActionListener { private JButton button1 = new JButton("Click me!"); private JButton button2 = new JButton("Click me too!"); public MyFrame() { button1.addActionListener(this); button2.addActionListener(this); ... add buttons to frame ... } public void actionPerformed(ActionEvent evt) { Object src = evt.getSource(); if (src == button1) { ... perform action for button 1 } else if (src == button2) { ... perform action for button 2 } } }

This pattern can obviously be a lot more succinct when you have several buttons in your window that each perform simple tasks.

Action commands
In some cases, you might have several buttons that you want to perform the same action. A facility called action commands can occasionally make this easier. In this pattern, you attach a string to each button that defines the type of action that that button is to perform when clicked. The pattern looks as follows: public class MyFrame extends JFrame implements ActionListener { private JButton button1 = new JButton("Exit"); private JButton button2 = new JButton("Cancel"); private JButton button3 = new JButton("Also Exit"); public MyFrame() { button1.setActionCommand("EXIT"); button2.setActionCommand("CANCEL"); button3.setActionCommand("EXIT"); button1.addActionListener(this); button2.addActionListener(this); button3.addActionListener(this); ... add buttons to frame ... } public void actionPerformed(ActionEvent evt) { String cmd = evt.getActionCommand(); if (cmd == "EXIT") { ... exit } else if (src == "CANCEL") {

... cancel } } }

In this case, then, buttons 1 and 3 will perform the same action. Notice in the actionPerformed() method that this is one of the few cases when we can break the golden rule about string comparison. Normally, you should compare strings with equals() (or equalsIgnoreCase() etc). In this particular instance, it so happens that we know that Swing will give us back the exact same String object that we passed to setActionCommand (). And we know that that object is a pooled String constant. (If you're not familiar with this concept, then it basically works like this: whenever you write a constant string in your program, all of those constant strings with the same content will actually end up being the selfsame String object. So although we write "EXIT" twice, there's actually only one String object created with the text EXIT.)

Unless otherwise stated, the Java programming articles and tutorials on this site are written by Neil Coffey. Suggestions are always welcome if you wish to suggest topics for Java tutorials or programming articles, or if you simply have a programming question that you would like to see answered on this site. Most topics will be considered. But in particular, the site aims to provide tutorials and information on topics that aren't well covered elsewhere, or on Java performance information that is poorly described or understood. Suggestions may be made via the Javamex blog (see the site's front page for details). Copyright Javamex UK 2009. All rights reserved.

Potrebbero piacerti anche