使用 Java Swing 选择文件路径并使用该选择执行某些操作
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/12697138/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Select a file path with Java Swing and do something with that selection
提问by Justin
I have been reading up on JFileChooser in javax.swing.* I know the showOpenDialog() method will allow me to select a File and click 'choose' but I have a specific way I want it to work.
我一直在阅读 javax.swing 中的 JFileChooser。* 我知道 showOpenDialog() 方法将允许我选择一个文件并单击“选择”,但我有一种我希望它工作的特定方式。
I want to use two JFileChooser's (probably side by side in a JPanel) to select a TO and FROM path and then click a button that will take the user input from both 'Chooser's and do something.
我想使用两个 JFileChooser(可能并排在 JPanel 中)来选择 TO 和 FROM 路径,然后单击一个按钮,该按钮将从两个“选择器”中获取用户输入并执行某些操作。
Perhaps if anyone has an example of just doing a single JFileChooser like this? Essentially just highlighting the file/directory in the chooser and clicking some OTHER button to take the input from the 'Chooser (also the JFileChoosers button's (cancel and choose) are hidden).
也许如果有人有一个像这样只做一个 JFileChooser 的例子?基本上只是在选择器中突出显示文件/目录,然后单击其他按钮以从“选择器”获取输入(JFileChoosers 按钮(取消和选择)也被隐藏)。
Most likely this 'other' button would just be a signal to the code to get the value from the JFileChooser object.
最有可能的是,这个“其他”按钮只是向代码发出信号,以从 JFileChooser 对象中获取值。
I'm hoping being new to Swing that there is another class I am missing that can do what I have described but it's just not coming up in Google searches I have been crafting.
我希望是 Swing 的新手,我缺少另一个可以完成我所描述的类的类,但它只是没有出现在我一直在制作的 Google 搜索中。
回答by MadProgrammer
This is my first pass (I'm on my Mac at the mo, so I'm having some issues with walking the JDK source ;))
这是我的第一次通过(我现在在 Mac 上,所以我在处理 JDK 源代码时遇到了一些问题;))
The problem is, getting rid of cancel
and okay
buttons...
问题是,摆脱cancel
和okay
按钮......
public class TestFileChooser2 {
public static void main(String[] args) {
new TestFileChooser2();
}
public TestFileChooser2() {
EventQueue.invokeLater(new Runnable() {
@Override
public void run() {
JFrame frame = new JFrame();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setLayout(new BorderLayout());
frame.add(new MainPane());
frame.setSize(800, 400);
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
});
}
protected class MainPane extends JPanel {
private JFileChooser fileChooser;
private JPanel filePane;
private JTextField fileField;
public MainPane() {
setLayout(new BorderLayout());
fileChooser = new JFileChooser();
fileChooser.addPropertyChangeListener(new PropertyChangeListener() {
@Override
public void propertyChange(PropertyChangeEvent evt) {
if (evt.getPropertyName().equals("SelectedFileChangedProperty")) {
File file = fileChooser.getSelectedFile();
if (file != null) {
setFile(file);
}
}
}
});
add(fileChooser, BorderLayout.WEST);
filePane = new JPanel(new GridBagLayout());
GridBagConstraints gbc = new GridBagConstraints();
gbc.gridx = 0;
gbc.gridy = 0;
gbc.weightx = 1;
gbc.fill = GridBagConstraints.HORIZONTAL;
fileField = new JTextField(10);
filePane.add(fileField, gbc);
add(filePane);
}
protected void setFile(File file) {
fileField.setText(file.getPath());
}
}
}
UPDATED
更新
Apparently Windows doesn't like playing nice with the property change listener...
显然 Windows 不喜欢与属性更改侦听器玩得很好......
Make no mistake, this is a complete hack...
毫无疑问,这是一个完整的黑客......
public class TestFileChooser2 {
public static void main(String[] args) {
new TestFileChooser2();
}
public TestFileChooser2() {
EventQueue.invokeLater(new Runnable() {
@Override
public void run() {
try {
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
} catch (ClassNotFoundException ex) {
} catch (InstantiationException ex) {
} catch (IllegalAccessException ex) {
} catch (UnsupportedLookAndFeelException ex) {
}
JFrame frame = new JFrame();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setLayout(new BorderLayout());
frame.add(new MainPane());
frame.setSize(800, 400);
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
});
}
protected class MainPane extends JPanel {
private JFileChooser fileChooser;
private JPanel filePane;
private JTextField fileField;
public MainPane() {
setLayout(new BorderLayout());
fileChooser = new JFileChooser();
fileChooser.setApproveButtonText("delete");
removeButtons(fileChooser);
JList list = findFirstChildren(fileChooser, JList.class);
list.addListSelectionListener(new ListSelectionListener() {
@Override
public void valueChanged(ListSelectionEvent e) {
if (!e.getValueIsAdjusting()) {
File file = (File)((JList)e.getSource()).getSelectedValue();
if (file != null) {
setFile(file);
}
}
}
});
// fileChooser.addPropertyChangeListener(new PropertyChangeListener() {
// @Override
// public void propertyChange(PropertyChangeEvent evt) {
// System.out.println(evt.getPropertyName());
// if (evt.getPropertyName().equals("SelectedFileChangedProperty")) {
// File file = fileChooser.getSelectedFile();
// if (file != null) {
// setFile(file);
// }
// }
// }
// });
add(fileChooser, BorderLayout.WEST);
filePane = new JPanel(new GridBagLayout());
GridBagConstraints gbc = new GridBagConstraints();
gbc.gridx = 0;
gbc.gridy = 0;
gbc.weightx = 1;
gbc.fill = GridBagConstraints.HORIZONTAL;
fileField = new JTextField(10);
filePane.add(fileField, gbc);
add(filePane);
}
protected void setFile(File file) {
fileField.setText(file.getPath());
}
protected void removeButtons(Container container) {
for (Component child : container.getComponents()) {
if (child instanceof JButton) {
JButton btn = (JButton) child;
if (btn.getText() != null && (btn.getText().equals(fileChooser.getApproveButtonText()) || btn.getText().equals("Cancel"))) {
container.remove(child);
}
} else if (child instanceof Container) {
removeButtons((Container) child);
}
}
}
public <T extends Component> T findFirstChildren(JComponent component, Class<T> clazz) {
T child = null;
for (Component comp : component.getComponents()) {
if (clazz.isInstance(comp)) {
child = (T) comp;
break;
} else if (comp instanceof JComponent) {
child = findFirstChildren((JComponent) comp, clazz);
if (child != null) {
break;
}
}
}
return child;
}
}
}
A better solution would be to utilise the FileSystemView
directly and build you're own view, but that's more effort then I have time for right now :(
更好的解决方案是FileSystemView
直接利用并构建您自己的视图,但这比我现在有时间要花更多的精力:(
回答by trashgod
回答by Philip Whitehouse
Edit: According to @MadProgrammer this is not correct.
编辑:根据@MadProgrammer,这是不正确的。
JFileChooser
is a dialogwhich is designed to perform the Open and Save stuff. You can change the name of the button and probably remove the Cancel. But you can't make it open as a Panel.
JFileChooser
是一个对话框,旨在执行打开和保存的东西。您可以更改按钮的名称并可能删除取消。但是你不能让它作为面板打开。
However, it's a dialog and it'll require a fair bit of reworking to get it embedded in a page instead. The source is available though, so it's possible. You can reuse the javax.swing.filechooser
classes.
但是,它是一个对话框,需要进行大量修改才能将其嵌入到页面中。源是可用的,所以它是可能的。您可以重用这些javax.swing.filechooser
类。