how make action event with jButton, without using ActionListener??
Welcome to the Java Programming Forums
The professional, friendly Java community. 21,500 members and growing!
The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.
>> REGISTER NOW TO START POSTING
Members have full access to the forums. Advertisements are removed for registered users.
how make action event with jButton, without using ActionListener??
I think a good question would be why do you want to avoid the ActionListener?
Rolf
Programs can, but typically don't *make* events - they just happen. If you want to make them, you invoke their constructor, like any other class. This doesn't involve an event listener.
If that doesn't answer what's on your mind, perhaps you could describe what it is you want to do by creating these events without using listeners.
Post code. Not the whole thing but showing the button and how you're writing the listener.
here sir....
import javax.swing.*; import java.awt.*; import java.awt.event.*; public class Example { public Example() { int x = 0; JButton button = new JButton("Add"); button.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { x = x + 1; } } ); } }
--- Update ---
please copy .... and try to run it
--- Update ---
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
public class Example
{
public Example()
{
int x = 0;
JButton button = new JButton("Add");
button.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
x = x + 1;
}
}
);
}
}
Last edited by pbrockway2; May 1st, 2013 at 12:48 AM. Reason: code tags added
Declare x as an instance variable of the Example class not a local variable of the Example constructor.
import javax.swing.*; import java.awt.*; import java.awt.event.*; public class Example { private int x; public Example() { //etc
Local variables have to be final. And final variables can't change their value. It's a Java thing... you simply can't declare a variable to have scope you mean the way you can in JavaScript.
--- Update ---
The update looks good. Of course you have to add the button to a gui, and actually do something with the updated value of x. I'm going offline now, but post if you get stuck, someone is sure to help.
(there are no sirs here. We're comrades, colleagues, citizens...)