Java 如何等待文件选择器选择?

Java 如何等待文件选择器选择?,java,path,selection,wait,filechooser,Java,Path,Selection,Wait,Filechooser,我要做的是使用FileChooser来选择路径 选择后,以下实例应使用该路径 我的问题是如何强制所有东西在路径上等待,因为否则程序只是在不等待的情况下运行 //GUI JFrame frame = new JFrame("Window"); FileChooser panel = new FileChooser(); frame.addWindowListener( new WindowAdapter() { public void

我要做的是使用FileChooser来选择路径

选择后,以下实例应使用该路径

我的问题是如何强制所有东西在路径上等待,因为否则程序只是在不等待的情况下运行

    //GUI 
    JFrame frame = new JFrame("Window");
    FileChooser panel = new FileChooser();

    frame.addWindowListener(
      new WindowAdapter() {
        public void windowClosing(WindowEvent e) {
          System.exit(0);
          }
        }
      );
    frame.getContentPane().add(panel,"Center");
    frame.setSize(panel.getPreferredSize());
    frame.setVisible(true);
    if(panel.getPath() == null){

    }
    String path = panel.getPath();

    //some additional stuff that does not need any pathinformation
    .......
    //next step calculation which runs without waiting 
    Calculation calc = new Calculation();
    calc.run(path);
提前谢谢

附言。 这就是我的ActionListner所包含的内容

        if (result == JFileChooser.CANCEL_OPTION) {
        System.out.println("Cancel was selected");
        }
        else if (result == JFileChooser.APPROVE_OPTION) {
        path = chooser.getSelectedFile().getAbsolutePath();
        System.out.println("getCurrentDirectory(): "

                + chooser.getCurrentDirectory());
        System.out.println("getSelectedFile() : "
                + chooser.getSelectedFile());
        }
        else {
             System.out.println("No Selection ");
        }

您可以使用均匀的侦听器:

panel.addActionListener(new ActionListener() {
   @Override
   public void actionPerformed(ActionEvent e){
        if (e.getActionCommand() == JFileChooser.APPROVE_OPTION) {
            file = panel.getSelectedFile();
            // Do what you want with selected file  
        } else {
            // When user pressed close or "X" in the corder.
        }
   }
});
代替字符串path=panel.getPath


希望这有帮助。

您想了解模态对话框。看看这里的例子:你是说这个吗?先看看@JürgenK。我想他们的意思是,JFileChooser已经在它自己的线程中运行了。这就是panel.getPath无法工作的原因。因此,在不同的线程中处理文件选择很可能是最简单的方法。这个程序根本就不需要等待时间result@J尤尔根克。我知道这不是等待。这就是我不工作的意思。原因是不等待是因为我解释的线程问题。解决此问题的一种方法是创建事件侦听器。另一种不推荐的方法是:while path=panel.getPath!=空{}。然而,这将带来更多的问题。