【发布时间】:2016-07-22 14:17:02
【问题描述】:
我有一个JFrame 和JFileChooser。需要有一个自定义的导入按钮,而不是默认的文件选择器操作按钮。
如果我使用自定义操作按钮,如果我在文件名文本框中输入值,JFileChooser.getSelectedFile() 返回 null。而如果我点击文件并点击自定义导入,我可以得到我选择的文件。
这里我包含了重现这个的示例代码
import java.awt.BorderLayout;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFileChooser;
import javax.swing.JFrame;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.SwingUtilities;
public class FileChooserDemo extends JPanel
implements ActionListener {
private static final long serialVersionUID = 1L;
JFileChooser importFileChooser;
JFrame frame;
private void createAndShowGUI() {
//Create and set up the window.
frame = new JFrame("FileChooserDemo");
JPanel inputJobDetailsPanel = new JPanel(new BorderLayout(0,5));
importFileChooser = new JFileChooser();
importFileChooser.setControlButtonsAreShown(false);
importFileChooser.setFileSelectionMode(JFileChooser.FILES_ONLY);
importFileChooser.setMultiSelectionEnabled(false);
inputJobDetailsPanel.add(importFileChooser, BorderLayout.CENTER);
GridBagLayout importButtonPanelLayout = new GridBagLayout();
importButtonPanelLayout.columnWidths = new int[] {150};
importButtonPanelLayout.rowHeights = new int[] {30};
JPanel importButtonPanel = new JPanel();
importButtonPanel.setLayout(importButtonPanelLayout);
JButton importButton = new JButton("Custom Import");
importButton.setActionCommand("import");
importButton.addActionListener(this);
importButtonPanel.add(importButton, new GridBagConstraints());
JButton OtherButton = new JButton("Other Action");
OtherButton.setActionCommand("otherImport");
OtherButton.addActionListener(this);
importButtonPanel.add(OtherButton, new GridBagConstraints());
inputJobDetailsPanel.add(importButtonPanel, BorderLayout.PAGE_END);
frame.add(inputJobDetailsPanel);
frame.pack();
frame.setVisible(true);
}
public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
public void run() {
new FileChooserDemo().createAndShowGUI();
}
});
}
@Override
public void actionPerformed(ActionEvent e) {
String command = e.getActionCommand();
if(command.equals("import")) {
if(importFileChooser.getSelectedFile() == null) {
JOptionPane.showMessageDialog(frame, "You entered file name but getSelectedFile() return null");
}else {
JOptionPane.showMessageDialog(frame, "Chosen File Name: " + importFileChooser.getSelectedFile().getName());
}
}else {
JOptionPane.showMessageDialog(frame, "You clicked other action");
}
}
}
O/P:
重现步骤:
- 运行应用程序
- 在“文件名”文本框中输入有效的文件名
- 点击自定义导入
- 现在您可以看到“您输入了文件名但 getSelectedFile() 返回 null”
注意:如果我使用 importFileChooser.setControlButtonsAreShown(true); 启用默认操作按钮
即使我在文本框中输入而不单击文件,我也可以获得 getSelectedFile()。
实际上我正在尝试编写一个自动化脚本,所以我只能通过“文件名”文本框输入文件路径。
有没有办法通过getSelectedFile() 获取文件而不点击文件??
【问题讨论】:
-
无法在 Mac OS X 上重现;选择器 UI 没有“文本框”。
-
@trashgod :这里我包括了屏幕截图。我可以在 Windows 和 Linux 中看到这样的结果。
标签: java swing action actionlistener jfilechooser