Ive implemented the jFileChooser in my command line program and it works, just as it should with only one annoying issue. It seems that it opens underneath every window with no alert of any kind. In fact I even missed it a couple of times at first leading me to believe that i had implemented it wrong.
I have implemented this as follows:
System.out.println("Please select the file");
JFileChooser fc = new JFileChooser();
int retValue = fc.showOpenDialog(new JPanel());
if(retValue == JFileChooser.APPROVE_OPTION){
g.inputFile = fc.getSelectedFile();
}else {
System.out.println("Next time select a file.");
System.exit(1);
}
Essentially I only want the jFileChooser in order to have the user select a file as an input file. This is the only component that has a need for a GUI implementation, so if i can avoid writing up an GUI, that would be helpful.
Class JFileChooser. JFileChooser provides a simple mechanism for the user to choose a file. For information about using JFileChooser , see How to Use File Choosers, a section in The Java Tutorial.
JFileChooser. setMultiSelectionEnabled(true) − To enable the multiple selection of file.
So after trying a variety of things from different stack overflow topics I ended up with a result that consistently and reliably opens above every window on Windows 7.
public class ChooseFile {
private JFrame frame;
public ChooseFile() {
frame = new JFrame();
frame.setVisible(true);
BringToFront();
}
public File getFile() {
JFileChooser fc = new JFileChooser();
if(JFileChooser.APPROVE_OPTION == fc.showOpenDialog(null)){
frame.setVisible(false);
return fc.getSelectedFile();
}else {
System.out.println("Next time select a file.");
System.exit(1);
}
return null;
}
private void BringToFront() {
frame.setExtendedState(JFrame.ICONIFIED);
frame.setExtendedState(JFrame.NORMAL);
}
}
As it stands in my program it is an inner class and is invoked by calling:
System.out.println("Please select the file");
g.inputFile = g.new ChooseFile().getFile();
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With