Win a copy of Kotlin in Action this week in the Kotlin forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

Help me I can add an action to the button  RSS feed

NoOor nice
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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 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));



public void showForm () {

protected JPanel getNamePanel() {
JPanel nameP = new JPanel();
nameP.setLayout(new FlowLayout(FlowLayout.LEFT));
JLabel nameL = new JLabel("Name: ");
nameTF = new JTextField(10);
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);
return lastNameP;

protected JPanel getCommandPanel(){

JPanel commandP = new JPanel();
commandP.setLayout(new FlowLayout(FlowLayout.LEFT));
clearB = new JButton("Clear");
printB = new JButton("Display Info");
// 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();

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();

} me ^_^

Eddie Vanda
Ranch Hand
Posts: 283
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Hi NoOor

I ran your code and got a null pointer exception in your action class because you had not initialized variable "info". I removed a couple of public statements, renamed the class with "main" to "TestActionClass", and added the constructor to the action class to set a value into "info". It now runs fine!

NoOor nice
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Eddie Vanda

thank u ,, I try the program and it work now ...really thanx a lot ^_^
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!