![]() |
![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]()
![]() |

|
| Programming tutorials All Knowledge Info and links to posted here |
![]() |
|
Java: Button Press - Action Event
|
LinkBack | Thread Tools | Display Modes |
|
|
#1 (permalink) |
|
Administrator
Posts: 876
Join Date: Oct 2005
Rep Power: 10
IM:
|
The ActionEvent is a class called when an action is performed on items in your GUI. As a simple example we will handle the event on a button press, the code will only output text, but you can add any code you want. CODE: import java.io.*; import java.awt.*; import java.util.*; import java.applet.*; import javax.swing.*; import java.awt.event.*; import javax.swing.event.*; public class AE extends JFrame{ static final int WIDTH = 500; static final int HEIGHT = 100; JButton b; B button; public class display extends JPanel{ public display(){ Container container; GridBagLayout layout = new GridBagLayout(); GridBagConstraints layoutConstraints = new GridBagConstraints(); getContentPane().setLayout(layout); container = getContentPane(); b = new JButton("Press!"); layoutConstraints.gridx = 0; layoutConstraints.gridy = 0; layoutConstraints.gridwidth = 1; layoutConstraints.gridheight = 1; layoutConstraints.fill = GridBagConstraints.BOTH; layoutConstraints.insets = new Insets(1,1,1,1); layoutConstraints.anchor = GridBagConstraints.CENTER; layoutConstraints.weightx = 1.0; layoutConstraints.weighty = 1.0; layout.setConstraints(b, layoutConstraints); container.add(b); } } public AE(){ JFrame j = new JFrame(); j.getContentPane().add(new display()); button = new B(); b.addActionListener(button); } private class B implements ActionListener{ public void actionPerformed(ActionEvent e){ System.out.println("Button Pressed"); } } public static void main(String args[]){ AE a = new AE(); a.setSize(WIDTH,HEIGHT); a.setVisible(true); } } To reduce the creation of multiple instances of the press method, we create a class for it and assign it to button, thus this assignment is done once and then the single copy is called each time the button is pressed. We assign the action code with b.addActionListener(button); the class may have any name you like but the method name public void actionPerformed(ActionEvent e) is non-negotiable. All code within this method will be run when this particular action occurs. Ther are many action events for each GUI object, such as document listeners for TextAreas etc. Check the java documentation for a full list, i will post examples of a few more common ones as well. |
|
|
|
|
|
|
![]() |
| Currently Active Users Viewing This Thread: 1 (0 members and 1 guests) | |
| Thread Tools | |
| Display Modes | |
|
|
Similar Threads
|
||||
| Thread | Thread Starter | Forum | Replies | Last Post |
| Landmark e-Crime Event Continues Unique Programme of Action for Welsh Business | newsprovider | Security News | 0 | 02-Jun-2008 08:44 AM |
| 4Pinball Set ball location: ------------------ Press the Left Mouse Button to pl | Cheaty | Game Cheats | 0 | 02-Mar-2008 12:35 AM |
| XP System getting Windows 2003 Error: Event Source: LSASRV Event Category: | Anilrgowda | Microsoft Windows xp error | 1 | 08-Mar-2007 09:40 PM |
| application shuts down: event view logs Event ID:1000 and 4097 | Anilrgowda | Microsoft Windows xp error | 1 | 15-Feb-2007 01:00 AM |
| The Javalog.txt file is created in the Windows\Java folder when Java logging is enabl | Anilrgowda | Microsoft windows vista error | 0 | 29-Jan-2007 10:09 AM |