So, I have a java GUI which can search for files on my desktop, open them (or at least their title!) and save them. How would I go about launching my standard (Adobe) pdf reader from it? Here's my code
import java.awt.*;
import java.awt.event.*;
import java.io.*;
import javax.swing.*;
public class GuiDatabaseImproved extends JFrame {
public GuiDatabaseImproved() {
super("File Chooser Test Frame");
setSize(350, 200);
setDefaultCloseOperation(EXIT_ON_CLOSE);
Container c = getContentPane();
c.setLayout(new FlowLayout());
JButton openButton = new JButton("Open File");
JButton saveButton = new JButton("Save File");
JButton dirButton = new JButton("Open Folder");
final JLabel statusbar =
new JLabel("Your selected file will show here");
// Create a file chooser that opens up as an Open dialog
openButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent ae) {
JFileChooser chooser = new JFileChooser();
chooser.setMultiSelectionEnabled(true);
int option = chooser.showOpenDialog(GuiDatabaseImproved.this);
if (option == JFileChooser.APPROVE_OPTION) {
File[] sf = chooser.getSelectedFiles();
String filelist = "nothing";
if (sf.length > 0) filelist = sf[0].getName();
for (int i = 1; i < sf.length; i++) {
filelist += ", " + sf[i].getName();
}
statusbar.setText("You chose " + filelist);
}
else {
statusbar.setText("You cancelled.");
}
}
});
// Create a file chooser that opens up as a Save dialog
saveButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent ae) {
JFileChooser chooser = new JFileChooser();
int option = chooser.showSaveDialog(GuiDatabaseImproved.this);
if (option == JFileChooser.APPROVE_OPTION) {
statusbar.setText("You saved " + ((chooser.getSelectedFile()!=null)?
chooser.getSelectedFile().getName():"nothing"));
}
else {
statusbar.setText("You canceled.");
}
}
});
// Create a file chooser that allows you to pick a directory
// rather than a file
dirButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent ae) {
JFileChooser chooser = new JFileChooser();
chooser.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);
int option = chooser.showOpenDialog(GuiDatabaseImproved.this);
if (option == JFileChooser.APPROVE_OPTION) {
statusbar.setText("You opened " + ((chooser.getSelectedFile()!=null)?
chooser.getSelectedFile().getName():"nothing"));
}
else {
statusbar.setText("You cancelled.");
}
}
});
c.add(openButton);
c.add(saveButton);
c.add(dirButton);
c.add(statusbar);
}
public static void main(String args[]) {
GuiDatabaseImproved sfc = new GuiDatabaseImproved();
sfc.setVisible(true);
}
}
I don't want to use an exteal library like icepdf. Any help would be much appreciated!
