• 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 ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

OK button does not dispose of parent frame within ActionListener

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

}
 
author and iconoclast
Posts: 24207
46
Mac OS X Eclipse IDE Chrome
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You sir are a gentleman and a scholar. Thank you so much. Your advice did the trick. Thanks again.
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic