Re: non-modal dialog issue
Re: non-modal dialog issue
Thanks. I'll given them a read.
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.
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 );
}
}
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
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.
Re: non-modal dialog issue
Quote:
Originally Posted by
newbie123
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
Re: non-modal dialog issue
Thanks again Darryl! The articles were very informative. Things are now working as intended.
Re: non-modal dialog issue