Solved

Disable/Enabled JDialog causes repaint

Posted on 2000-03-23
9
723 Views
Last Modified: 2008-03-10
Hi,

Sometimes in my program, I would like to disable a JDialog and change the mouse cursor to the Cursor.WAIT_CURSOR when the program is doing some processing. The problem is that when I disabled the JDialog throught setEnabled(false), the whole JDialog would flicker because it was repainted. What I don't understand is why the JDialog should repaint when I disabled (or enabled) it. Is there a better way of doing this?
0
Comment
Question by:yongsing
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 5
  • 4
9 Comments
 
LVL 4

Expert Comment

by:jerch
ID: 2651846
Hello yongsing...
For you to be able to avoid calling the paint method, a simple solution is to override the update method since the repaint method simply forwards it's call to the update method and the update method will clear the graphics or the jdialog (in your case) and schedule a call for the paint method.  Override the method update method with blank implementation.

cheers...
Jerson
0
 
LVL 9

Author Comment

by:yongsing
ID: 2652504
This is what I found in the JDialog class implementation:

    /**
     * Just calls <code>paint(g)</code>.  This method was overridden to
     * prevent an unneccessary call to clear the background.
     */
    public void update(Graphics g) {
        paint(g);
    }

Apparently the graphics isn't cleared.

Here's the setEnabled implementation:

        public void setEnabled(boolean b) {
            JDialog.this.setEnabled(b);
        }
What's JDialog.this.setEnabled(b) suppose to mean?
0
 
LVL 9

Author Comment

by:yongsing
ID: 2652558
This is what I found in the JDialog class implementation:

    /**
     * Just calls <code>paint(g)</code>.  This method was overridden to
     * prevent an unneccessary call to clear the background.
     */
    public void update(Graphics g) {
        paint(g);
    }

Apparently the graphics isn't cleared.

Here's the setEnabled implementation:

        public void setEnabled(boolean b) {
            JDialog.this.setEnabled(b);
        }
What's JDialog.this.setEnabled(b) suppose to mean?
0
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 4

Expert Comment

by:jerch
ID: 2652966
What you see is actually the setEnabled method of the inner class AccessibleJDialog.  For a method of an inner class to call a method of the enclosing class (if the implementation of the enclosing class is hidden, which means that the inner class has a method with the same name and signature as the enclosing class, same case with the setEnabled method).  This is how you call it.  

<Enclosing Class>.this.<methodname>

e.g.

class Enclosing {
public void myMethod() {
//blah blah...
}

private class InnerClass {
public void myMethod() {
Enclosing.this.myMethod(); //this is how you call the method of the enclosing class inside an inner class.
}
}
}

For you to be able to see the code of the setEnabled of the JDialog.  Check the source code of the class Component since

java.awt.Component        |        +--java.awt.Container              |
              +--java.awt.Window                    |
                    +--java.awt.Dialog                          |
                          +--javax.swing.JDialog

and Container, Window and Dialog does not override the setEnabled method.  It now leaves you with the setEnabled of the Component class.

sincerely yours...
Jerson
0
 
LVL 9

Author Comment

by:yongsing
ID: 2659438
Hi,

I got what you mean, but what I don't understand now is this: the update method has already been overridden in JDialog, so how come the graphics is still being cleared?
0
 
LVL 4

Expert Comment

by:jerch
ID: 2659642
Yes, you're right the update method is overriden but the code actually calls the paint() method which cause it to repaint.

    public void update(Graphics g) {
        paint(g);
    }

Here's my suggestion.... Look at the skeleton class.

class MyJDialog extends JDialog {
    boolean refresh = true;
    public void setEnabled(boolean b) {
        refresh = false;
        super.setEnabled(b);
    }
   
    public void update(Graphics g) {
        if (update)
            paint(g);
        else
            update = true;                              
    }
}

I haven't tried this code but I think it will work. It will not work if the setEnable calls the update method more than once. Tell me the result.

cheers....
Jerson

0
 
LVL 9

Author Comment

by:yongsing
ID: 2671813
Hi,

Your code doesn't work. In your update(Graphics g) method, is the variable 'update' actually 'refresh'?

I looked at the setEnabled() method in the Component class, and it looks like it never asks for a repaint at all, and there isn't any override of this method in Container, Window, Dialog, and JDialog. So why is the JDialog repainted whenever setEnabled() is called?
0
 
LVL 4

Accepted Solution

by:
jerch earned 50 total points
ID: 2671982
Hi yongsing...
I tried disabling the JDialog. It's not calling the repaint method. Check this site http://www.javaworld.com/javaworld/javatips/jw-javatip87.html

You don't actually have to disable the dialog box.

best regards...
Jerson  
0
 
LVL 9

Author Comment

by:yongsing
ID: 2705943
Actually I do know about that web site, but I was hoping for something simpler.
0

Featured Post

MS Dynamics Made Instantly Simpler

Make Your Microsoft Dynamics Investment Count  & Drastically Decrease Training Time by Providing Intuitive Step-By-Step WalkThru Tutorials.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
configure dependency in POM for new database 3 47
check java version using powershell 13 306
Java basic valueOf question 1 35
Eclipse for Java EE development 2 45
In this post we will learn different types of Android Layout and some basics of an Android App.
In this post we will learn how to connect and configure Android Device (Smartphone etc.) with Android Studio. After that we will run a simple Hello World Program.
This theoretical tutorial explains exceptions, reasons for exceptions, different categories of exception and exception hierarchy.
Viewers will learn how to properly install Eclipse with the necessary JDK, and will take a look at an introductory Java program. Download Eclipse installation zip file: Extract files from zip file: Download and install JDK 8: Open Eclipse and …

730 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question