0

Ever since I have learned C++, I cannot help but think of some of the features in some of the other programming languages (namely, Java) in terms of C++ every now and then. This may be because I simply don't trust them 100% (I think it is foolish to trust ANYTHING 100%), and the stuff about JFrame only confirms my worry. I remember reading somewhere that the memory requested by the JFrames must be given back to the system when it is no longer being used, and that could be done by JFrame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE); I would like to know if having a class that is a JFrame making it dispose on close the equivalent of, in C++, invoking delete this; inside a close() method of a class.

I have tried this, and had the class implement WindowListener. I then put some System.out.print() method in windowClosed(), windowClosing() AND NOTHING HAPPENED WHEN I CLOSED THE JFrame!!

I did something like:

import java.awt.GridLayout;
import java.awt.HeadlessException;
import java.awt.event.ItemEvent;
import java.awt.event.ItemListener;
import java.awt.event.WindowListener;
import java.awt.event.WindowEvent;

import javax.swing.ButtonGroup;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JRadioButton;

public class ItemListenerDemo extends JFrame implements WindowListener,ItemListener
{
private JRadioButton buttonA, buttonB;
private ButtonGroup radioButtonGroup;
private JLabel buttonSelectionLabel;
private JPanel buttonPanel;

private GridLayout gridLayout;

public ItemListenerDemo(String title) throws HeadlessException 
{
    super(title);
    //setting up the buttons
    buttonA = new JRadioButton("Option A", true);
    buttonB = new JRadioButton("Option B", false);
    //adding itemListeners
    buttonA.addItemListener(this);
    buttonB.addItemListener(this);
    //adding the buttons to the radioButtonGroup
    radioButtonGroup = new ButtonGroup();
    radioButtonGroup.add(buttonA);
    radioButtonGroup.add(buttonB);
    //adding buttons to JPanel
    buttonPanel = new JPanel();
    buttonPanel.add(buttonA);
    buttonPanel.add(buttonB);

    //making the buttonSelectionLabel
    buttonSelectionLabel = new JLabel("some text");
    //setting up the gridLayout
    gridLayout = new GridLayout(2,1);
    //setting the layout of the demo to the gridLayout
    setLayout(gridLayout);
    //adding components to demo
    add(buttonSelectionLabel);
    add(buttonPanel);
    //setting up the demo
    setSize(200,200);
    setVisible(true);
    setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
}

/**
 * @param args
 */
public static void main(String[] args) 
{
    ItemListenerDemo demo = new ItemListenerDemo("Item Listener Example");

}

@Override
public void itemStateChanged(ItemEvent e) {
    // TODO Auto-generated method stub
    buttonSelectionLabel.setText("You selected " + 
            ((JRadioButton)e.getItem()).getText());
}

@Override
public void windowActivated(WindowEvent e) {
    // TODO Auto-generated method stub

}

@Override
public void windowClosed(WindowEvent e) {
    // TODO Auto-generated method stub
    System.out.println(this.toString() + " was deleted");
}

@Override
public void windowClosing(WindowEvent e) {
    // TODO Auto-generated method stub
    System.out.println(this.toString() + " was deleted");
}

@Override
public void windowDeactivated(WindowEvent e) {
    // TODO Auto-generated method stub

}

@Override
public void windowDeiconified(WindowEvent e) {
    // TODO Auto-generated method stub

}

@Override
public void windowIconified(WindowEvent e) {
    // TODO Auto-generated method stub

}

@Override
public void windowOpened(WindowEvent e) {
    // TODO Auto-generated method stub

}

}

Yes, I ended up showing you ALL MY CODE

Mike Warren
  • 3,796
  • 5
  • 47
  • 99
  • 2
    Regarding the last part -- did you actually register the class as a WindowListener for the JFrame? – Ted Hopp Jun 19 '13 at 04:06
  • 2
    Your preamble is unnecessary. We don't have time to read all the irrelevant stuff. Keep the question plain and simple. Better yet post your `SSCCE` that demonstrates your problem. We can't (always) guess what you are doing wrong. – camickr Jun 19 '13 at 04:19
  • And did you set the default close operation to something other then `HIDE_ON_CLOSE`? – MadProgrammer Jun 19 '13 at 04:32
  • I said something like `public class MyFrame extends JFrame implements WindowListener` and then in `windowClosed()` and in `windowClosing()`, I had `System.out.println(this.toString() + " is closed....");`. I also used `setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE)`. I have a `main()` in this class to open the JFrame. I close the JFrame, and it simply terminates the application! – Mike Warren Jun 19 '13 at 07:29
  • *"AND NOTHING HAPPENED WHEN.."* All the SHOUTING began? Maybe it did happen, but the observers were too deafened to notice. See also: 1) See [The Use of Multiple JFrames, Good/Bad Practice?](http://stackoverflow.com/a/9554657/418556) 2) For better help sooner, post an [SSCCE](http://sscce.org/). – Andrew Thompson Jun 19 '13 at 12:07

1 Answers1

1

You seem to have forgotten to add a window listener:

addWindowListener(this);
nos
  • 223,662
  • 58
  • 417
  • 506