Solved

Disable/Enabled JDialog causes repaint

Posted on 2000-03-23
9
710 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
IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

 
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

Free Trending Threat Insights Every Day

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

Suggested Solutions

Introduction Java can be integrated with native programs using an interface called JNI(Java Native Interface). Native programs are programs which can directly run on the processor. JNI is simply a naming and calling convention so that the JVM (Java…
Java Flight Recorder and Java Mission Control together create a complete tool chain to continuously collect low level and detailed runtime information enabling after-the-fact incident analysis. Java Flight Recorder is a profiling and event collectio…
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…
This tutorial will introduce the viewer to VisualVM for the Java platform application. This video explains an example program and covers the Overview, Monitor, and Heap Dump tabs.

760 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

18 Experts available now in Live!

Get 1:1 Help Now