2

I have an AWT modal dialog

public class d extends java.awt.Dialog {...

On the dialog frame, using netbeans gui designer I put dialog then panel then button. I am trying to close the dialog by pressing the button. I am not interested in System.exit(0).

The netbeans generator created

private void jButtonCloseActionPerformed(java.awt.event.ActionEvent evt){ 

I think I should call dispose in that function, however when called it disposes the dialog but dialog thread never ends.

I have the following handler working when window is closed by default dialog close button


     dialog.addWindowListener(new java.awt.event.WindowAdapter() {
       @Override
        public void windowClosing(java.awt.event.WindowEvent e) {
           Window window = SwingUtilities.getWindowAncestor(e.getComponent());
           window.dispose();
        }
     });
    

and the above is working fine, i.e. thread ends.

I could use the same approach in the jButtonCloseActionPerformed but I don't know how can I get window object.

How can I achieve that? Any other good solution is very welcomed as well.

I will appreciate your help very much.

mKorbel
  • 109,525
  • 20
  • 134
  • 319
norbi771
  • 814
  • 2
  • 12
  • 29
  • 1
    What "dialog thread"? And just for idle curiosity -- why an AWT application and not a Swing application? – Hovercraft Full Of Eels Jun 27 '12 at 23:28
  • I am Java newbie so unintentionally I could use wrong classification. I called it AWT because of dialog that extends java.awt.dialog ... but maybe it is Swing, I don't really know. Please don't take me as ignorant, but I really don't know that ... Java is not the language I do programming every day, just need to delegate some tasks of web application to java applet. – norbi771 Jun 27 '12 at 23:39
  • Dialog thread ... in that case it is a process that never ends. In netbeans when I run that application, after the application ends (or reaches the last line in main) I can still see "(run-single) *running*" in the bottom part of netbeans IDE, which I call running thread. The only way to finish that is by pressing stop button in the output window. I think that it is a thread of the dialog window. – norbi771 Jun 27 '12 at 23:54
  • 1
    Yeah, if it is a java.awt.Dialog, then it's an AWT component. A word of advice: don't. Use only Swing components unless you have a very good reason not to do so -- and you don't. – Hovercraft Full Of Eels Jun 27 '12 at 23:56
  • in the meantime I found that I can use *this* in the jButtonSelectCertificateActionPerformed function. And then: Window window = SwingUtilities.getWindowAncestor(this); window.setVisible(false); window.dispose(); If I run this like that, the thread ends properly ... is this right approach to close that dialog? – norbi771 Jun 27 '12 at 23:58
  • Won't my applet size increase much if I use swing? I am trying to use basic components / libraries in order to avoid the applet getting bigger. – norbi771 Jun 28 '12 at 00:00
  • 1
    ??? that statement makes no sense. Why do you assume that using Swing components will take up more size than AWT? Do you want to use a GUI library that is 15 years out of date? Swing is getting long in the tooth as well, but it is a lot more flexible and powerful than AWT. Your statements suggest that you could benefit from reading some of the Java GUI tutorials. You won't regret doing this. – Hovercraft Full Of Eels Jun 28 '12 at 00:03
  • Thanks, I just realized that I should use swing and moved all the stuff to swing. You are right that I could benefit from Java GUI tutorials .... I already did go through some of them. I just thought that if I use awt less resources would be needed to download. Then I realized that at some point I use swing anyway and there was no point sticking to awt. I have had more problems with awt and yesterday said good bye to awt :-) Thanks for your help. – norbi771 Jun 29 '12 at 09:15
  • you're welcome. Glad you've got things working! – Hovercraft Full Of Eels Jun 29 '12 at 09:34
  • AWT is heavyweight i.e. its components uses the resources of system, Swing provides platform-independent and lightweight components such as JButton, JTextField, JTextArea, JRadioButton, JCheckbox, JMenu, JColorChooser etc – Jaimin Patel Mar 04 '16 at 10:46

1 Answers1

0

I think the best answer (in short) is to use the following code

Window window = SwingUtilities.getWindowAncestor(this);
window.dispose();

this is important here. I tried to get window object somehow by getting parent object from events, etc. In case of WindowClosing I could get window object reference in that way indeed, but in case of a button it didn't work ... then I realized I can simply refer to this. Most examples in the Internet calls System.Exit(0) but IMHO calling System.Exit(0) could be OK in the case of examples ONLY, not in real app.

norbi771
  • 814
  • 2
  • 12
  • 29
  • `SwingUtilities` Congratulations on following, partly, the advice of @Hovercraft. Now move over to using Swing **components,** and get a better component toolkit that people can actually remember from recent use! – Andrew Thompson Jun 29 '12 at 06:38