We help IT Professionals succeed at work.

open a JFileChooser/ modal window will block the event dispatching thread

pdidominic
pdidominic asked
on
Medium Priority
906 Views
Last Modified: 2013-11-23
Please tell me whether opening a JFileChooser/ modal window in event handling code will block the event dispatching thread.

Thank you
Comment
Watch Question

CERTIFIED EXPERT
Top Expert 2016

Commented:
Yes it will. Use a SwingWorker
Java Developer
CERTIFIED EXPERT
Top Expert 2010
Commented:
Unlock this solution and get a sample of our free trial.
(No credit card required)
UNLOCK SOLUTION
Mick BarryJava Developer
CERTIFIED EXPERT
Top Expert 2010

Commented:
If it did then opening JFileChooser would freeze up your gui
you'll find example and discussion here
http://java.sun.com/docs/books/tutorial/uiswing/components/filechooser.html
CERTIFIED EXPERT
Top Expert 2016

Commented:
>>If it did then opening JFileChooser would freeze up your gui

(which is exactly what it does do)

All you need to do to demonstrate this is to try to close your main window with the file chooser dialog open

Author

Commented:
i have a little confuse here. Despite the JFileChooser dialog blocks the actionPerform event handling code as the following snippet but the event dispatching thread is still executed :-??

public class FileChooser extends JFrame
{
	public FileChooser()
	{
		initComponents();
		Runnable runner = new Runnable()
		{
			public void run()
			{
				ActionListener actionListener = new ActionListener()
				{
					public void actionPerformed(ActionEvent actionEvent)
					{
						testTxt.setText(testTxt.getText() + 'a');
						System.out.println(SwingUtilities.isEventDispatchThread());
					}
				};
				Timer timer = new Timer(500, actionListener);
				timer.start();
			}
		};
		EventQueue.invokeLater(runner);
	}
 
	public static void main(String[] args)
	{
		
		new FileChooser().setVisible(true);
		
	}
 
	private void startBtnActionPerformed(ActionEvent e)
	{
		JFileChooser fileChoose = new JFileChooser();
		
		System.out.println(SwingUtilities.isEventDispatchThread() + "---------");
		fileChoose.showOpenDialog(FileChooser.this);
		System.out.println(SwingUtilities.isEventDispatchThread() + "---------");
	}

Open in new window

ksivananthVice President

Commented:
>>All you need to do to demonstrate this is to try to close your main window with the file chooser dialog open

I think, object is right, it will block UI( parent ) but not the event dispatching thread! if the event dispatching thread blocked, you will not see the drawing in the back UI, all will be greyed out!

Author

Commented:
in this snippet, i have a simple form with a textarea and a button. Pressing the button will open a file chooser dialog. I also have a Timer that constantly updates the textarea.
CERTIFIED EXPERT
Top Expert 2016

Commented:
>>it will block UI( parent )

UIs don't get blocked - only threads get blocked ;-)
CERTIFIED EXPERT
Top Expert 2016

Commented:
http://en.wikipedia.org/wiki/Modal_window

Look at the first sentence under criticisms (which to me is absurd - if you don't want it to block workflow then don't make it modal)
Mick BarryJava Developer
CERTIFIED EXPERT
Top Expert 2010

Commented:
> but the event dispatching thread is still executed :-??

thats because the event dispatch thread does *not* get blocked as I stated above.
Mick BarryJava Developer
CERTIFIED EXPERT
Top Expert 2010

Commented:
> in this snippet, i have a simple form with a textarea and a button. Pressing the button will open a
> file chooser dialog. I also have a Timer that constantly updates the textarea.

And does it continue to update?  That should verify whether it is blocked or not.
Mick BarryJava Developer
CERTIFIED EXPERT
Top Expert 2010

Commented:
(-:
Unlock the solution to this question.
Thanks for using Experts Exchange.

Please provide your email to receive a sample view!

*This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.

OR

Please enter a first name

Please enter a last name

8+ characters (letters, numbers, and a symbol)

By clicking, you agree to the Terms of Use and Privacy Policy.