java - 如何从 java 文件对话框获取值并将其传递给变量?

标签 java swing

目前,我无法将事件驱动的文件对话框传递给变量

这就是程序应该如何工作,用户按下文件路径,然后它将保存在文件对象中以供进一步操作

这是代码

public class FileChooser {

 private String filePath;      

  public FileChooser(){
          prepareGUI();
  }

 private void prepareGUI(){
  mainFrame = new Frame("Naufal File Chooser");
  mainFrame.setSize(400,400);
  mainFrame.setLayout(new GridLayout(3, 1));
  mainFrame.addWindowListener(new WindowAdapter() {
     public void windowClosing(WindowEvent windowEvent){
        System.exit(0);
     }        
  });    
  headerLabel = new Label();
  headerLabel.setAlignment(Label.CENTER);
  statusLabel = new Label();        
  statusLabel.setAlignment(Label.CENTER);
  statusLabel.setSize(350,100);

  controlPanel = new Panel();
  controlPanel.setLayout(new FlowLayout());

  mainFrame.add(headerLabel);
  mainFrame.add(controlPanel);
  mainFrame.add(statusLabel);
  mainFrame.setVisible(true); 


 }


  public void showFileDialogDemo(){
     final FileDialog fileDialog = new FileDialog(mainFrame,"Select file");
      Button showFileDialogButton = new Button("Open File");
      showFileDialogButton.addActionListener(new ActionListener() {
      @Override
      public void actionPerformed(ActionEvent e) {
           fileDialog.setVisible(true);
           statusLabel.setText("File Selected :" 
           + fileDialog.getDirectory() + fileDialog.getFile());
                //Here is where I should get the value, I tried set and get and as well as using return at the bottom both return null
                // I'm trying to use setFilePath to store the file
           setFilePath(fileDialog.getDirectory() + fileDialog.getFile());
             }
          });
       controlPanel.add(showFileDialogButton);
       mainFrame.setVisible(true);  
  }

   public void setFilePath(String file) {
       this.filePath = file;
   }

   public String getFilePath() {
       return filePath;
   }

    }

在main.java中

public class FileMain {

    public static void main(String[] args) throws IOException {

        FileChooser fileChosen = new FileChooser();
        fileChosen.showFileDialogDemo();
        // Here it is always return null
        String fileName = fileChosen.getFilePath();
        System.out.println(fileName); // Always return null even before I click the file path.
        File myFile = new File(fileName);

   }
}

从事件驱动对象获取值的逻辑是什么?

最佳答案

编辑:mainFrame 现在是一个具有空父级的对话框。模态类型确保一旦显示 mainFrame 代码就会被阻止。

public static void main(String[] args) throws IOException {

    FileChooser fileChosen = new FileChooser();
    fileChosen.showFileDialogDemo();

    String fileName = fileChosen.getFilePath();
    // This is called as soon as mainFrame is hidden
    System.out.println(fileName);
}

public static class FileChooser {

    private String filePath;
    // This is now a Dialog instead of a frame
    private Dialog mainFrame;

    public FileChooser() {
        prepareGUI();
    }

    private void prepareGUI() {
        // APPLICATION_MODAL makes sure the code is blocked once mainFrame is shown.
        mainFrame = new Dialog(null, "Naufal File Chooser", Dialog.ModalityType.APPLICATION_MODAL);
        mainFrame.setSize(400, 400);
        mainFrame.setLayout(new GridLayout(3, 1));
        mainFrame.addWindowListener(new WindowAdapter() {
            @Override
            public void windowClosing(WindowEvent windowEvent) {
                System.exit(0);
            }
        });
    }

    public void showFileDialogDemo() {
        Button showFileDialogButton = new Button("Open File");
        showFileDialogButton.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                FileDialog fileDialog = new FileDialog(mainFrame, "Select file");
                fileDialog.setVisible(true);
                setFilePath(fileDialog.getDirectory() + fileDialog.getFile());
                // This is to make sure the code resumes where it was blocked
                mainFrame.setVisible(false);
            }
        });
        mainFrame.add(showFileDialogButton);
        mainFrame.setVisible(true);
    }

    public void setFilePath(String file) {
        this.filePath = file;
    }

    public String getFilePath() {
        return filePath;
    }
}

关于java - 如何从 java 文件对话框获取值并将其传递给变量?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32795125/

相关文章:

java - 字符串未在条件中声明?

java - map 位置更改监听器未触发

Java Swing架构问题?

java - 高对比度模式下的 JTree View

java - 无法将 ArrayList<String> 转换为 ArrayList<java.lang.String>

java - 使用 SpinnerDateModel 仅返回时间

java - 为什么 JVM 显示错误的时区 (UTC+02 :00) Istanbul?

java - 在两个线程中使用相同的变量,但我不知道为什么它们不会在 Java 中更新

java - 执行maven命令即mvn install时发生异常

java - 程序跳过我创建 JTable 的行