Solved

Using setTitle() in modal JDialog

Posted on 2000-04-04
2
2,353 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
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 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

Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
Introduction This article is the first of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article explains our test automation goals. Then rationale is given for the tools we use to a…
Viewers will learn one way to get user input in Java. Introduce the Scanner object: Declare the variable that stores the user input: An example prompting the user for input: Methods you need to invoke in order to properly get  user input:
The viewer will learn how to implement Singleton Design Pattern in Java.

762 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

20 Experts available now in Live!

Get 1:1 Help Now