Paid for writing

In case you like my writing and would like me to write for your website, then please leave a comment to any of my blog article, mentioning your Email Id and I will reply back. Thanks

Tuesday, April 7, 2009

Open any file using Java

This is a simple code which will explain how you can open any application on your system, using Java.
First lets take a look at the source code.



//DesktopOpenImpl.java

import java.awt.BorderLayout;
import java.awt.Desktop;
import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.File;
import java.io.IOException;

import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JTextField;

/*
* Java Code to Launch any application.
*/
public class DesktopOpenImpl implements ActionListener
{
JFrame desktopFrame;
JPanel mainPanel;
JLabel pathLabel, listLabel;
JTextField pathText;
JButton goButton, openButton;
JComboBox listFilesCombo;
Desktop desktop;

public DesktopOpenImpl()
{

pathLabel=new JLabel("Path : ");
pathText=new JTextField(30);
goButton=new JButton("Go");
goButton.addActionListener(this);
listLabel=new JLabel("List : ");
listFilesCombo=new JComboBox();
openButton=new JButton("Open");
openButton.addActionListener(this);

mainPanel=new JPanel();
mainPanel.setLayout(new FlowLayout(FlowLayout.CENTER));

mainPanel.add(pathLabel);
mainPanel.add(pathText);
mainPanel.add(goButton);
mainPanel.add(listLabel);
mainPanel.add(listFilesCombo);
mainPanel.add(openButton);

desktopFrame=new JFrame("Application Opener");
desktopFrame.setLayout(new BorderLayout());

desktopFrame.add(mainPanel,BorderLayout.NORTH);
desktopFrame.setVisible(true);
desktopFrame.pack();

}

public static void main(String args[])
{
new DesktopOpenImpl();
}

public void actionPerformed(ActionEvent ae)
{
if(ae.getSource()==goButton)
{
if(Desktop.isDesktopSupported()==false)
{
JOptionPane.showConfirmDialog(null,"Desktop not supported","Error",JOptionPane.DEFAULT_OPTION,JOptionPane.ERROR_MESSAGE);
desktopFrame.dispose();
}

desktop=Desktop.getDesktop();

File root=new File(pathText.getText());
File listFiles[]=root.listFiles();

for(File file : listFiles)
{
listFilesCombo.addItem(file);

}

desktopFrame.pack();

}

else if(ae.getSource()==openButton)
{
if(!desktop.isSupported(Desktop.Action.OPEN))
{
JOptionPane.showConfirmDialog(null,"Application Type Not Supported","Error",JOptionPane.DEFAULT_OPTION,JOptionPane.ERROR_MESSAGE);
desktopFrame.dispose();
}

try
{
desktop.open(new File(listFilesCombo.getSelectedItem().toString()));
}
catch(IOException e)
{
e.printStackTrace();
}

}
}
}



Explanation

The code uses the Desktop classes defined in java.awt package to open the file using the native OS' application opener.
This means that in order to open a .pdf file you need to have a PDF reader installed in your system or in order to open a .doc file
you need to have MS Word or any such software installed in your system.
Desktop class doesnt try to reinvent the wheel, it simply checks what the default application is for the file, you selected and opens it accordingly.

The GUI of the code has a TextField wherein you can enter the directory where you want to search for a file.
On click of the Go Button, the code first checks whether the Desktop is supported on your machine. In case its not then the code displays an error message and closes down.
In case Desktop is supported on your system then the contents of the textfield are taken and passed as input in the constructor,for the object of File class.
The root is the object of the File class and root.listFiles() lists all the files and directories that are present in the directory mentioned by root.
The combo box is populated with the list of files/directories which we got from the root.listFiles().

Now you can select any of the file from here and click on Open button. In case you select another directory then its opened in the explorer (Windows).
In case you select a file then it checks for the default application for that and opens the file in that.
The desktop.open() does that task.

3 comments:

Anonymous said...

Thanks for the post..very useful!!

Anonymous said...

Thanks ... found your post very useful

CongNT said...

Thank Anand. I'm use it for my problem :)