Solved

Using setTitle() in modal JDialog

Posted on 2000-04-04
2
2,518 Views
Last Modified: 2012-05-04
In my "real" code, the first call to setTitle in a modal JDialog sets the title; then, future calls seem to have no effect.  Borrowing some code from a PAQ (thanks mjenkins!), I distilled it pretty well down (see below) - just switch the dialog from non-modal to modal to cause the problem.

Any clues?

I'm using JDK 1.2.2 on Windows NT 4.

Thanks in advance!

import java.awt.*;
import javax.swing.*;

public class Title
        implements Runnable
{
        boolean b = true;
        private JDialog f;

        public static void main( String args[] )
        {
                Title foo = new Title();
                new Thread( foo ).start();
        }

        public Title()
        {
                f = new JDialog((Frame)null, false);  //works
                //f = new JDialog((Frame)null, true);  //does not work
        }

        public void run()
        {
                f.setSize( 400,400 );
                f.show();

                while( true )
                {
                        step();
                        try{
                        Thread.sleep(500);
                        }
                        catch( Exception e ) {}
                }
        }

        public void step()
        {
                  SwingUtilities.invokeLater(new Runnable()
                  {
                        public void run()
                        {
                b = !b;
                f.setTitle( b ? "True" : "False" );
                f.repaint(1);
                              
                        }
                  });
        }
}
0
Comment
Question by:azami
[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
2 Comments
 
LVL 2

Accepted Solution

by:
stalefish earned 100 total points
ID: 2686133
I think a modal dialog holds a lock (of sorts) on the system. The whole idea behind a modal component is one that holds up the program until it is executed. You can see from the code I modified, that you can change the dialog from within.

import java.awt.*;
import javax.swing.*;

public class Title /*implements Runnable*/ {
  private MyDialog f;

  public static void main( String args[] ) {
          Title foo = new Title();
         // new Thread( foo ).start();
  }

  public Title() {
          //f = new JDialog((Frame)null, false);  //works
          f = new MyDialog(null);  //does not work
  }
 }
      
class MyDialog extends JDialog implements Runnable{
  boolean b = true;
      Thread runner;
      public MyDialog(Frame owner){
            super(owner, true);
            runner = new Thread(this);
            runner.start();
            this.setSize( 400,400 );
    this.show();
      }
      
      public void run() {    
    while( true ){
           b = !b;
      this.setTitle( b ? "True" : "False" );
      this.repaint();

      try{ Thread.sleep(500); }
      catch( Exception e ) {}
    }
  }
 
}
0
 
LVL 1

Author Comment

by:azami
ID: 2687301
Yep, that sure 'nuff fixes the code.  Boy, is my face red... of course in my code the thread blocks when it calls f.show(), and doesn't reach the while loop until after the dialog is closed....

Turns out to not really be the problem I had, though - but it got me on the right track.  When trying to put together a new "simple" class that demonstrated my problem, I noticed that my code to change the title was only called once (in the constructor), so of course my title was never changing.

Guess that's what happens when working late at night...

Thanks!
0

Featured Post

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

An old method to applying the Singleton pattern in your Java code is to check if a static instance, defined in the same class that needs to be instantiated once and only once, is null and then create a new instance; otherwise, the pre-existing insta…
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 arithmetic and Boolean expressions in Java and the logical operators used to create Boolean expressions. We will cover the symbols used for arithmetic expressions and define each logical operator and how to use them in Boole…
This theoretical tutorial explains exceptions, reasons for exceptions, different categories of exception and exception hierarchy.

717 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