• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
  • Mikalai Zaikin

Help me ..how I can add an action to the button

Posts: 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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 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));



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


plz...help me ^_^

Ranch Hand
Posts: 283
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Eddie Vanda

thank u ,, I try the program and it work now ...really thanx a lot ^_^
I don't always make ads but when I do they're tiny
a bit of art, as a gift, that will fit in a stocking
    Bookmark Topic Watch Topic
  • New Topic