From what i understand you want your button in one class and the ActionListener in another.
I'm not 100% sure if this is what you want but here is some code for that:
Main class Btn:
package btn;
public class Btn
{
public static void main(String[] args)
{
Frame f = new Frame();
f.setVisible(true);
}
}
Button class Button:
package btn;
import javax.swing.JButton;
public class Button extends JButton
{
public Button()
{
super("Hello world");
this.addActionListener(new ActionListenerClass());
}
}
ActionListener class ActionListenerClass:
package btn;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
public class ActionListenerClass implements ActionListener
{
public static int numberOfClicks = 0;
@Override
public void actionPerformed(ActionEvent e)
{
numberOfClicks++;
System.out.println("HELLO WORLD!!\n" + "number of times pressed " + numberOfClicks);
}
}
Frame class Frame:
package btn;
import javax.swing.JFrame;
public class Frame extends JFrame
{
public Frame()
{
super("Hello world test");
this.setBounds(0, 0, 300, 500);
this.add(new Button());
this.setDefaultCloseOperation(Frame.EXIT_ON_CLOSE);
}
}
and the output:
run:
HELLO WORLD!!
number of times pressed 1
HELLO WORLD!!
number of times pressed 2
HELLO WORLD!!
number of times pressed 3
HELLO WORLD!!
number of times pressed 4
BUILD SUCCESSFUL (total time: 3 seconds)
What you are doing here is simply putting the action listener in a separate class and then telling the button to look for the action listener in that class
using this line of code: this.addActionListener(new ActionListenerClass());
Hope this helps, Luke.
EDIT:
try use e.paramString():
this will print something like this:
HELLO WORLD!!
number of times pressed 1
Button: ACTION_PERFORMED,cmd=Hello world,when=1399588160253,modifiers=Button1
BUILD SUCCESSFUL (total time: 2 seconds)
or e.getActionCommand():
this will print the button name:
run:
HELLO WORLD!!
number of times pressed 1
Button: Hello world
BUILD SUCCESSFUL (total time: 4 seconds)
Full actionListener code block:
@Override
public void actionPerformed(ActionEvent e)
{
numberOfClicks++;
System.out.println("HELLO WORLD!!\n" + "number of times pressed " + numberOfClicks +
"\ne.getActionCommand();: " + e.getActionCommand()
+ "\ne.paramString();: " + e.paramString());
}
output:
run:
HELLO WORLD!!
number of times pressed 1
e.getActionCommand();: Hello world
e.paramString();: ACTION_PERFORMED,cmd=Hello world,when=1399588455144,modifiers=Button1
BUILD SUCCESSFUL (total time: 6 seconds)
SECOND EDIT:
Ok use the getActionCommand();
method and then use a switch stricture to check witch button was pressed.
i have added another button, one called "LOL" and the other called "Hello world" both use the same action listener.
the output from pressing both:
run:
HELLO WORLD!!
number of times pressed 1
e.getActionCommand();: LOL
e.paramString();: ACTION_PERFORMED,cmd=LOL,when=1399590104631,modifiers=Button1
HELLO WORLD!!
number of times pressed 2
e.getActionCommand();: LOL
e.paramString();: ACTION_PERFORMED,cmd=LOL,when=1399590106679,modifiers=Button1
HELLO WORLD!!
number of times pressed 3
e.getActionCommand();: Hello world
e.paramString();: ACTION_PERFORMED,cmd=Hello world,when=1399590107665,modifiers=Button1
HELLO WORLD!!
number of times pressed 4
e.getActionCommand();: Hello world
e.paramString();: ACTION_PERFORMED,cmd=Hello world,when=1399590107780,modifiers=Button1
BUILD SUCCESSFUL (total time: 5 seconds)
now use a switch stricture to tell the difference between the buttons:
@Override
public void actionPerformed(ActionEvent e)
{
numberOfClicks++;
System.out.println("HELLO WORLD!!\n" + "number of times pressed " + numberOfClicks +
"\ne.getActionCommand();: " + e.getActionCommand()
+ "\ne.paramString();: " + e.paramString());
switch(e.getActionCommand())
{
case "LOL":
System.out.println("Button \"LOL\" was clicked");
break;
case "Hello world":
System.out.println("Button \"Hello world\" was clicked");
break;
}
}
output with switch:
run:
HELLO WORLD!!
number of times pressed 1
e.getActionCommand();: Hello world
e.paramString();: ACTION_PERFORMED,cmd=Hello world,when=1399590324792,modifiers=Button1
Button "Hello world" was clicked
HELLO WORLD!!
number of times pressed 2
e.getActionCommand();: Hello world
e.paramString();: ACTION_PERFORMED,cmd=Hello world,when=1399590324943,modifiers=Button1
Button "Hello world" was clicked
HELLO WORLD!!
number of times pressed 3
e.getActionCommand();: Hello world
e.paramString();: ACTION_PERFORMED,cmd=Hello world,when=1399590325089,modifiers=Button1
Button "Hello world" was clicked
HELLO WORLD!!
number of times pressed 4
e.getActionCommand();: LOL
e.paramString();: ACTION_PERFORMED,cmd=LOL,when=1399590330897,modifiers=Button1
Button "LOL" was clicked
HELLO WORLD!!
number of times pressed 5
e.getActionCommand();: LOL
e.paramString();: ACTION_PERFORMED,cmd=LOL,when=1399590331048,modifiers=Button1
Button "LOL" was clicked
BUILD SUCCESSFUL (total time: 11 seconds)
I hope this answers your question.