473,411 Members | 2,019 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,411 software developers and data experts.

ActionListener

176 100+
Hello guys. How do you create an ActionListener. I am confused on how to do it.
Here is my code:
Expand|Select|Wrap|Line Numbers
  1. /**
  2.  * Written By: Edward Sanger
  3.  * Coded in: Java
  4.  * IDE Used: IntelliJ IDEA
  5.  * Program Name: Calculator
  6.  * Program Description: This program will create a GUI window with 4 buttons for 4 mathematical operators and 2 text
  7.  * fields for the numbers to add, subtract, multiply, and divide.
  8.  * Date: May 26, 2008
  9.  * Time: 2:34:52 PM
  10.  */
  11.  
  12. //import declarations
  13. import java.awt.*;
  14. import javax.swing.*;
  15.  
  16. public class Calculator implements ActionListener {
  17.     public static void main(String[] args) {
  18.         //create a new jframe
  19.         JFrame frame = new JFrame();
  20.         frame.setTitle("Calculator");
  21.         //create a container
  22.         Container contentPane = frame.getContentPane();
  23.  
  24.         //create the frame objects
  25.         JTextField num1 = new JTextField("Number 1");
  26.         JTextField num2 = new JTextField("Number 2");
  27.  
  28.         JButton add = new JButton("Add");
  29.         JButton subtract = new JButton("Subtract");
  30.         JButton multiply = new JButton("Multiply");
  31.         JButton divide = new JButton("Subtract");
  32.  
  33.         JLabel result = new JLabel("Result");
  34.  
  35.         //add the objects
  36.         contentPane.add(num1);
  37.         contentPane.add(num2);
  38.  
  39.         contentPane.add(add);
  40.         contentPane.add(subtract);
  41.         contentPane.add(multiply);
  42.         contentPane.add(divide);
  43.  
  44.         contentPane.add(result);
  45.  
  46.         //create the layout
  47.         FlowLayout layout = new FlowLayout();
  48.         contentPane.setLayout(layout);
  49.  
  50.         //create an action listener
  51.  
  52.  
  53.         //frame settings
  54.         frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  55.         frame.pack();
  56.         frame.setVisible(true);
  57.     }
  58. }
  59.  
May 26 '08 #1
4 1982
BigDaddyLH
1,216 Expert 1GB
Did you look in the new tutorial?

http://java.sun.com/docs/books/tutor...nlistener.html
May 26 '08 #2
Kid Programmer
176 100+
Did you look in the new tutorial?

http://java.sun.com/docs/books/tutor...nlistener.html
2 questions.

1. What goes in the parentheses in this line of code
Expand|Select|Wrap|Line Numbers
  1. b.addActionListener(this);
2. In my code I have four buttons. How do I create a seperate event for each one. It just says in the tutorial:
Expand|Select|Wrap|Line Numbers
  1. public void actionPerformed(ActionEvent e) {
May 27 '08 #3
BigDaddyLH
1,216 Expert 1GB
2 questions.

1. What goes in the parentheses in this line of code
Expand|Select|Wrap|Line Numbers
  1. b.addActionListener(this);
2. In my code I have four buttons. How do I create a seperate event for each one. It just says in the tutorial:
Expand|Select|Wrap|Line Numbers
  1. public void actionPerformed(ActionEvent e) {
1. To find out how to use a method, look it up in the API:

void addActionListener(ActionListener listener)

So you pass addActionListener a reference to an ActionListener.

2. Separate events? The buttons create the events. I think you mean "separate event listeners". You create separate listeners by creating separate listeners. I think you are confused about something, because that question is like asking "how can I define four classes" or "how can I eat four apples". You go ahead and do it. Here's a quick demo:

Expand|Select|Wrap|Line Numbers
  1. import java.awt.*;
  2. import java.awt.event.*;
  3. import javax.swing.*;
  4.  
  5. class Listener1 implements ActionListener {
  6.     public void actionPerformed(ActionEvent evt) {
  7.         System.out.println("Listener1");
  8.     }
  9. }
  10.  
  11. class Listener2 implements ActionListener {
  12.     public void actionPerformed(ActionEvent evt) {
  13.         System.out.println("Listener2");
  14.     }
  15. }
  16.  
  17. class Listener3 implements ActionListener {
  18.     public void actionPerformed(ActionEvent evt) {
  19.         System.out.println("Listener3");
  20.     }
  21. }
  22.  
  23. class Listener4 implements ActionListener {
  24.     public void actionPerformed(ActionEvent evt) {
  25.         System.out.println("Listener4");
  26.     }
  27. }
  28.  
  29. public class FourListenersExample {
  30.     public static void main(String[] args) {
  31.         EventQueue.invokeLater(new Runnable(){
  32.             public void run() {
  33.                 launch();
  34.             }
  35.         });
  36.     }
  37.  
  38.     static void launch() {
  39.         JPanel cp = new JPanel();
  40.         configure(cp, "button 1", new Listener1());
  41.         configure(cp, "button 2", new Listener2());
  42.         configure(cp, "button 3", new Listener3());
  43.         configure(cp, "button 4", new Listener4());
  44.  
  45.         JFrame f = new JFrame("FourListenersExample");
  46.         f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  47.         f.setContentPane(cp);
  48.         f.pack();
  49.         f.setLocationRelativeTo(null);
  50.         f.setVisible(true);
  51.     }
  52.  
  53.     static void configure(JPanel cp, String text, ActionListener al) {
  54.         JButton btn = new JButton(text);
  55.         btn.addActionListener(al);
  56.         cp.add(btn);
  57.     }
  58. }
May 27 '08 #4
Kid Programmer
176 100+
1. To find out how to use a method, look it up in the API:

void addActionListener(ActionListener listener)

So you pass addActionListener a reference to an ActionListener.

2. Separate events? The buttons create the events. I think you mean "separate event listeners". You create separate listeners by creating separate listeners. I think you are confused about something, because that question is like asking "how can I define four classes" or "how can I eat four apples". You go ahead and do it. Here's a quick demo:

Expand|Select|Wrap|Line Numbers
  1. import java.awt.*;
  2. import java.awt.event.*;
  3. import javax.swing.*;
  4.  
  5. class Listener1 implements ActionListener {
  6.     public void actionPerformed(ActionEvent evt) {
  7.         System.out.println("Listener1");
  8.     }
  9. }
  10.  
  11. class Listener2 implements ActionListener {
  12.     public void actionPerformed(ActionEvent evt) {
  13.         System.out.println("Listener2");
  14.     }
  15. }
  16.  
  17. class Listener3 implements ActionListener {
  18.     public void actionPerformed(ActionEvent evt) {
  19.         System.out.println("Listener3");
  20.     }
  21. }
  22.  
  23. class Listener4 implements ActionListener {
  24.     public void actionPerformed(ActionEvent evt) {
  25.         System.out.println("Listener4");
  26.     }
  27. }
  28.  
  29. public class FourListenersExample {
  30.     public static void main(String[] args) {
  31.         EventQueue.invokeLater(new Runnable(){
  32.             public void run() {
  33.                 launch();
  34.             }
  35.         });
  36.     }
  37.  
  38.     static void launch() {
  39.         JPanel cp = new JPanel();
  40.         configure(cp, "button 1", new Listener1());
  41.         configure(cp, "button 2", new Listener2());
  42.         configure(cp, "button 3", new Listener3());
  43.         configure(cp, "button 4", new Listener4());
  44.  
  45.         JFrame f = new JFrame("FourListenersExample");
  46.         f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  47.         f.setContentPane(cp);
  48.         f.pack();
  49.         f.setLocationRelativeTo(null);
  50.         f.setVisible(true);
  51.     }
  52.  
  53.     static void configure(JPanel cp, String text, ActionListener al) {
  54.         JButton btn = new JButton(text);
  55.         btn.addActionListener(al);
  56.         cp.add(btn);
  57.     }
  58. }
I am getting errors in my code for the methods. It says Identifier expected.
May 27 '08 #5

Sign in to post your reply or Sign up for a free account.

Similar topics

2
by: Hal Vaughan | last post by:
I have a "wizard" type program with a series of panels that are placed in a JPanel. When the "Next >" button is pressed, I put in a new panel (in other words, the ActionListener for "Next >"...
1
by: Patrick Vanhoof | last post by:
Hi, I have a small program to test the ActionListener, but it gives errors. Here's part of the code: class ButtonListener implements ActionListener { public void actionPerformed(ActionEvent...
0
by: linux newbie | last post by:
is there any java expert could tell me what the problem below and explain the code to me, your explanation is greatly appreciated :) static Class class$java$awt$event$ActionListener; ...
1
by: cnixuser | last post by:
Hello, I am currently attempting to implement a simple actionlistener for a button in a JFrame that was created via "drag and drop" with the Netbeans 5.0 IDE, the code that I am using to implement...
1
by: Phil Latio | last post by:
Quite a while back I used Java Swing (for Uni project) which allowed the developer to a create form and on the "Submit" button I recall adding something called an ActionListener. I was thinking of...
3
by: shailajaanand | last post by:
can we add Actionlistener to textarea?
3
by: stmfc | last post by:
KeyListener listens keyboard strokes, MouseListener listens mouse events, etc. My question is about ActionListener. what kind of events does ActionListener listen? its name does not give...
1
by: carlos123 | last post by:
if (bathroom.isSelected()){ bathroom is a jcheckbox, why doesnt that work.
4
Stubert
by: Stubert | last post by:
Hi there just a quick question, if I have multiple JMenuItems do I need to create an inner class ActionListener for each and every JMenuItem I create? Or is there a more efficient way of doing it? ...
7
by: Kid Programmer | last post by:
Hey guys. Once again I am making a calculator like I do so much. I ran into an error with this program though. I just couldn't figure out how to make the operators work. I want this calculator to...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.