Hello everybody
I have problem in adding an action to the buttons from separate class..
I know how to add the actionListner class as inner class but if I want to ceate this class separate from other classes..how I can implement it..
it show me many errors..
this is the class I wrote...I don't know what the wrong
import javax.swing.*;
import java.awt.*;
public class PersonInfoForm {
JFrame frame;
JTextField nameTF;
JTextField lastNameTF;
JButton clearB, printB;
public PersonInfoForm() {
frame = new JFrame("Person Information");
frame.setSize(300, 300);
Container mainC = frame.getContentPane();
mainC.setLayout(new GridLayout(3, 2));
mainC.add(getNamePanel());
mainC.add(getLastNamePanel());
mainC.add(getCommandPanel());
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
public void showForm () {
frame.setVisible(true);
}
protected JPanel getNamePanel() {
JPanel nameP = new JPanel();
nameP.setLayout(new FlowLayout(FlowLayout.LEFT));
JLabel nameL = new JLabel("Name: ");
nameTF = new JTextField(10);
nameP.add(nameL);
nameP.add(nameTF);
return nameP;
}
protected JPanel getLastNamePanel(){
JPanel lastNameP = new JPanel();
lastNameP.setLayout(new FlowLayout(FlowLayout.LEFT));
JLabel lastNameL = new JLabel("Last Name: ");
lastNameTF = new JTextField(10);
lastNameP.add(lastNameL);
lastNameP.add(lastNameTF);
return lastNameP;
}
protected JPanel getCommandPanel(){
JPanel commandP = new JPanel();
commandP.setLayout(new FlowLayout(FlowLayout.LEFT));
clearB = new JButton("Clear");
printB = new JButton("Display Info");
commandP.add(clearB);
commandP.add(printB);
// clearB.addActionListener(new displayAction());
printB.addActionListener(new displayAction());
return commandP;
}
public JTextField nameTF(){
return nameTF;
}
/* class displayAction implements ActionListener{
public void actionPerformed(ActionEvent e){
String messg = nameTF.getText()+ ", "+lastNameTF.getText();
JOptionPane.showMessageDialog(null,messg);
}
}*/
}
as u see I add the ActionListner class as inner class and it work but when I try to use it in separate class the program not working
this is the class of action
import java.awt.event.*;
import javax.swing.*;
class displayAction implements ActionListener {
PersonInfoForm info;
public void actionPerformed(ActionEvent e) {
String messg = info.nameTF().getText() + ", " + info.lastNameTF.getText();
JOptionPane.showMessageDialog(null, messg);
}
}
I try to use an object for the PersonInfoForm but it still give me an error
and this is my
test class
public class Test {
public static void main(String[] args) {
PersonInfoForm info = new PersonInfoForm();
info.showForm();
}
}
plz...help me ^_^
NoOor