Win a copy of Re-engineering Legacy Software this week in the Refactoring forum
or Docker in Action in the Cloud/Virtualization forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

OK button does not dispose of parent frame within ActionListener

 
Jerry Goldsmith
Ranch Hand
Posts: 53
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have a simple class which I am using to test out ActionListener functionality. It opens a frame and panel with an OK button. An action listener is added to the OK button and references a method to dispose of the parent frame.

This should be really straight forward; however, when the OK button is clicked nothing happens. It's like the code within the action listener is never being executed. I'm probably making a very simple mistake but I can't find it. Can someone take a look and see if you can find what I am doing wrong? Thanks a bunch. Here is my code:

package test2;

import javax.swing.*;
import java.io.*;
import java.awt.*;
import java.awt.event.*;

public class Main implements ActionListener {

public static String new_name;
public static JTextField new_text;
public static ActionListener new_listener;
public static JFrame parentFrame;

public Main() {
}

public static void main(String[] args) {

parentFrame = new JFrame();
JButton ok_button = new JButton("OK");
JPanel new_panel = new JPanel();

new_panel.add(ok_button);
parentFrame.getContentPane().add(new_panel);
parentFrame.setVisible(true);

ok_button.addActionListener(new_listener);

}

public void actionPerformed(ActionEvent e) {
parentFrame.dispose();
}

}
 
Ernest Friedman-Hill
author and iconoclast
Marshal
Pie
Posts: 24208
35
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
new_listener is null. Did you mean to set it to something, like an instance of Main?
 
Jerry Goldsmith
Ranch Hand
Posts: 53
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You sir are a gentleman and a scholar. Thank you so much. Your advice did the trick. Thanks again.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic