2012-06-29 76 views
0

我想爲JFileChooser按鈕使用Abstract操作,因爲會有幾十個這樣的操作。Swing:在操作後處理JFileChooser結果?

public class OpenFileAction extends AbstractAction { 
JFrame frame; 
JFileChooser chooser; 

OpenFileAction(JFrame frame, JFileChooser chooser) { 
    super("Open..."); 
    this.chooser = chooser; 
    this.frame = frame; 
} 

public void actionPerformed(ActionEvent evt) { 
    // Show dialog; this method does not return until dialog is closed 
    chooser.showOpenDialog(frame); 
} 
}; 

很明顯,我想將JFileChooser結果寫入一個變量。如何訪問e.getSource()操作完成後?這不工作,因爲它觸發文件選擇對話框打開之前:

JButton btnNewButton_1 = new JButton(new OpenFileAction(new JFrame(), new JFileChooser(new File(".")))); 
    btnNewButton_1.addActionListener(new ActionListener() { 
     public void actionPerformed(ActionEvent e) { 
      //process e.getSource() ? 
     } 
    }); 
+0

我真的不明白你想要做什麼。你爲什麼要用你的動作(第一個動作監聽器)創建一個按鈕,然後在按鈕後面添加第二個動作監聽器?你最終的目標是什麼? –

+0

我需要以某種方式從文件選擇器中獲取值。鑑於我必須知道OpenFileAction的結果,那麼會怎麼做呢? –

回答

3

我想你以後是這樣的:

public abstract class OpenFileAction extends AbstractAction { 
    JFrame frame; 
    JFileChooser chooser; 

    public OpenFileAction(JFrame frame, JFileChooser chooser) { 
     super("Open..."); 
     this.chooser = chooser; 
     this.frame = frame; 
    } 

    public void actionPerformed(ActionEvent evt) { 
     int option = chooser.showOpenDialog(this.frame); 
     if (option == JFileChooser.APPROVE_OPTION) { 
      File selectedFile = chooser.getSelectedFile(); 
      doWithSelectedFile(selectedFile) 
     } 
    } 

    /** 
    * Method to override, which gets called with the selected file. 
    */ 
    protected abstract doWithSelectedFile(File file); 
} 

... 

OpenFileAction action = new OpenFileAction(frame, new JFileChooser(new File("."))) { 
    @Override 
    protected void doWithSelectedFile(File file) { 
     // do what you want here 
    } 
}; 
JButton button = new JButton(action); 
+0

你可以通過構造函數重寫另一個類的方法嗎?哇。這看起來會起作用。謝謝! –

+0

與你在你的問題中創建一個新的匿名ActionListener時所做的完全一樣:你正在創建一個OpenFileAction的匿名子類。 –

1

這裏是你的代碼展示如何來解決這一問題的最新情況。由於您已在JButton(也是ActionListener)上設置Action,因此不需要您的動作偵聽器。

import java.awt.Component; 
import java.awt.event.ActionEvent; 
import java.io.File; 

import javax.swing.AbstractAction; 
import javax.swing.JButton; 
import javax.swing.JFileChooser; 
import javax.swing.JFrame; 
import javax.swing.JOptionPane; 
import javax.swing.SwingUtilities; 

public class TestFileChooser { 

    public static class OpenFileAction extends AbstractAction { 
     JFileChooser chooser; 

     OpenFileAction(JFileChooser chooser) { 
      super("Open..."); 
      this.chooser = chooser; 
     } 

     @Override 
     public void actionPerformed(ActionEvent evt) { 
      int retval = chooser.showOpenDialog((Component) evt.getSource()); 
      if (retval == JFileChooser.APPROVE_OPTION) { 
       File selectedFile = chooser.getSelectedFile(); 
       JOptionPane.showMessageDialog((Component) evt.getSource(), "You have chosen " + selectedFile.getAbsolutePath()); 
      } 
     } 
    }; 

    protected void initUI() { 
     JFrame frame = new JFrame("Test file chooser"); 
     frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
     JFileChooser chooser = new JFileChooser(new File(".")); 
     JButton btnNewButton_1 = new JButton(new OpenFileAction(chooser)); 
     frame.add(btnNewButton_1); 
     frame.setSize(600, 400); 
     frame.setVisible(true); 
    } 

    public static void main(String[] args) { 
     SwingUtilities.invokeLater(new Runnable() { 

      @Override 
      public void run() { 
       new TestFileChooser().initUI(); 
      } 
     }); 
    } 
} 

這裏是關於JFileChooser教程其可以提供額外的信息。

+0

謝謝,但我真的希望將文件選擇器放在單獨的類中,這是抽象的全部要點。這是我現在的代碼。我想訪問ActionEvent,因爲你永遠無法確定是否調用了JFileChooser.APPROVE_OPTION,並且所有的都來自類之外... –

+0

@ user70448什麼阻止你將retval設置爲OPenFileAction類的成員,併爲該值添加一個getter ? –

+0

沒有任何東西阻止我,但是如何在動作完成後從外部調用getter? –

0

編輯:

下面的代碼是容易爲你:

JButton openButton = new JButton(); 
openButton.addActionListener(new ActionListener(){ 
    public void actionPerformed(evt){ 
     final JFileChooser fileChooser = new JFileChooser(); 
     File openFile; 
     if(fileChooser.showOpenDialog(frame) == JFileChooser.APPROVE_OPTION) 
      openFile= fileChooser.getSelectedFile(); 
     //place your code here 
    } 
}); 

OR(如果你絕對需要一個本地方法)

JButton openButton = new JButton(); 
openButton.addActionListener(new ActionListener(){ 
    public void actionPerformed(evt){ 
     final JFileChooser fileChooser = new JFileChooser(); 
     if(fileChooser.showOpenDialog(frame) == JFileChooser.APPROVE_OPTION) 
      openButtonActionPerformed(fileChooser.getSelectedFile()); 
    } 
}); 
openButtonActionPerformed(File openFile){ 
    //place your code here 
} 

有沒有必要建立另一個類

+0

謝謝,但是這是使用JButton來調用FileChooser。 –

+0

只需將此代碼放入jButtonActionPerformed方法中即可完成 – vedant1811