Good Morning, I want to show the file location when the button "Locate" is pressed. Also I have full file path in JTextField, when I tried, I get an 'NullPointerException' exception, please give me directions, thanks
So far have tried:
public class locateExp {
private JFrame frame;
private JTextField txtCusersmyFirstPdf;
private Desktop desktop;
/**
* Launch the application.
*/
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
locateExp window = new locateExp();
window.frame.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
/**
* Create the application.
*/
public locateExp() {
initialize();
}
/**
* Initialize the contents of the frame.
*/
private void initialize() {
frame = new JFrame();
frame.setBounds(100, 100, 489, 329);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.getContentPane().setLayout(null);
JButton button = new JButton("Locate");
button.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0) {
//full file path
File pdfFilepath = new File(txtCusersmyFirstPdf.getText());
if (pdfFilepath.exists()){
try {
//desktop.open(pdfFilepath.getParentFile());
desktop.open(pdfFilepath);
} catch(IOException ex) {
}
}
}
});
button.setMnemonic('t');
button.setFont(new Font("Calibri", Font.BOLD, 12));
button.setBorder(null);
button.setBackground(SystemColor.menu);
button.setBounds(126, 112, 44, 19);
frame.getContentPane().add(button);
JButton button_1 = new JButton("Open");
button_1.setMnemonic('o');
button_1.setFont(new Font("Calibri", Font.BOLD, 12));
button_1.setBorder(null);
button_1.setBackground(SystemColor.menu);
button_1.setBounds(180, 112, 44, 19);
frame.getContentPane().add(button_1);
JButton button_2 = new JButton("Print");
button_2.setMnemonic('p');
button_2.setFont(new Font("Calibri", Font.BOLD, 12));
button_2.setBorder(null);
button_2.setBackground(SystemColor.menu);
button_2.setBounds(228, 112, 44, 19);
frame.getContentPane().add(button_2);
JLabel label = new JLabel("File:");
label.setFont(new Font("Calibri", Font.BOLD, 12));
label.setBounds(114, 142, 44, 14);
frame.getContentPane().add(label);
JLabel lblMyFirstPdf = new JLabel("My First PDF Doc.pdf");
lblMyFirstPdf.setBounds(162, 141, 269, 14);
frame.getContentPane().add(lblMyFirstPdf);
JLabel label_2 = new JLabel("Path/name:");
label_2.setFont(new Font("Calibri", Font.BOLD, 12));
label_2.setBounds(89, 173, 63, 14);
frame.getContentPane().add(label_2);
txtCusersmyFirstPdf = new JTextField();
txtCusersmyFirstPdf.setText("C:\\Users\\My First PDF Doc.pdf");
txtCusersmyFirstPdf.setColumns(10);
txtCusersmyFirstPdf.setBounds(162, 168, 269, 23);
frame.getContentPane().add(txtCusersmyFirstPdf);
JLabel label_3 = new JLabel("File Size:");
label_3.setFont(new Font("Calibri", Font.BOLD, 12));
label_3.setBounds(106, 205, 325, 14);
frame.getContentPane().add(label_3);
JLabel label_4 = new JLabel("513 k bytes");
label_4.setBounds(162, 204, 269, 14);
frame.getContentPane().add(label_4);
}
}