I am using JAVA Swing to create a very basic UI. When I run the program, a window will open with a message and browse button(using frame and JButtons for the same). On click of browse button, another window will open to navigate to the file. This I have achieved by calling a FileChooser on the click event of Browse button. However, my program does not wait for user input. The first window with browse button opens and program keeps on executing and ends up in an error as no file has been selected. How do I halt the execution till user input is provided? In a forum it was advised to use showOpenDialog() method of browser but that straightway opens a browsing window, whereas I want to give the provision to user to click on Browse buttonbrowsewindow pick file window
My code is below
frame.setLayout(new FlowLayout());
// set up a file picker component
JFilePicker filePicker = new JFilePicker("Pick a file", "Browse...");
filePicker.setMode(JFilePicker.MODE_OPEN);
filePicker.addFileTypeFilter(".jpg", "JPEG Images");
filePicker.addFileTypeFilter(".mp4", "MPEG-4 Videos");
// access JFileChooser class directly
JFileChooser fileChooser = filePicker.getFileChooser();
fileChooser.setCurrentDirectory(new File("C:/"));
// add the component to the frame
frame.add(filePicker);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(520, 100);
frame.setLocationRelativeTo(null); // center on screen
frame.setVisible(true);
System.out.println();
JPicker is the custom class which creates a filechooser and sets things to be done on click of Browse button