简体   繁体   中英

Why is the sleep method affecting/not showing my updated JTextArea on the panel?

I have a window that has various panels. On one panel, I have a JTextArea called dogTalk where I update its text. Upon user's click of a button, I want the text to add what I have mentioned below in setText.

I used the sleep method so that the user can read my updated text and the window can close automatically within 4 seconds. (I don't want the user to have the ability to close the window on close, hence I didn't use Jframe.EXIT_ON_CLOSE but used JFrame.DO_NOTHING_ON_CLOSE and used my automatic closing with the help of sleep and system.ext(0))

However, I noticed that the sleep method does not allow the dogTalk to get updated. It prints out "we're working", though, so I am guessing it's a problem with the window? I know that the sleep is causing the issue and not something else in my code because when I commented out the sleep and system.exit(0) and tested if my if statement is executing, I noticed the JTextArea did update with my statement just fine! Could you please help me?

if (e.getActionCommand().equals("buybone")) {

        System.out.println("We're working");
        dogTalk.setText(dogTalk.getText() + "\nWow bone very wow much thanks bye.");
        try
        {  
            TimeUnit.SECONDS.sleep(4);
        }
        catch ( InterruptedException e1 )
        {
            // TODO Auto-generated catch block
            e1.printStackTrace();
        }

       System.exit(0);

}

Rather than calling System.exit , let the application gracefully die out. An application terminates when there are no non-daemon threads still alive. Daemon is just a flag used to determine whether the JVM should terminate if that thread is still running; the JVM will still terminate if non-daemon threads are running.

With that said, the problem is that you're calling sleep on the Event Dispatch Thread.

The EDT handles all updating and rendering of Swing and AWT components, as well as execute the behavior specified in event listeners (like ActionListener#actionPerformed(ActionEvent) ). If you cause it to block (through sleeping or other forms of blocking), it won't be able to process updating and rendering. When you call setText , the EDT needs to be able to adjust the text. You're preventing this by forcing it to sleep.

How to fix

Spawn a new thread, have it wait 4 seconds, then have it dispose of your frame:

Java 8+

public void actionPerformed(ActionEvent e) {
     dogTalk.setText(...);
     new Thread(() -> {
          TimeUnit.SECONDS.sleep(4);
          frame.dispose();
     }).start();
}

Before Java 8

public void actionPerformed(ActionEvent e) {
    dogTalk.setText(...);
    new Thread(new Runnable() {
        public void run() {
            TimeUnit.SECONDS.sleep(4);
            frame.dispose();
        }
    }).start();
}

Just right after dogTalk.setText(dogTalk.getText() + "\\nWow bone very wow much thanks bye."); put the following code :

dogTalk.revalidate();
dogTalk.repaint();

the following code works well i used Frame.update function it worked. in your case you have to update panel i guess

           dogTalk.setText(dogTalk.getText() + "\nWow bone very wow much thanks bye.");
        frame.update(getGraphics());
        try
        {  
            TimeUnit.SECONDS.sleep(4);
        }
        catch ( InterruptedException e1 )
        {
            // TODO Auto-generated catch block
            e1.printStackTrace();
        }
        System.exit(0);

this is my full program

public class TestSleep extends JFrame implements ActionListener{

JTextArea are=new JTextArea("asdjkfh");

JButton button=new JButton("Submit done");

public TestSleep()
{
    are.setBounds(20, 20, 30, 10);
    button.setBounds(10, 50, 20, 20);
    this.add(are);
    this.add(button);
    button.addActionListener(this);

}
public static void main(String[] args)
{
    TestSleep sleep=new TestSleep();
    sleep.setLayout(new GridLayout());
    sleep.setVisible(true);
    sleep.setBounds(10, 10, 500, 280);
}
@Override
public void actionPerformed(ActionEvent e)
{
        System.out.println("Working");
        are.setText(are.getText() + "\nWow bone very wow much thanks bye.");
        this.update(getGraphics());
        try
        {  
            TimeUnit.SECONDS.sleep(4);
        }
        catch ( InterruptedException e1 )
        {
            // TODO Auto-generated catch block
            e1.printStackTrace();
        }
        System.exit(0);

}   

}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM