Solved

Using setTitle() in modal JDialog

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

Gigs: Get Your Project Delivered by an Expert

Select from freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely and get projects done right.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
backtracking recursion  code 19 57
How  Can  I  resolve   this  Error "java.sql.SQLException: Invalid column index" 4 37
arguments to jar 5 26
throw exception 21 43
INTRODUCTION Working with files is a moderately common task in Java.  For most projects hard coding the file names, using parameters in configuration files, or using command-line arguments is sufficient.   However, when your application has vi…
Are you developing a Java application and want to create Excel Spreadsheets? You have come to the right place, this article will describe how you can create Excel Spreadsheets from a Java Application. For the purposes of this article, I will be u…
Video by: Michael
Viewers learn about how to reduce the potential repetitiveness of coding in main by developing methods to perform specific tasks for their program. Additionally, objects are introduced for the purpose of learning how to call methods in Java. Define …
This video teaches viewers about errors in exception handling.

785 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