简体   繁体   中英

Wait for user input Java Swing

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 button browsewindow 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

Of Course, You set the JFrame visible at the end of its' initialization. You need to do this within the main() method of your startup class. Where is yours?

The JFilePicker (created by: Nam Ha Minh ) is applied to a JFrame as a Java Component in order to save a little time in GUI development. I personally would just use the JFileChooser directly within a JButton ActionPerformed event. If you had followed the directions properly then you would see that you need a main() method which only makes sense. What your application Startup class should look like is something like this:

import java.awt.FlowLayout;
import java.io.File;
import javax.swing.JFileChooser;
import javax.swing.JFrame;
import javax.swing.SwingUtilities;

public class TestJFilePicker extends JFrame {

    private static final long serialVersionUID = 1L;

    public TestJFilePicker() {
        super("Test using JFilePicker");

        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("D:/"));

        // add the component to the frame
        add(filePicker);

        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setSize(520, 100);
        setLocationRelativeTo(null);    // center on screen
    }

    public static void main(String[] args) {
        SwingUtilities.invokeLater(new Runnable() {
            @Override
            public void run() {
                new TestJFilePicker().setVisible(true);
            }
        });
    }

}

The above code (which is the work of Nam Ha Minh ) of course assumes that you have already applied the JFilePicker and the FileTypeFilter class files to your project. Without them the above code will not work.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM