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