actionListener for buttons
I am creating a simple program and I am unable to get the actionListeners to work. In my public static void main method I create some buttons and try to add action listeners. When I compile it says that I can not use the 'this' keyword.
JButton Ok = new JButton("Ok")
Ok.addActionListener(this) < this does not work?
[347 byte] By [
US101] at [2007-9-26 2:09:01]

what is "this" referring to? I think you may have a syntax issue. Here is how my classes look when I use actions:
public class SomeClass{
....
ActionListener listener = new YourClass();
JButton Ok = new JButton("Ok")
Ok.addActionListener(listener)
....
private class YourClass implements ActionListener{
//override the methods in the action listener interface
//so the functions do what you want them to do
}
}
Here is what my code looks like:
public class SomeClass{
....
public static void main (String args[]) {
JButton Ok = new JButton("Ok")
Ok.addActionListener(this)
}
public void actionPerformed(ActionEvent AE) {
...
}
The "this" is suspossed to mean in the current class.
When I compile the .java file I get an error message saying - "non static varible this cannot be referenced from a static context"
US101 at 2007-6-29 8:58:13 >

Also what does this mean in your code - //override the methods in the action listener interface//so the functions do what you want them to doI am new to java so could you explain a little more?Thank you.
US101 at 2007-6-29 8:58:13 >

modify your code as shown
public class SomeClass implemens ActionListener {
JButton Ok = new JButton("Ok")
Ok.addActionListener(this)
....
public static void main (String args[]) {
}
public void actionPerformed(ActionEvent AE) {
...
}
oh, SomeClass must be container for the buttons, eg extends JFrame
ActionListener class does not have any implementation, essentially it is a blank slate. The methods are there but they do not have any functionality to them yet. That is an interface so, then you have to override the function by
...
public YourClass implements ActionListener {
...
then you have to look at the function in the interface (ActionListener) and make some functions with the same name in your class (override teh methods.) So this would be in YourClass and it would override the interfaces method (which, like I said before, was purposly left empty.)
public void actionperformed(ActionEvent e){
//your code goes here
}
I get what you are saying, but one question.
Before I was using:
JFrame Window = new JFrame("TEST");
Window.getContentPane().setLayout(new BorderLayout());
etc.
Now should I do this?
public some class extends JFrame implements ActionListener
then what, how do I make Window?
US101 at 2007-6-29 8:58:13 >

Also the error you got is a tricky one at first... I think it is a common stumbling block. What went wropng there was that main is static meaning that it exists once the class is loaded, but the rest of the class does not exist until you explicity call it with a constructor. The static method exists across all of the copies of that object
meaning that there is one main method for ech of the obj variables below:
YourClass obj1 = new YourClass();
YourClass obj2 = new YourClass();
YourClass obj3 = new YourClass();
so the compiler complains becuase there is one main method for each of those objects. Esentially if the same main exists for all three of those objects which one would "this" be pointing to? That is why it had the error.
ok this one is a little trickier cause there are so many way to do it.
It is probably best to have
public YourClass extends JFrame{
JFrame Window = new JFrame("TEST");
Window.getContentPane().setLayout(new BorderLayout());
Action listener = new YourActionListener();
// this private class is inside of YourClass
private class YourActionListener implements ActionListener {
//overide the methods here
// adn you don't need to worry about making any
//constructors for this class
}
}//end of YourClass
now this class will set up the frame so that it can be viewed, BUT it can't make itself viewable. You'll need yet another class to construct the class then use the inherited JFrame method of show()
it coudl be as simple as
public class LanucherClass{
public static void main(String[] args){
YourClass yc = new YourClass();
yc.show();
}
}
and that should display what you've made... I hope that this helps.
I am still getting errors. Here is my code.
public class TEST extends JFrame{
//Create Window with title
JFrame Window = new JFrame("TEST");
//Set Layout to BorderLayout (North,South,East,West,Center)
Window.getContentPane().setLayout(new BorderLayout());
//Set Action to do when the X is clicked
Window.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
//Create ActionListener class
Action listener = new ActionHandle();
// this private class is inside of YourClass
private class ActionHandle implements ActionListener {
public void actionPerformed(ActionEvent AE) {
String Command = AE.getActionCommand();
if (Command == "Split") {
//CmdSplit was hit
} else if (Command == "...") {
//CmdBrowse was hit
}
}
}
}
US101 at 2007-6-29 8:58:14 >

try something like this:
import javax.swing.*;
import java.awt.event.*;
import java.awt.*;
public class FrameButt extends JFrame
{
public FrameButt()
{
getContentPane().add(new ButtPan());
pack();
show();
}
public static void main(String args[])
{
(new FrameButt());
}
}
class ButtPan extends JPanel implements ActionListener
{
Color bColor;
JButton jButt;
public ButtPan()
{
bColor = color.black;
setLayout(new FlowLayout());
setBackground(Color.blue);
jButt = new JButton("JBUTT");
jButt.setBackground(bColor);
jButt.addActionListener(this);
add(jButt);
}
public void actionPerformed(ActionEvent e)
{
bColor = color.white;
jButt.repaint();
}
}
I believe Subclassing is always best. It makes the code cleaner and more reusable.
Hope this helps,
IanMechura
IT WORKS!!! Thank you!
US101 at 2007-6-29 8:58:14 >
