Re: Opening a File for User
Do you mean open the file in excel? You can make a call to System.exec, or alternatively use the new Desktop (Java Platform SE 6) class and call Desktop.open (you should make sure the file has the appropriate extension so that it is recognizes as an excel file)
Re: Opening a File for User
Fantastic. Exactly what I needed. Thanks copeg.
Re: Opening a File for User
You could do something like this.
It's just an example but a JOptionPane will popup with YES and NO. If you click YES, the file will open.
Code Java:
String fileName = "file.txt";
// File written. Open?
JOptionPane pane = new JOptionPane("file saved. Open file?");
Object[] options = new String[] { "YES", "NO" };
pane.setOptions(options);
JDialog dialog = pane.createDialog(new JFrame(), "File Saved");
dialog.show();
Object obj = pane.getValue();
int result = -1;
for (int k = 0; k < options.length; k++)
if (options[k].equals(obj)){
result = k;
String opt = Integer.toString(result);
// if YES
if(opt.equals("0")){
// Open file
Process p = Runtime.getRuntime().exec("cmd /c \""+fileName+"\"");
}else{
// do nothing
}
}
This opens the file:
You will need to import:
javax.swing.JOptionPane;
javax.swing.JDialog;