Java.beginner
Java.beginner

Reputation: 891

Open a PDF file using the path from the JTextField - Swing

Have small GUI, on-click Open button trying to open the pdf document stored locally and the file path is in the JTextField.

enter image description here

I couldn't get it, Please give me some directions, thanks.

So Far the code;

        JButton btnEdi = new JButton("Open");
    btnEdi.setMnemonic('o');
    btnEdi.setFont(new java.awt.Font("Calibri", Font.BOLD, 12));
    btnEdi.setBorder(null);
    btnEdi.setBackground(SystemColor.menu);
    btnEdi.setBounds(378, 621, 35, 19);
    frmViperManufacturingRecord.getContentPane().add(btnEdi);

    btnEdi.addActionListener(new ActionListener(){
        //desktop = Desktop.getDesktop();
        //String storedFileName = txtText.getText();
        public void actionPerformed(ActionEvent ae) {
            desktop.open(txtText.getText());

        }
    });

Upvotes: 1

Views: 965

Answers (2)

Bartosz Grudzień
Bartosz Grudzień

Reputation: 1

Use the isDesktopSupported() method to determine whether the Desktop API is available on your operation system.

File pdfFile = new File("resources\\ManoeuvreRules-2010.pdf");   
if(isDesktopSupported()){
    try {
        Desktop.getDesktop().open(pdfFile);
    } catch (IOException ex) {
        Logger.getLogger(getClass().getName()).log(Level.SEVERE, null, ex);
        JOptionPane.showMessageDialog( null
                                     , "An error happened trying to open file : " + pdfFile.getPath()
                                     , "IOException"
                                     , JOptionPane.WARNING_MESSAGE
        );
    }
}
else{
    JOptionPane.showMessageDialog( null
                                     , "This is not supported on your Operating System: " 
                                     , "IOException"
                                     , JOptionPane.WARNING_MESSAGE
        );
}

Upvotes: 0

dic19
dic19

Reputation: 17971

At this line:

btnEdi.addActionListener(new ActionListener() {
    @Override // don0t forget @Override annotation
    public void actionPerformed(ActionEvent ae) {
        desktop.open(txtText.getText()); // here
    }
});

According to Desktop#open(File file) method documentation it takes a File object as argument, not a String, so I doubt your code even compiles. It also throws an IOException that has to be treated.

That being said, this is what I'd do:

btnEdi.addActionListener(new ActionListener() {
    @Override
    public void actionPerformed(ActionEvent ae) {
        File pdfFile = new File(txtText.getText());                
        try {
            Desktop.getDesktop().open(pdfFile));
        } catch (IOException ex) {
            Logger.getLogger(getClass().getName()).log(Level.SEVERE, null, ex);
            JOptionPane.showMessageDialog( null
                                         , "An error happened trying to open file : " + pdfFile.getPath()
                                         , "IOException"
                                         , JOptionPane.WARNING_MESSAGE
            );
        }
    }
});

See also How to Integrate with the Desktop Class tutorial.

Upvotes: 3

Related Questions