Ernestas Gruodis
Ernestas Gruodis

Reputation: 8787

How to know that paintComponent(Graphics g) has finished it's job (Java)?

How to know that paintComponent(Graphics g) has finished it's job?

I can do:

 @Override
 public void paintComponent(Graphics g) {
    try {
         //Paint some stuff
    } finally {
         //Do something after painting
    }
 }

Is there any other way?

Upvotes: 2

Views: 261

Answers (2)

Ernestas Gruodis
Ernestas Gruodis

Reputation: 8787

I liked this example from SwingWorker documentation:

   final JLabel label;
   class MeaningOfLifeFinder extends SwingWorker<String, Object> {
       @Override
       public String doInBackground() {
           return findTheMeaningOfLife();
       }

       @Override
       protected void done() {
           try { 
               label.setText(get());
           } catch (Exception ignore) {
           }
       }
   }

   (new MeaningOfLifeFinder()).execute();

This simple example with a little modification could be very helpful in my situation. P.S. Does anybody know the source code of findTheMeaningOfLife(); method above?.. :))

Upvotes: 1

Hovercraft Full Of Eels
Hovercraft Full Of Eels

Reputation: 285405

Your question is:

How to know that paintComponent(Graphics g) has finished it's job?

And then you state in a comment to the question:

I created the button (simple JComponent), and it must first to fade out, and right after that perform actionlistener.actionPerformed(someEvent)

As I suspected, you appear to be doing something wrong inside of paintComponent since the driver behind the fading out should not be inside of this method. The method should be fast as all get out, it should complete in the figurative "blink of an eye", and should not have any delays or fading inside of it. Rather you'll likely use a Swing Timer to change a class field that the button uses for it's transparency, calling repaint after each change of state, and then when the Timer is done, you call your code do whatever action you desire.

For more specific help, consider creating and posting an sscce.

Upvotes: 5

Related Questions