我想对JFileChooser按钮使用Abstract动作,因为会有很多这些按钮。
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()?这不起作用,因为它在FileChooser对话框打开之前被触发:
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 :(得分: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);
答案 1 :(得分: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上可能提供其他信息的教程。
答案 2 :(得分: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
}
无需创建另一个类