Thursday, January 3, 2019

How to open documents e.g .pdf, .doc ,.png, excel file from By a jbutton in NetBeans


In this tutorial show you how open any document using java application. Here include source code and video tutorial. 






Document path select



   JFileChooser jfc= new JFileChooser();
        jfc.showOpenDialog(this);
       
   

        try {
            File f= jfc.getSelectedFile();
            filepath=f.getAbsolutePath();
            filepath=filepath.replace('\\', '/');
       
            txtfilepath.setText(filepath);

        } catch (Exception e) {
            JOptionPane.showMessageDialog(rootPane, e);
        }
        



Document open

 filepath=txtfilepath.getText();
       
       
        try {
           
                File pdfFile = new File(filepath);
if (pdfFile.exists()) {

if (Desktop.isDesktopSupported())
                        {
Desktop.getDesktop().open(pdfFile);
}
                        else
                        {
                                JOptionPane.showMessageDialog(rootPane,"Desktop is not supported");
}

} else
               
                {
JOptionPane.showMessageDialog(rootPane,"File is not exists");
}


                         
        } catch (Exception e) {
            JOptionPane.showMessageDialog(rootPane, e);
        }

No comments:

Post a Comment