JFrame GUI not showing anything other than title....

  • Java
  • Thread starter Wrichik Basu
  • Start date
  • Tags
    Gui
In summary: Here, a Swing Timer is used to wait for 3 seconds before calling makeGUI_2(). This ensures that the window is visible for 3 seconds before disappearing.
  • #1
Wrichik Basu
Science Advisor
Insights Author
Gold Member
2,116
2,691
I have written a program that will launch three JFrame windows if the user enters the correct things everywhere. In case there is a mistake, an error window will open, and then after 3s, the user will be re-directed to the window where he made the error.

The flowchart is attached.

Everything is working fine except the JFrame created by makeGUI_4().

The function makeGUI_4() looks something like this:
Java:
private void makeGUI_4() {
        try {
            jfrm4 = new JFrame();
            jfrm4.setLayout(new FlowLayout());
            jfrm4.setBounds(50, 50, 527, 351);
            jfrm4.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            jfrm4.setTitle("Error!");
            
            jlb1 = new JLabel("Wrong date/time.");
            jlb2 = new JLabel("Program restarting...");
            jfrm4.add(jlb1);
            jfrm4.add(jlb2);
            jfrm4.setVisible(true);
            Thread.sleep(3000);
            jfrm4.setVisible(false);
            makeGUI_2();
            
        } catch (InterruptedException ex) {
            Logger.getLogger(BuildGUI.class.getName()).log(Level.SEVERE, null, ex);
        }
 }
Here, jlb1 and jlb2 are two JLabels declared as class variables. jfrm4 is a JFrame also declared as a class variable.

I want a simple window that shows:
"Wrong date/time."
"Program restarting..."
and then the thread goes to sleep for 3s, and calls makeGUI_2().

This is what I get when the user makes an error and makeGUI_4() is called:

1569257887725.png


The JLabels just don't appear in the window.

Where am I going wrong?
 

Attachments

  • flowchart.pdf
    53.4 KB · Views: 198
Technology news on Phys.org
  • #2
Could this be a race condition? The code I've seen used an invokeLater() setup to allow the GUI to get created before its displayed. Also pack() and setVisible(true) is usually the very last thing you do.

Can you run this via a debugger like use NetBeans and its debugger?

Here's an example of EventQueue.invokeLater() usage:

http://referencedesigner.com/tutorials/javaswing/javaswing_02.php
and a nice presentation of HelloWorld but without invokeLater

https://www.javaguides.net/2019/06/java-swing-hello-world-example-tutorial.html
https://www.codejava.net/java-se/sw...orld-tutorial-for-beginners-using-text-editor
Also I found this site with lots of examples though none are using invokeLater but might be useful in your other programs:

https://javacodex.com/Swing/Hello-World
 
  • #3
jedishrfu said:
Could this be a race condition? The code I've seen used an invokeLater() setup to allow the GUI to get created before its displayed.
I am not quite sure where to add the invokeLater(). I added it to the function call of makeGUI() (the first function being called), and there is no change. I added it to the function call of makeGUI_4() by changing the code in makeGUI_3() to this:
Java:
SwingUtilities.invokeLater(new Runnable(){
    @Override
    public void run(){
        makeGUI_4();
    }
});
but there is still no luck.
jedishrfu said:
Can you run this via a debugger like use NetBeans and its debugger?
I am using Apache NetBeans 11.0 with JDK 12. Note that the build always completes successfully, even if the window is not displayed correctly. How do I use the debugger here?

I did some debugging myself and found that if I comment out the last parts of the function and change it to:
Function makeGUI_4():
private void makeGUI_4() {

        jfrm4 = new JFrame();
        jfrm4.setLayout(new FlowLayout());
        jfrm4.setBounds(50, 50, 527, 351);
        jfrm4.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        jfrm4.setTitle("Error!");

        jlb1 = new JLabel("Wrong date/time.");
        jlb2 = new JLabel("Program restarting...");
        jfrm4.add(jlb1);
        jfrm4.add(jlb2);
        jfrm4.setVisible(true);
        try {
            Thread.sleep(2000);
        } catch (InterruptedException ex) {
            Logger.getLogger(BuildGUI.class.getName()).log(Level.SEVERE, null, ex);
        }
        //jfrm4.setVisible(false);
        //makeGUI_2();

}
then the window is created properly. It seems that the problem is occurring when I am calling makeGUI_2()
 
Last edited:
  • #4
It's been a while, but I think instead of
Java:
            jfrm4.add(jlb1);
            jfrm4.add(jlb2);
You want
Java:
            jfrm4.getContentPane().add(jlb1);
            jfrm4.getContentPane().add(jlb2);
and I think this might work as well
Java:
            jfrm4.setRootCheckingEnabled(true);
            jfrm4.add(jlb1);
            jfrm4.add(jlb2);
But like I say, it has been a while since I did any Java, especially Swing.
 
  • #5
I found out the error. It is a simple logical error.

As @jedishrfu said, jfrm4.setVisible(true) is being executed at the end. As a result, the window is being created after a delay of 3s, but as soon as the window is created, the next command, jfrm4.setVisible(false) is executed, and the window vanishes. Hence, I was being unable to see the output. Commenting out the last code solved the problem.

Basically I wanted the window to stay for 3s before disappearing, and that's why I put the Thread.sleep() after setVisible(true). Let's see how I can do that now.

Update:

This can be done in the following way:
Edited version of function makeGUI_4():
private void makeGUI_4() {

        jfrm4 = new JFrame();
        jfrm4.setLayout(new FlowLayout());
        jfrm4.setBounds(50, 50, 527, 351);
        jfrm4.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        jfrm4.setTitle("Error!");

        jlb1 = new JLabel("Wrong date/time.");
        jlb2 = new JLabel("Program restarting...");
        jfrm4.add(jlb1);
        jfrm4.add(jlb2);
        jfrm4.setVisible(true);

        Timer timer = new Timer(1000, new ActionListener() {
            int i = 0;

            @Override
            public void actionPerformed(ActionEvent ae) {
                try {
                    Thread.sleep(1000);
                } catch (InterruptedException ex) {
                    Logger.getLogger(BuildGUI.class.getName()).log(Level.SEVERE, null, ex);
                }
                i++;
                if (i == 3) {
                    SwingUtilities.invokeLater(new Runnable() {
                        @Override
                        public void run() {
                            jfrm4.setVisible(false);
                            makeGUI_2();
                        }
                    });
                }
            }
        });
        timer.start();

}
 
Last edited:
  • #6
You can not create the JFrame, add stuff to it, then sleep all in a row. Here is what is happening in order:

Create JFrame, which creates an actual window in Windows and sets it up.
You add two Labels to the object, which causes a repaint event to go into the event queue
You then sleep in your thread
Your method returns and goes back into the event loop which then gets the queued repaint event, which then draws

You're expecting the setVisible to be a synchronous function that will instantly show you the state of the window, but that's now how these windows work, they're almost entirely event driver and you are IN the event loop when you create this window.

You need to get your head around event driven code. You'll need this in order to have the window stick around for three seconds, you don't want to stop your UI thread for stuff like that, you want to register a timeout event, which then calls a function to dispose of your window.
 
  • Like
Likes Wrichik Basu

What could be causing my JFrame GUI to only show the title and not any other components?

There could be a few different reasons for this issue. One possibility is that you have not added any components to your JFrame, so there is nothing to display. Another common issue is that you have not set the visibility of your JFrame to true, which means it will not be visible on the screen. Another possible cause is that you have not set the layout manager for your JFrame, so your components are not being positioned correctly on the screen.

How can I troubleshoot why my JFrame GUI is not displaying correctly?

To troubleshoot this issue, you can try printing out the content pane of your JFrame to see if any components have been added. You can also check the visibility of your JFrame and make sure it is set to true. Additionally, you can try setting a different layout manager or adding a simple component, like a button, to see if it is being displayed. Finally, check your code for any errors or typos that may be causing the issue.

Why are my JFrame components not showing up even though I have added them?

One possible reason for this could be that you have not set the size or preferred size of your JFrame. If the size is not set, the components may be added but they will not be visible because the JFrame is too small. Another possibility is that you have not set the layout manager correctly, causing the components to be positioned off-screen. It is also possible that the components are being covered by another component or are behind the JFrame itself.

How can I make sure my JFrame GUI is displaying properly on different platforms?

To ensure your JFrame GUI displays correctly on different platforms, it is important to use layout managers instead of hard-coding the positioning and size of your components. Layout managers will adjust the positioning and size of your components based on the size of the JFrame and the platform it is being run on. Additionally, it is important to test your GUI on different platforms and make adjustments as needed to ensure it looks and functions correctly.

What are some common mistakes that can cause a JFrame GUI to not display properly?

Some common mistakes that can cause issues with displaying a JFrame GUI include not setting the visibility to true, not adding any components, not setting a layout manager, and not setting the size or preferred size of the JFrame. Other mistakes could include typos or errors in the code, using absolute positioning instead of a layout manager, or not testing the GUI on different platforms. It is important to carefully check your code and troubleshoot any potential issues to ensure your JFrame GUI is displaying correctly.

Similar threads

  • Programming and Computer Science
Replies
1
Views
1K
  • Engineering and Comp Sci Homework Help
Replies
2
Views
5K
  • Programming and Computer Science
Replies
6
Views
2K
  • Engineering and Comp Sci Homework Help
Replies
1
Views
2K
  • Programming and Computer Science
Replies
2
Views
3K
  • Computing and Technology
Replies
6
Views
2K
  • Engineering and Comp Sci Homework Help
Replies
4
Views
12K
Back
Top