Use a thread to wait until the user has picked a file


I have a mainClass in Java, that starts a GUI in swing. I ask the user to open a file using a JFileChooser. I want the main to wait until the user has finished picking the file and then continue with the rest of the code in main. How do I do this using threads? Thanks in advance.

Here is the skeleton code:

public class MainClass { public static void main(String[] args) { GUI gui= new GUI(); //wait for user input here //Continue with code System.out.println("User has picked a file"); } }


public class GUI{ //User picks file using JFileChooser JFileChooser chooseFile= new JFileChooser(); //Notify mainclass we're done with fiction to continue with code }


OK, Two things.

<h3>You don't need multiple threads</h3>

The thing is, you can accomplish your goal of waiting for a user to select a file simply by using a Modal dialog. This works about like the following:

import javax.swing.*; public class DialogTest { public static void main(String[] args) { JFileChooser chooser = new JFileChooser(); chooser.showOpenDialog(null); System.out.println("File chooser is now closed. File is: " + chooser.getSelectedFile().toString()); } }

The showOpenDialog method will not return until the user has either selected a file, clicked cancel, or else clicked the X. Just be aware that getSelectedFile() will return null if the user cancels.

<h3>If you do need threads (you know, for something else)</h3>

Swing uses what it calls the <a href="https://docs.oracle.com/javase/tutorial/uiswing/concurrency/dispatch.html" rel="nofollow">Event Dispatch Thread</a>. Swing is not thread safe, as mentioned in the comment. What this means is that any and all method calls to Swing components should be done from the EDT. You can schedule code to be run on the EDT by using SwingUtilities.invokeLater(Runnable). You can schedule something to run in a background thread (using a thread pool) by using a <a href="https://docs.oracle.com/javase/tutorial/uiswing/concurrency/worker.html" rel="nofollow">Swing Worker</a>. Most of your code will probably just run on the EDT. Long-running operations can be sent to a background thread using swing workers.


  • Unexpected namespace prefix “xmlns” found for tag fragment
  • Referencing a CDI Bean in a non managed CDI Bean
  • What are the difference between Cherry-pick and patch apply?
  • Rails 3 Link_to :remote is not triggering RJS
  • How does this compute ? I am trying to understand how the values of H get assigned in the list
  • How to avoid the use of Spring annotations in my java beans and use them only in the configuration c
  • SQL Server: preventing dirty reads in a stored procedure
  • Wrong row deleted from custom listview with spinner
  • Reading a file into a multidimensional array
  • Display java JPanel in a JFrame
  • App restarts from wrong activity
  • How to Cache Real-time Data?
  • why do I get the error when installing the gem 'pg'? [duplicate]
  • Jetty Server not starting: Unable to establish loopback connection
  • FileReader+canvas image loading problem
  • Why value captured by reference in lambda is broken? [duplicate]
  • Spring Data JPA custom method causing PropertyReferenceException
  • Java Scanner input dilemma. Automatically inputs without allowing user to type
  • Possible to stop flickering java tooltip in heavyweight mode?
  • sending/ receiving email in Java
  • How to set my toolbar fixed while scrolling android
  • what is the difference between the asp.net mvc application and asp.net web application
  • Timeout for blocking function call, i.e., how to stop waiting for user input after X seconds?
  • AT Commands to Send SMS not working in Windows 8.1
  • Matrix multiplication with MKL
  • Windows forms listbox.selecteditem displaying “System.Data.DataRowView” instead of actual value
  • Proper folder structure for lots of source files
  • Change div Background jquery
  • How does Linux kernel interrupt the application?
  • Authorize attributes not working in MVC 4
  • apache spark aggregate function using min value
  • EntityFramework adding new object to nested object collection
  • Checking variable from a different class in C#
  • Busy indicator not showing up in wpf window [duplicate]
  • Sorting a 2D array using the second column C++
  • Binding checkboxes to object values in AngularJs
  • failed to connect to specific WiFi in android programmatically
  • Python/Django TangoWithDjango Models and Databases
  • Net Present Value in Excel for Grouped Recurring CF
  • How can I use threading to 'tick' a timer to be accessed by other threads?