2015-09-26 57 views
1

目前我有麻煩的事件驅動文件對話框傳遞給一個變量如何從java文件對話框中獲取值並將其傳遞給變量?

這檔節目應該如何工作,用戶按下一個文件路徑,那麼這將是在文件保存對象,準備進一步操作

下面的代碼

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

    } 
} 

什麼是邏輯得到來自事件驅動對象的值?

+0

Post FileDemo code。 – Amila

+0

我的壞它應該是FileChoser –

+0

而你的showFileDialogDemo()方法? – Amila

回答

1

編輯: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; 
    } 
} 
+0

爲什麼要擴展從'框架',這只是混淆事件 – MadProgrammer

+0

@MadProgrammer是的,我同意,但問題是,對話框需要一個父窗口。這就是爲什麼我說它需要優化。 Imo真正的問題在於主要的println的調用。相反,這個調用應該在'FileChooser'或'mainFrame'內。缺少的是實際解決方案的確切用途。 –

+0

你可以用'null'離開父母 – MadProgrammer

0

使用線程,因爲您不知道用戶完成大型機部分需要多少時間。我們需要定期檢查 - 大型機已經不在了。然後我們會得到這個值,否則我們會得到空值。

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; 
    } 
} 
+0

或者只是使用一個模式'Dialog'其目的只是這種情況下 – MadProgrammer

+0

如果(fileChosen.mainFrame.isVisible)有錯誤,真的不知道是什麼問題 –

+0

什麼是錯誤?如果你需要使用,那麼使用'if(fileChosen.mainFrame.isVisible())',在isVisible之後不要使用'() –

相關問題