unable to close OK button of JOption dialog when 2 dialogs are displayed.

1. Run the below sample program

2. Click the "Click me" button immediately.

3. two dialogs are displayed, one by mouse click and another by threa.d

4. Now OK button of both dialogs are not working.

Expected behaviour:

the ok button of second dialog shold be active and should close the dialog.

Sample:

import java.awt.FlowLayout;

import java.awt.event.ActionEvent;

import java.awt.event.ActionListener;

import javax.swing.*;

publicclass test

{

publicstaticvoid main(String arg[])

{

MainFrame frm1 =new MainFrame();

frm1.setVisible(true);

}

}

class MainFrameextends JFrameimplements Runnable, ActionListener

{

messagedlg errdlg =new messagedlg();

JButton btn;

MainFrame()

{

btn =new JButton("Click Me");

JPanel pnl =new JPanel();

this.getContentPane().add(pnl);

pnl.setLayout(new FlowLayout());

pnl.add(btn);

btn.addActionListener(this);

setSize(400,400);

setLocation(400,200);

Thread t =new Thread(this);

t.start();

}

publicvoid run()

{

errdlg.showError(this,"mainscreen message");

}

publicvoid actionPerformed(ActionEvent ae)

{

System.out.println("action start");

//SwingUtilities.invokeLater(Thread.currentThread());

errdlg.showError(this,"mainscreen action message");

System.out.println("action end");

}

}

class messagedlg

{

publicstaticvoid showError(JFrame frame, String msg)

{

try{Thread.sleep(1000);}catch(Exception ex){}

Object[] options ={"OK"};

int n = JOptionPane.showOptionDialog(frame,(Object)msg, msg,

JOptionPane.OK_OPTION,1,

null,//don't use a custom Icon

options,//the titles of buttons

options[0]);//default button title

}

}

[3837 byte] By [palaniscreena] at [2007-10-3 4:16:33]
# 1

Some things you need to know.

1. NEVER create or modify UI objects outside of the event dispatch thread (EDT) - you are doing this when you pop up the dialog on the new thread

2. NEVER sleep on the EDT - you are doing this when you respond to the action event

3. When you make mistake number 1, if you feed the same root frame to two modal dialogs on different threads then, well, on your head be it

Some reading for you, before you get into more trouble ;o)

http://java.sun.com/products/jfc/tsc/articles/threads/threads1.html

itchyscratchya at 2007-7-14 22:18:06 > top of Java-index,Desktop,Core GUI APIs...