I am trying to add a JFileChooser
which selects a parent directory and allows users to input a name for a file. I am aware of the showSaveDialog
and showOpenDialog
methods, however I don't want to create a new window.
Here's what I have so far:
public class BrowserTest extends JFrame {
private JPanel contentPane;
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
BrowserTest frame = new BrowserTest();
frame.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
public BrowserTest() {
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setBounds(100, 100, 450, 300);
contentPane = new JPanel();
contentPane.setLayout(new BorderLayout(0, 0));
setContentPane(contentPane);
JFileChooser browser = new JFileChooser();
browser.setFileFilter(new FileFilter() {
@Override
public String getDescription() {
return "A .extension file";
}
@Override
public boolean accept(File f) {
return f.isDirectory();
}
});
browser.setDialogType(JFileChooser.SAVE_DIALOG);
browser.setSelectedFile(new File("*.extension"));
browser.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
final String action = e.getActionCommand();
if(action.equals(JFileChooser.APPROVE_SELECTION)) {
System.out.println("trigger");
File f = browser.getCurrentDirectory();
System.out.println(f);
}
if(action.equals(JFileChooser.CANCEL_SELECTION))
JOptionPane.showConfirmDialog(null, "Start without saving?", "No save directory selected.", JOptionPane.YES_NO_OPTION, JOptionPane.WARNING_MESSAGE);
}
});
contentPane.add(browser);
revalidate();
}
}
For some reason, pressing the save button only executes System.out.println("trigger");
if I select a file. Is there a way to listen for a save button press directly?
您可以使用以下代码访问文件选择器的默认按钮,然后将您自己的侦听器添加到该按钮:
JButton defaultButton = browser.getUI().getDefaultButton(browser);
As noted, one kludge solution is to recursively iterate through the components of your JFileBrowser until you find the correct component, here a JButton with an action command String "Open".
For example, this method could work:
public static void recursiveComponentSearch(Component c, String actionCommand,
ActionListener listener) {
if (c instanceof JButton) {
JButton button = (JButton) c;
// TODO: delete the line below
System.out.printf("Text: \"%s\"; action command: \"%s\"%n", button.getText(),
button.getActionCommand());
if (button.getActionCommand().equalsIgnoreCase(actionCommand)) {
button.addActionListener(listener);
}
}
// recursive search here
if (c instanceof Container) {
Container container = (Container) c;
Component[] components = container.getComponents();
for (Component component : components) {
recursiveComponentSearch(component, actionCommand, listener);
}
}
}
used like:
ActionListener listener = evt -> System.out.println("Save button here");
String actionCommand = "Open";
recursiveComponentSearch(browser, actionCommand, listener);
I just discovered that the problem was in the line browser.setSelectedFile(new File("*.extension"));
. Apparently, this method does not like the asterisk and replacing it with anything else will fix the problem. For example: browser.setSelectedFile(new File("new.extension"));
will work as intended.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.