0

I am trying to close the current window in a Java Swing. I referred to some questions and I used the code posted as an answer, but when I use that code mentioned below and run it, it has no effect, and the window turns to appear as shown in the picture.

Code

btn_Ok.addActionListener(new ActionListener() { //ActionListener für Button Bsave
    public void actionPerformed(ActionEvent ae) {
        if (ae.getSource() == btn_Ok) {
            System.out.println("Button Ok Pressed");
            setVisible(true);
            dialogdatei.dispose();
            dialogdatei.dispatchEvent(new WindowEvent(dialogdatei, WindowEvent.WINDOW_CLOSING));
            dialogdatei.setDefaultCloseOperation(EXIT_ON_CLOSE);

enter image description here

Community
  • 1
  • 1
rmaik
  • 1,076
  • 3
  • 15
  • 48
  • 2
    setVisible(false); and dialogdatei.dispose(); is the same and setDefaultCloseOperation(EXIT_ON_CLOSE); is designated to terminate current JVM – mKorbel Dec 09 '14 at 12:36
  • 1
    See [Optional way to close a dialog window](http://stackoverflow.com/a/22105496/1795530) and [Check what triggered WindowClosing event](http://stackoverflow.com/a/26818888/1795530) for some guidance. – dic19 Dec 09 '14 at 12:40
  • @mKorbel `setVisible(false)` and `dispose()` are not the same thing. – khelwood Dec 09 '14 at 12:47
  • @khelwood is the same until current JVM is alive, agree (only in this case) but there is important difference for application that contains a few windows, last one to turn off light (terminate current JVM) – mKorbel Dec 09 '14 at 13:01
  • 1
    I am trying to close the current window (reall not not sure about ypour goal ...) there are 1. reuse dialogdatei for another event, 2. dialogdatei.setVisible(false) 3. or to set DISPOSE_ON_CLOSE as setDefaultCloseOpetarion, because code posted (wrong code lines, ordering, and only method dispatchEvent to closing current JVM) – mKorbel Dec 09 '14 at 13:06
  • 1
    For better help sooner, post an [MCVE](http://stackoverflow.com/help/mcve) (Minimal Complete Verifiable Example) or [SSCCE](http://www.sscce.org/) (Short, Self Contained, Correct Example). But @mKorbel has given some good advice. Note that what you are doing now, by shoving a lot of code lines together is what I call 'programming by magic'. Unfortunately it does not work. You actually need to **read the documentation** for the methods being used, and try to ***understand*** what the documentation is telling you. – Andrew Thompson Dec 09 '14 at 23:43

0 Answers0