File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Swing / AWT / SWT and the fly likes Gui refresh Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Swing / AWT / SWT
Bookmark "Gui refresh" Watch "Gui refresh" New topic
Author

Gui refresh

Kasi Viswan
Ranch Hand

Joined: Sep 27, 2008
Posts: 42
Hello,

Can someone guide me how to refresh a swing gui when switching between application.

my gui gets an input file from the user, and on submission it start a worker thread to read and do some process.
When a user switches to other application and comes back, the screen doesn't repaints, i don't know how to do it.

Can someone help.

Thanks
Kasi
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19543
    
  16

How do you (re)paint your application?


SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
How To Ask Questions How To Answer Questions
Kasi Viswan
Ranch Hand

Joined: Sep 27, 2008
Posts: 42
i only know that i can call frame.repaint() or panel.repaint() when i want the gui to be repainted.

but i don't know how to call it when switching between applications, can you help?
Michael Dunn
Ranch Hand

Joined: Jun 09, 2003
Posts: 4632
> but i don't know how to call it when switching between applications

windowListener's windowActivated(..)
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19543
    
  16

But windows should get repainted automatically when it regains focus.
Michael Dunn
Ranch Hand

Joined: Jun 09, 2003
Posts: 4632
> But windows should get repainted automatically when it regains focus.

and the problem with putting the repaint code in windowActivated is ....?
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19543
    
  16

There is no problem with it, except that it should be redundant.
takatoshi yanase
Greenhorn

Joined: Mar 04, 2009
Posts: 6
Hello,

The following steps may solve your question.

............

Kasi Viswan
Ranch Hand

Joined: Sep 27, 2008
Posts: 42
I am so sorry, the problem was my understanding of thread concepts in Swing,
Changed some code and it worked.

I need to learn it quickly.

Thanks everyone for your advise.

Could you please tell me if my understanding of these concepts is correct,

SwingUtilities.invokeLater() and SwingUtilities.invokeAndWait() are two ways to start an event dispatch thread.
Anything that deals with painting the gui needs to be done in EDT.
If there is any task that would take more time to complete it needs to be done in Swing Worker Thread.
One can start a worker thread from an Event Dispatch thread.

Am using Java5, is there a way to get the progress of a task done in a worker thread and get it displayed in the gui when the task in the worker thread is still running. I know there is a way to do this in Java6 but is there a way to do it in java5? Please advise.

Thanks
Kasi
Kasi Viswan
Ranch Hand

Joined: Sep 27, 2008
Posts: 42
Missed this in my previous message,

Is it allowed/possible to start an EDT from inside another EDT.
Is it allowed to start an EDT from inside a Swing Worker Thread.

Please advise.
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19543
    
  16

SwingUtilities.invokeLater() and SwingUtilities.invokeAndWait() are two ways to start an event dispatch thread.

There is just one event dispatch thread. The two methods you named (which are actually aliases for EventQueue.invokeLater() and EventQueue.invokeAndWait()) will trigger an event on the existing EDT.

Anything that deals with painting the gui needs to be done in EDT.

Correct, but not just painting. All updates* to the GUI, including updating models, need to be done in the EDT.

* Well there are a few exceptions, like in JTextArea. All these exceptions are mentioned explicitly in the API.

If there is any task that would take more time to complete it needs to be done in Swing Worker Thread.
One can start a worker thread from an Event Dispatch thread.

Exactly. SwingWorker is a utility class you can use, but you can also use regular threads.

Am using Java5, is there a way to get the progress of a task done in a worker thread and get it displayed in the gui when the task in the worker thread is still running. I know there is a way to do this in Java6 but is there a way to do it in java5?

Since SwingWorker isn't part of the API in Java 5 or before, you will either need to create / find a replacement for it, or simply use EventQueue.invokeLater.

Is it allowed/possible to start an EDT from inside another EDT.

As said before, you trigger events, you don't start an EDT.

invokeLater is allowed to be called from the EDT. invokeAndWait isn't though; it will throw an Error. Fortunately, there is a method called isDispatchThread(), so you can check:


Is it allowed to start an EDT from inside a Swing Worker Thread.

It is allowed to trigger events from inside a Swing Worker Thread. That's what the mechanism is there for.
Kasi Viswan
Ranch Hand

Joined: Sep 27, 2008
Posts: 42
Thank you so much for taking time to clear my doubts.

Swing brings life to an application and so nice to instead of using the application from command prompt.
Swing has a learning curve but it is amazing what it can do for you when you get it to work.

I was breaking my head that my Gui freezes when given a task, but now overcame it with your help.

Thanks again,
Kasi
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Gui refresh
 
Similar Threads
How to navigate in a standalone application
Help! Java Newbie
How to handle booked records?
How to get search criteria:Field vs ComboBox
Capturing right-click from mouse in AWT