0

I am new to java and learning about swing and JFrames. I have followed multiple tutorials and I can not get my JFrame to stay open. When I run my code I can see the empty JFrame open, but it closes or disappears after about 2 seconds. This happens when I use IntelliJ and Eclipse. I have posted a sample of my code that should open a empty JFrame, but it always disappears after a few seconds. Could someone please help me stop it from closing?

Things I have tried to fix it.

  • Reinstalled Java jdk-13.0.1
  • Reinstalled IntelliJ and Eclipse
  • Added jdk/bin directory to windows environment variables
  • Updated video card drivers

    package com.FrameDemo;
    
    import javax.swing.*;
    
    public class EmptyJFrame {
    
        public static void main(String[] args) {
    
            JFrame frame = new JFrame();
            frame.setSize(300, 300);
            frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            frame.setVisible(true);
        }
    }
    
Slaw
  • 37,820
  • 8
  • 53
  • 80
  • 4
    I tried it from the console & IntelliJ but i couldn't reproduce the issue. Is this the whole code? – Stelios Papamichail Jan 11 '20 at 12:25
  • You may want to use a separate thread https://stackoverflow.com/questions/5780936/java-eventqueue-why-should-everything-be-in-invokelater-method – OneCricketeer Jan 11 '20 at 13:00
  • The tutorials did not use a separate thread. It is very small and should open a empty JFrame. When I run the code the JFrame opens for 2-3 seconds and disappears. I have only tired to run my code from both IntelliJ and Eclipse IDE. I don't know how to run it outside of an IDE. – LateNightCoder Jan 11 '20 at 13:03
  • Does this answer your question? [Java - how do I prevent WindowClosing from actually closing the window](https://stackoverflow.com/questions/7613577/java-how-do-i-prevent-windowclosing-from-actually-closing-the-window) – Kalana Jan 11 '20 at 13:12
  • If you are running this program on a Windows OS, do check in the task manager if you find a java process active/added after launching your program, even if the swing frame "auto-closes". If yes, try to bring it to front and see if it stays. – S B Jan 11 '20 at 13:32
  • Does the Java icon of the application disappear from the task bar? My guess would rather be that the IDE comes up in focus because of some setting or perhaps because you just click on it, but the application and its window is still running fine. – tevemadar Jan 11 '20 at 13:55
  • Btw. do you happen to use an "Ease of access" function, especially the one called "Activate a window by hovering over it with the mouse"? Because that would do exactly what you describe. – tevemadar Jan 11 '20 at 13:59
  • @tevemadar When I click run the empty jframe shows in my taskbar with the java icon and disappears after about 2 seconds. I looked in task manager after clicking run and I can see it listed as "Open JDK Platform binary" and then it closes after about 2 seconds. – LateNightCoder Jan 11 '20 at 15:27
  • @S B I saw it in the task manager listed as "Open JDK Platform binary" but it closes after about 2 seconds and its not enough time to click on it for properties. – LateNightCoder Jan 11 '20 at 15:29
  • Has anyone tried to run my code with java jdk-13.0.1 and successfully have a empty jframe stay open without it closing on them after 2 seconds? – LateNightCoder Jan 11 '20 at 17:43

1 Answers1

0

I can't reproduce the issue, but I can describe how your code works for me, with Java13, built from command line:

  • somehow get a commmand prompt and navigate to the location of your com folder (which then contains FrameDemo and then EmptyJFrame.java)
  • as you say the JDK is in path already, type javac com\FrameDemo\EmptyJFrame.java
  • then type java com.FrameDemo.EmptyJFrame

In my particular case I have your file as c:\test\com\FrameDemo\EmptyJFrame.java, so I typed these commands in c:\test, and I also needed to add JDK in the path first:

Microsoft Windows [Version 10.0.17134.1184]
(c) 2018 Microsoft Corporation. All rights reserved.

C:\test>path c:\External\jdk-13\bin

C:\test>java --version
openjdk 13 2019-09-17
OpenJDK Runtime Environment (build 13+33)
OpenJDK 64-Bit Server VM (build 13+33, mixed mode, sharing)

C:\test>javac com\FrameDemo\EmptyJFrame.java

C:\test>java com.FrameDemo.EmptyJFrame

So it is Java13 (though I am not sure about the .0.1, it is from 2019-09-17), and yes, it works, the panel remains open until I close it myself.

tevemadar
  • 12,389
  • 3
  • 21
  • 49
  • Thank you taking the time to try out my code. I did everything you did step by step and my jframe opens and closes in about 2 seconds. I can not get it to stay open. It doesn't matter if I try to open it with the command line, Intellij, or Eclipse. The frame disappears after a few seconds. I am out of ideas on how to fix this. – LateNightCoder Jan 11 '20 at 19:45
  • This is 13.0.0, not 13.0.1. – Mark Rotteveel Jan 13 '20 at 13:29