Solved

Disable/Enabled JDialog causes repaint

Posted on 2000-03-23
9
713 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
  • 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
 
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
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
java 8 lambda expresssions exception handling 3 72
Java Server Faces parameter pass? 6 37
how do i compare an object based on two fields 6 48
varialbe initialization 11 30
For customizing the look of your lightweight component and making it look opaque like it was made of plastic.  This tip assumes your component to be of rectangular shape and completely opaque.   (CODE)
Basic understanding on "OO- Object Orientation" is needed for designing a logical solution to solve a problem. Basic OOAD is a prerequisite for a coder to ensure that they follow the basic design of OO. This would help developers to understand the b…
Viewers will learn about the different types of variables in Java and how to declare them. Decide the type of variable desired: Put the keyword corresponding to the type of variable in front of the variable name: Use the equal sign to assign a v…
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 …

932 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

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now