How to Show the "Open With" File Dialog

How to show the modern open file dialog?

Thanks vesan, I changed Microsoft.Win32.OpenFileDialog to System.Windows.Forms.OpenFileDialog, and now the code shows the "Modern" Open File Dialog!

Now I just have to adjust the code to work completely.

How to use OpenFileDialog on Open File

Use the ValidateNames property. Set it to false.

How to open FileDialog on top of all windows

I don't understand why using a JFileChooser should hang your application when used on a MAC...it shouldn't but then again I've read that Swing can do strange things on a MACs due to the EDT. I can't personally confirm that however since I've never worked on a MAC.

One solution might be to run the dialog in a separate thread thus allowing the JFileChooser to function independently from the EDT and therefore not posing any threat to it.

As for your File Chooser dialog hiding behind your Swing application, I think it may be because of the fact that your application's JFrame is set to be Always-On-Top and even though the FileChooser dialog is considered modal (which it is) doesn't mean that it will be displayed above everything if null is used as its Parent component. The parent itself for the dialog should also be set to be Always-On-Top as well. This is usually the case regardless of what Operating System a dialog is displayed in. The following code should work regardless of what the JFileChooser or JOptionPane (, etc) dialog might be for a parent or if there is in fact no parent at all:

final JFrame iFRAME = new JFrame();
iFRAME.setAlwaysOnTop(true); // ****
iFRAME.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
iFRAME.setLocationRelativeTo(null);
iFRAME.requestFocus();

JFileChooser jfc = new JFileChooser(FileSystemView.getFileSystemView().getHomeDirectory());
int returnValue = jfc.showOpenDialog(iFRAME);
iFRAME.dispose();
if (returnValue == JFileChooser.APPROVE_OPTION) {
File selectedFile = jfc.getSelectedFile();
// Display selected file in console
System.out.println(selectedFile.getAbsolutePath());
}
else {
System.out.println("No File Selected!");
}

And for the fact that your application crashes when run in a MAC you might want to try this:

final JFrame iFRAME = new JFrame();
iFRAME.setAlwaysOnTop(true); // ****
iFRAME.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
iFRAME.setLocationRelativeTo(null);
iFRAME.requestFocus();

EventQueue.invokeLater(new Runnable() {
@Override
public void run() {
JFileChooser jfc = new JFileChooser(FileSystemView.getFileSystemView().getHomeDirectory());
int returnValue = jfc.showOpenDialog(iFRAME); // ****
iFRAME.dispose();
if (returnValue == JFileChooser.APPROVE_OPTION) {
File selectedFile = jfc.getSelectedFile();
// Display selected file in console
System.out.println(selectedFile.getAbsolutePath());
}
else {
System.out.println("No File Selected!");
}
}
});

How To Open File Dialog And Create File On It?

To save a file with JFileChooser, you need to use the showSaveDialog() method instead of the showOpenDialog() like in your snippet. For more information check out How to use File Choosers and check out the JFileChooser JavaDoc.

Then the next step if the saving has been approved, is to actually write the file.
For this, you can use a FileWriter.

I put together a small snippet, which opens a JFileChooser on a button click, where you can provide the filename, where some String will be written to this file.

Example:

public class Test {

public static void main(String[] args) {
SwingUtilities.invokeLater(() -> buildGui());
}

private static void buildGui() {
JFrame frame = new JFrame();
JPanel panel = new JPanel();
JButton btn = new JButton("Save your File");

// action listener for the button
btn.addActionListener(e -> {
JFileChooser fileChooser = new JFileChooser(); // create filechooser
int retVal = fileChooser.showSaveDialog(frame); // open the save dialog
if (retVal == JFileChooser.APPROVE_OPTION) { // check for approval
// create a bufferedwriter with the specified file
try (BufferedWriter writer = new BufferedWriter(new FileWriter(fileChooser.getSelectedFile()))) {
// write the content to the file
writer.write("Your content that shall be written to the file");
} catch (IOException e1) {
e1.printStackTrace();
}
}
});

panel.add(btn);
frame.add(panel);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.pack();
frame.setVisible(true);
}
}

Output:

Sample Image

Sample Image



Related Topics



Leave a reply



Submit