*
The moose likes Swing / AWT / SWT and the fly likes OK button does not dispose of parent frame within ActionListener Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of The Java EE 7 Tutorial Volume 1 or Volume 2 this week in the Java EE forum
or jQuery UI in Action in the JavaScript forum!
JavaRanch » Java Forums » Java » Swing / AWT / SWT
Bookmark "OK button does not dispose of parent frame within ActionListener" Watch "OK button does not dispose of parent frame within ActionListener" New topic
Author

OK button does not dispose of parent frame within ActionListener

Jerry Goldsmith
Ranch Hand

Joined: Nov 29, 2006
Posts: 53
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

Joined: Jul 08, 2003
Posts: 24183
    
  34

new_listener is null. Did you mean to set it to something, like an instance of Main?


[Jess in Action][AskingGoodQuestions]
Jerry Goldsmith
Ranch Hand

Joined: Nov 29, 2006
Posts: 53
You sir are a gentleman and a scholar. Thank you so much. Your advice did the trick. Thanks again.
 
Consider Paul's rocket mass heater.
 
subject: OK button does not dispose of parent frame within ActionListener