File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
Win a copy of Clojure in Action this week in the Clojure forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Gui refresh

 
Kasi Viswan
Ranch Hand
Posts: 42
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 20372
44
Chrome Eclipse IDE Java Windows
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
How do you (re)paint your application?
 
Kasi Viswan
Ranch Hand
Posts: 42
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 4632
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
> but i don't know how to call it when switching between applications

windowListener's windowActivated(..)
 
Rob Spoor
Sheriff
Pie
Posts: 20372
44
Chrome Eclipse IDE Java Windows
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
But windows should get repainted automatically when it regains focus.
 
Michael Dunn
Ranch Hand
Posts: 4632
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
> But windows should get repainted automatically when it regains focus.

and the problem with putting the repaint code in windowActivated is ....?
 
Rob Spoor
Sheriff
Pie
Posts: 20372
44
Chrome Eclipse IDE Java Windows
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
There is no problem with it, except that it should be redundant.
 
takatoshi yanase
Greenhorn
Posts: 6
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello,

The following steps may solve your question.

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

 
Kasi Viswan
Ranch Hand
Posts: 42
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 42
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 20372
44
Chrome Eclipse IDE Java Windows
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 42
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Consider Paul's rocket mass heater.
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic