如何等待FileChooser选择?

时间:2015-09-07 07:58:50

标签: java path selection wait filechooser

我想要做的是使用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);

提前致谢

P.S。 这是我的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 ");
        }

1 个答案:

答案 0 :(得分:-1)

您可以使用均衡的听众:

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.
        }
   }
});

取代String path = panel.getPath();

希望这有帮助。