Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

Using setTitle() in modal JDialog

Posted on 2000-04-04
2
Medium Priority
?
2,623 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 400 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

Important Lessons on Recovering from Petya

In their most recent webinar, Skyport Systems explores ways to isolate and protect critical databases to keep the core of your company safe from harm.

Question has a verified solution.

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

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…
Introduction This article is the second of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers the basic installation and configuration of the test automation tools used by…
Viewers learn about the third conditional statement “else if” and use it in an example program. Then additional information about conditional statements is provided, covering the topic thoroughly. Viewers learn about the third conditional statement …
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:
Suggested Courses

610 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