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

时间:2015-09-26 08:25:40

标签: java swing

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

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

这是代码

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

   }
}

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

2 个答案:

答案 0 :(得分:1)

编辑:mainFrame现在是一个带有null-parent的Dialog。 modality-type确保在显示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;
    }
}

答案 1 :(得分:0)

使用主题,因为您不知道用户完成大型机部分所需的费用。我们需要定期检查 - 大型机已经不见了。然后我们将得到值,否则我们得到null。

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.File;
import java.io.IOException;

public class FileMain {

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

        final FileChooser fileChosen = new FileChooser();
        fileChosen.showFileDialogDemo();
        // Here it is always return null 
        javax.swing.Timer t = new javax.swing.Timer(1000, new ActionListener() {
                 @Override
                public void actionPerformed(ActionEvent e) {

                        if(fileChosen.mainFrame.isVisible() == false){
                            String fileName = fileChosen.getFilePath();
                        System.out.println("\nIn main: "+fileName); // Always return null even before I click the file path.
                        File myFile = new File(fileName);
                        System.out.println(fileName);
                            System.exit(0);
                        }
                   }
                });
                t.start();
        }
   }



import java.awt.Button;
import java.awt.FileDialog;
import java.awt.FlowLayout;
import java.awt.Frame;
import java.awt.GridLayout;
import java.awt.Label;
import java.awt.Panel;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;

public class FileChooser {
    Frame mainFrame;
    Label headerLabel;
    Label statusLabel;
    Panel controlPanel;

 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() {
      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());
           mainFrame.setVisible(false);
             }
          });
       controlPanel.add(showFileDialogButton);

  }

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

   public String getFilePath() {
       return filePath;
   }
}