I have an event listener that basically creates a file chooser pop up when a button is pushed, further the popup to not close when hitting save file button and save the file, but to remain open in order to hit the download button from another exiting popup from the parent frame.
The code is like:
private JPanel controlPanel;
public void addFileChooserPopopEventListener(ActionEvent e) {
e -> { JfileChooser fileChooser = new FileChooser(new File("."));
fileChooser.approveSelection();
if(JFileChooser.APPROVE_OPTION == fileChooser.showDialog(controlPanel, "Save") {
//Save file
}
}
The problem is that the file chooser pop up closes after the showDialog
method is called.
JFileChooser extends JComponent
. Its showDialog
method creates a JDialog
that contains itself, i.e. this
. Refer to the source code for class JFileChooser
. Hence, rather than calling method showDialog
, create your own JDialog
that contains a JFileChooser
.
Calling the JFileChooser
constructor that you use in the code in your question, adds a Open button and a Cancel button to the GUI. (Refer to below screen capture.) JFileChooser
has a addActionListener
method. When you register an ActionListener via addActionListener
, it is invoked when the Cancel button is activated and also when the Open button is activated and a file has been selected. Hence you can handle file selection without closing the dialog. Below is code that should be considered as a proof of concept (POC). In other words, it simply demonstrates that a JFileChooser
"dialog" can be displayed and a file can be selected without closing the dialog window.
Having said that, from the description in your question, I get the impression that your GUI design is flawed, i.e. I get the impression that it is not intuitive to the user, but since you asked only how to implement an aspect of your application, I will leave it at that.
Note that the below code uses method references to implement the ActionListener
interface.
import java.awt.EventQueue;
import java.awt.event.ActionEvent;
import java.awt.event.KeyEvent;
import java.io.File;
import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JFileChooser;
import javax.swing.JFrame;
import javax.swing.JPanel;
public class FilChoos implements Runnable {
private JFrame frame;
public void run() {
createAnddisplayGui();
}
private void createAnddisplayGui() {
frame = new JFrame();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.add(createButton());
frame.pack();
frame.setLocationByPlatform(true);
frame.setVisible(true);
}
private JPanel createButton() {
JPanel panel = new JPanel();
JButton button = new JButton("Browse...");
button.setMnemonic(KeyEvent.VK_B);
button.setToolTipText("Display \"file chooser\" dialog.");
button.addActionListener(this::selectFile);
panel.add(button);
return panel;
}
private void selectFile(ActionEvent event) {
JFileChooser fileChooser = new JFileChooser(new File("."));
fileChooser.addActionListener(this::handleFileSelection);
JDialog dialog = new JDialog(frame);
dialog.add(fileChooser);
dialog.setDefaultCloseOperation(JDialog.DO_NOTHING_ON_CLOSE);
dialog.pack();
dialog.setLocationRelativeTo(frame);
dialog.setVisible(true);
}
private void handleFileSelection(ActionEvent event) {
switch (event.getActionCommand()) {
case "ApproveSelection":
// File selected and 'Open' button activated.
JFileChooser fc = (JFileChooser) event.getSource();
System.out.println(fc.getSelectedFile());
break;
case "CancelSelection":
// 'Cancel' button activated.
System.out.println("CANCEL");
break;
default:
}
}
public static void main(String[] args) {
EventQueue.invokeLater(new FilChoos());
}
}
Screen capture:
Regarding your comment, the text of the Open button can be set via method setApproveButtonText (in class JFileChooser
), e.g.
fileChooser.setApproveButtonText("My text");
Screen capture: