Results 1 to 9 of 9
Like Tree1Likes
  • 1 Post By newbie123

Thread: non-modal dialog issue

  1. #1
    newbie123 is offline Member
    Join Date
    Nov 2009
    Posts
    90
    Rep Power
    0

    Default non-modal dialog issue

    I have an issue with non-modal dialogs.

    I have a program that accesses a rather large remote database. As it takes a bit of time to complete the transaction with the database, I want to display a non-modal dialog asking the user "to be patient".

    The problem is that the label associated with the dialog does not become visible until after the database transaction is complete, thus defeating the purpose for the dialog.

    Why isn't the dialog's label shown when the dialog itself is displayed?

    I have ginned up a little program that essentially reproduces the behavior.

    Java Code:
    import java.awt.*; import java.awt.event.*;
    import javax.swing.*; import javax.swing.event.*;
    
    class DlgTest extends JFrame 
    {
       int iters = 10000000;
    
       JButton frameBtn;
       JDialog testDlg;
       JLabel testDlgLbl, frameLbl;
    
       DlgTest() 
       {
          JFrame frame = new JFrame( "Non-Modal Dialog Test" );
          frame.setSize( 200, 200 );
          frame.setLayout( new GridLayout( 2, 1 ) );
          frame.setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE );
    
          testDlg = new JDialog( frame, "Non-Modal Dialog", false );
          testDlg.setSize( 200, 100 );
          testDlgLbl = new JLabel( "This is a non-modal dialog.", SwingConstants.CENTER );
          testDlg.add( testDlgLbl );
    
          frameLbl = new JLabel( "iteration = ", SwingConstants.CENTER );
    
          frameBtn = new JButton( "Push Me" );
          frameBtn.addActionListener( new ActionListener()
          {
             public void actionPerformed( ActionEvent ae )
             {
                testDlg.setVisible( true );
     
                for ( int i = 0; i <= iters; i++ )
                {
                   frameLbl.setText( "iteration = " + i );
                }
    
                //testDlg.setVisible( false );
             }
          } );
    
          frame.add( frameLbl );
          frame.add( frameBtn );
    
          frame.setVisible( true );
       }
    
       public static void main( String[] args ) 
       {
          SwingUtilities.invokeLater( new Runnable() 
          {
             public void run() { new DlgTest(); }
          });
       }
    }

  2. #2
    DarrylBurke's Avatar
    DarrylBurke is offline Member
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,193
    Rep Power
    19

    Default Re: non-modal dialog issue

    If you're forever cleaning cobwebs, it's time to get rid of the spiders.

  3. #3
    newbie123 is offline Member
    Join Date
    Nov 2009
    Posts
    90
    Rep Power
    0

    Default Re: non-modal dialog issue

    Thanks. I'll given them a read.

  4. #4
    newbie123 is offline Member
    Join Date
    Nov 2009
    Posts
    90
    Rep Power
    0

    Default Re: non-modal dialog issue

    Thanks Darryl for pointing me in the right direction. My concurrency problems stretched far beyond non-modal dialogs. For example, in the code I posted earlier, the label also would not update.

    I have attached code that solves these issues. I would love if you (and other forum users) would comment on it.

    Java Code:
    import java.awt.*; import java.awt.event.*;
    import javax.swing.*; import javax.swing.event.*;
    
    class TestIt extends JFrame 
    {
       JButton startBtn, stopBtn;
       static JFrame frame;
       static JLabel frameLbl;
       JPanel buttonPnl;
    
       Thread t1, t2;
    
       TestIt() 
       {
          frame = new JFrame( "Concurrency Test" );
          frame.setSize( 200, 200 );
          frame.setLayout( new BorderLayout() );
          frame.setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE );
    
          frameLbl = new JLabel( "iteration = ", SwingConstants.CENTER );
    
          startBtn = new JButton( "Start" );
          startBtn.setPreferredSize( new Dimension( 75, 25 ) );
          startBtn.addActionListener( new ActionListener()
          {
             public void actionPerformed( ActionEvent ae )
             {
                Runnable r1 = new MyRunnable1();
                t1 = new Thread( r1 );
                t1.start();
    
                Runnable r2 = new MyRunnable2();
                t2 = new Thread( r2 );
                t2.start();
             }
          } );
    
          stopBtn = new JButton( "Stop" );
          stopBtn.setPreferredSize( new Dimension( 75, 25 ) );
          stopBtn.addActionListener( new ActionListener()
          {
             public void actionPerformed( ActionEvent ae )
             {
                t1.interrupt();
             }
          } );
    
          buttonPnl = new JPanel();
    
          buttonPnl.add( startBtn );
          buttonPnl.add( stopBtn );
    
          frame.add( frameLbl, BorderLayout.CENTER );
          frame.add( buttonPnl, BorderLayout.SOUTH );
    
          frame.setVisible( true );
       }
    
       public static void main( String[] args ) 
       {
          SwingUtilities.invokeLater( new Runnable() 
          {
             public void run() { new TestIt(); }
          });
       }
    } 
    
    class MyRunnable1 implements Runnable
    {
       int iters = 100;
    
       public void run()
       {
          try
          {
             for ( int i = 0; i <= iters; i++ )
             {
                TestIt.frameLbl.setText( "iteration = " + i );
                Thread.sleep( 100 );   
             }
          }
          catch ( InterruptedException ie )
          {
             TestIt.frameLbl.setText( "Thread has been interrupted!" );
          }
          finally
          {
             MyRunnable2.aDlg.setVisible( false );
          }
       }
    }
    
    class MyRunnable2 implements Runnable
    {
       static JDialog aDlg = new JDialog( TestIt.frame, "A Non-Modal Dialog", false );
       
       JLabel dialogLbl = new JLabel( "This is a non-modal dialog.", SwingConstants.CENTER );
    
       public void run()
       {
          aDlg.setSize( 200, 100 );
          aDlg.add( dialogLbl );
          aDlg.setVisible( true );
      }
    }

  5. #5
    DarrylBurke's Avatar
    DarrylBurke is offline Member
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,193
    Rep Power
    19

    Default Re: non-modal dialog issue

    The code in MyRunnable2's run() method calls Swing methods which need to be run on the EDT -- not on a background thread as in your code.

    Using meaningful variable and class names throughout would greatly imprive your chances of members actually going through the code. For example, I didn't need to look around to see what startBtn and stopBtn do, but I scrolled up and down about 4 times before I was sure you were calling Swing code off of the EDT, only because of the class names you chose for the two Runnables.

    db
    If you're forever cleaning cobwebs, it's time to get rid of the spiders.

  6. #6
    newbie123 is offline Member
    Join Date
    Nov 2009
    Posts
    90
    Rep Power
    0

    Default Re: non-modal dialog issue

    I want to make sure I fully understand your comment. Before the "Start" button is pressed, which creates two new threads, how many threads are running, one or two?

    I have read the admonition that one should not call Swing methods off the EDT. Why?

    I'll be sure to make the code I post in the future easier to read by documenting it more thoroughly.

  7. #7
    DarrylBurke's Avatar
    DarrylBurke is offline Member
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,193
    Rep Power
    19

    Default Re: non-modal dialog issue

    Quote Originally Posted by newbie123 View Post
    I have read the admonition that one should not call Swing methods off the EDT. Why?
    Threads and Swing
    The Last Word in Swing Threads

    Many more informative pages if you search the net for Swing single thread.

    db
    If you're forever cleaning cobwebs, it's time to get rid of the spiders.

  8. #8
    newbie123 is offline Member
    Join Date
    Nov 2009
    Posts
    90
    Rep Power
    0

    Default Re: non-modal dialog issue

    Thanks again Darryl! The articles were very informative. Things are now working as intended.
    DarrylBurke likes this.

  9. #9
    DarrylBurke's Avatar
    DarrylBurke is offline Member
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,193
    Rep Power
    19

    Default Re: non-modal dialog issue

    Good to know.

    db
    If you're forever cleaning cobwebs, it's time to get rid of the spiders.

Similar Threads

  1. Making a JInternalFrame modal
    By arkiegreg in forum AWT / Swing
    Replies: 0
    Last Post: 12-02-2010, 03:38 PM
  2. how to set modal to JFrame
    By newbiejava in forum New To Java
    Replies: 10
    Last Post: 02-17-2010, 03:30 PM
  3. How to set Modal to JFrame
    By newbiejava in forum New To Java
    Replies: 3
    Last Post: 02-02-2010, 01:49 PM
  4. Replies: 3
    Last Post: 01-16-2010, 07:14 PM
  5. Can't edit JTextField after modal dialog
    By JavaNerd in forum Java Applets
    Replies: 0
    Last Post: 02-07-2008, 09:11 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •