?
Solved

Using setTitle() in modal JDialog

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

Optimize your web performance

What's in the eBook?
- Full list of reasons for poor performance
- Ultimate measures to speed things up
- Primary web monitoring types
- KPIs you should be monitoring in order to increase your ROI

Question has a verified solution.

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

Java contains several comparison operators (e.g., <, <=, >, >=, ==, !=) that allow you to compare primitive values. However, these operators cannot be used to compare the contents of objects. Interface Comparable is used to allow objects of a cl…
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 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 …
The viewer will learn how to implement Singleton Design Pattern in Java.
Suggested Courses

777 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