Re: ACTION LISTENER HANDLING
Hello fari,
Welcome to the Java Programming Forums :)
Please take a look at this link, im sure it will help.
http://www.javaprogrammingforums.com...ava-swing.html
Re: ACTION LISTENER HANDLING
Or instead of using anonymous classes you could define the classes A, B, etc to implement the ActionListener interface and put the actionPerformed() method in them.
Re: ACTION LISTENER HANDLING
And mind not using all caps? I know you need help but screaming isn't going to make it come any faster, as a matter of fact it might put people off
Re: ACTION LISTENER HANDLING
Code :
import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JTextField;
import java.awt.Component;
import java.awt.Container;
import javax.swing.JPanel;
import java.awt.*;
import java.awt.event.*;
import java.io.*;
import javax.swing.AbstractButton;
import java.util.*;
class NewClass1 extends JFrame implements ActionListener{
JTextField t;
JLabel l;
private JButton j, j1, j2;
JFrame f;
public NewClass1()
{
// JFrame f =new JFrame("faria");
JTextField t= new JTextField(15);
JLabel l=new JLabel("Label");
JButton j= new JButton("a");
j.addActionListener(this);
JButton j1= new JButton("b");
j1.addActionListener(this);
JButton j2= new JButton("c");
j2.addActionListener(this);
t.setText("5");
// l.setLayout(new FlowLayout());
// f.add(l);
// l.add(t);
// l.add(j);
// l.add(j1);
// l.add(j2);
setTitle("farris");
Container pane = getContentPane();
pane.add(l);
pane.add(t);
pane.add(j);
pane.add(j1);
pane.add(j2);
// j.addActionListener(new A());
// j1.addActionListener(new B());
// j2.addActionListener(new C());
pane.setLayout(new GridLayout(4,4));
setVisible(true);
setSize(400,400);
setDefaultCloseOperation(EXIT_ON_CLOSE);
}
public static void main(String args[]){
NewClass1 refVar = new NewClass1();
}
public void actionPerformed(ActionEvent e)
{
if (e.getActionCommand().equals("a"))
{
System.out.println("hiii" );
t.setText("hi");
}
else if (e.getActionCommand().equals("b"))
{
System.out.println("hello" );
t.setText("hello");
}
else if (e.getActionCommand().equals("c"))
{
System.out.println("how r u" );
t.setText("how r u");
}
}
}
Code should work but for mysterious NullPointerExceptions.
at NewClass1.actionPerformed(NewClass1.java:98)
which is
t.setText("how r u");
Re: ACTION LISTENER HANDLING
What is the scope of the t variable assigned a value in the Constructor?
Re: ACTION LISTENER HANDLING
What was scope again? I forgot.
Do I have to assign it coordinates or something?
Re: ACTION LISTENER HANDLING
What text book are you using?
I bet you could get a definition using Google.
Nothing to do with coordinates.
Re: ACTION LISTENER HANDLING
I think scope is whether or not Java has access to something, or can find it at least.
Why isn't it finding it?
I'm baffled.
Re: ACTION LISTENER HANDLING
Ok, it's not carrying over. Should I change it to 3 handlers?
Re: ACTION LISTENER HANDLING
Quote:
Why isn't it finding it
The compiler is finding it, otherwise there would be an error message about missing definition.
The one its using at that point in the code has a null value when the code is executed.
Where does your code give it a value? It Never does!!!
Re: ACTION LISTENER HANDLING
Quote:
Originally Posted by
Norm
The compiler is finding it, otherwise there would be an error message about missing definition.
The one its using at that point in the code has a null value when the code is executed.
Where does your code give it a value? It Never does!!!
What do you mean by "give it a value"?
Give what a value?
The button has a text.
Re: ACTION LISTENER HANDLING
Quote:
What do you mean by "give it a value"?
x = 4; // Give the value 4 to the variable x
In your program we were talking about the variable t.
Remember:
Quote:
mysterious NullPointerExceptions.
at NewClass1.actionPerformed(NewClass1.java:98)
which is
t.setText("how r u");
Re: ACTION LISTENER HANDLING
Quote:
Originally Posted by
Norm
x = 4; // Give the value 4 to the variable x
In your program we were talking about the variable t.
Remember:
t is a text field, not a variable.
Re: ACTION LISTENER HANDLING
Quote:
t is a text field, not a variable
Well not really. t is a variable that refers/points to a text field. It really is a pointer because you get the NullPointerException when you try to use it when its null. It is possible to have many variables point/refer to the same object. Many variables, one object.
Re: ACTION LISTENER HANDLING
Quote:
Originally Posted by
Norm
Well not really. t is a variable that refers/points to a text field. It really is a pointer because you get the NullPointerException when you try to use it when its null. It is possible to have many variables point/refer to the same object. Many variables, one object.
So I set it to four?
So really, what do I do?
8-|
Re: ACTION LISTENER HANDLING
The variable t must be given the address of a textfield. See how you did it in the Constructor.
Re: ACTION LISTENER HANDLING
It is a text field.
private JTextField t;
Constructor
{
t = new JTextField(15);
t.setText("5"); // in case that was the cause, not having it set to anything, but it doesn't appear to be.
Container pane = getContentPane();
pane.add(t);
}
Re: ACTION LISTENER HANDLING
Well, I made a new textfield called t in the actionPerformed, and the stupdi exception problem went away, though it won't go change text in text field. Now I know that somehow it isn't recognizing the text field, but somehow isn't throwing a compiler error.
Re: ACTION LISTENER HANDLING
Wait never mind, I fixed it, the program code that'll work is:
Code :
import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JTextField;
import java.awt.Component;
import java.awt.Container;
import javax.swing.JPanel;
import java.awt.*;
import java.awt.event.*;
import java.io.*;
import javax.swing.AbstractButton;
import java.util.*;
import javax.swing.*;
class NewClass1 extends JFrame implements ActionListener{
private JTextField t;
JLabel l;
private JButton j, j1, j2;
// JFrame f;
public NewClass1()
{
// JFrame f =new JFrame("faria");
t= new JTextField(15);
l = new JLabel("Label: ",
SwingConstants.RIGHT);
JButton j= new JButton("a");
j.addActionListener(this);
JButton j1= new JButton("b");
j1.addActionListener(this);
JButton j2= new JButton("c");
j2.addActionListener(this);
// l.setLayout(new FlowLayout());
// f.add(l);
// l.add(t);
// l.add(j);
// l.add(j1);
// l.add(j2);
setTitle("farria");
Container pane = getContentPane();
pane.add(l);
pane.add(t);
pane.add(j);
pane.add(j1);
pane.add(j2);
// j.addActionListener(new A());
// j1.addActionListener(new B());
// j2.addActionListener(new C());
pane.setLayout(new GridLayout(4,4));
setVisible(true);
setSize(400,400);
setDefaultCloseOperation(EXIT_ON_CLOSE);
}
public static void main(String args[]){
NewClass1 refVar = new NewClass1();
}
public void actionPerformed(ActionEvent e)
{
// JTextField t = new JTextField();
if (e.getActionCommand().equals("a"))
{
System.out.println("hiii" );
t.setText("hi");
}
else if (e.getActionCommand().equals("b"))
{
System.out.println("hello" );
t.setText("hello");
}
else if (e.getActionCommand().equals("c"))
{
System.out.println("how r u" );
t.setText("how r u");
}
}
}