Sei sulla pagina 1di 2
Swing introduction Swing components Events and listeners Text fields/labels Layouts JPanel and Box Search Comment
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

JButton A JButton has generally a so-called represents ActionListener a button that, attached when to clicked it, which by the in effect user, defines carries out the a task particular to be performed action. A when the action button listener is clicked. for each There button are in generally a given window, two patterns dialog for box using etc. a The JButton. second The is to first use is a to single create action an individual listener

for all the buttonspossibly even making the window/dialog itself be the ActionListener. The first pattern usually looks as follows:

butt.addActionListener(new JButton public // butt void called = actionPerformed(ActionEvent new when JButton("Click button ActionListener() clicked me!"); evt) { {

}

});

Using from outside this first callers. pattern At has its simplest, the advantage the second that the pattern grubby looks details something of the button like this: click code is completely hidden

public private class JButton MyFrame button1 extends = new JFrame JButton("Click implements ActionListener me!"); { private JButton button2 = new JButton("Click me too!"); public button1.addActionListener(this); button2.addActionListener(this); add MyFrame() buttons { to frame

}

public Object void src actionPerformed(ActionEvent = evt.getSource(); evt) {

} if else (src perform if == (src button1) action == button2) { for button { 1 perform action for button 2

}

}

}

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

Action commands

action defines In some commands the cases, type you of action can might occasionally that have that several button make buttons this is to easier. perform that you In when this want pattern, clicked. to perform you The the attach pattern same a looks string action. as to A follows: each facility button called that

public private class JButton MyFrame button1 extends = new JFrame JButton("Exit"); implements ActionListener { private private JButton JButton button2 button3 = = new new JButton("Cancel"); JButton("Also Exit"); public button1.setActionCommand("EXIT"); button2.setActionCommand("CANCEL"); button3.setActionCommand("EXIT"); MyFrame() { button1.addActionListener(this); button2.addActionListener(this); button3.addActionListener(this); add buttons to frame

}

public String void cmd actionPerformed(ActionEvent = evt.getActionCommand(); evt) {

if (cmd == "EXIT") { } else exit if (src == "CANCEL") {

}

}

}

cancel

In this case, then, buttons 1 and 3 will perform the same action. Notice in the actionPerformed() method that should this is one compare of the strings few cases with when equals() we can (or break equalsIgnoreCase() the golden rule etc). about In string this particular comparison. instance, Normally, it so happens you 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 with the same works content like this: will whenever actually end you up write being a constant the selfsame string String in your object. program, So although all of those we constant write "EXIT" strings 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.