How to open a stored text file using java?
Respected Sir / Madam
I have a JList containing absolute path of files stored in my operating system in String format....I have added a ListSelectionListener...Now when i select an absolute path in the JList, the file should open ....I don't know how to do that....Hope you got my problem....Can any one pls help me out?
Thank you[
# 4
Yes....i want to open a file(word document) like the one we open by double clicking on it....Actually i have a JList which contains the path of files stored on my pc..So when i select any row, event occurs...Inside the public void valueChanged(ListSelectionEvent e) method i want have some instructions which will help me open the file......
Thank you...
# 6
thats ok buddy! best of luck
U need to import
import javax.swing.*;
import javax.swing.event.*;
import java.awt.*;
import java.awt.event.*;
public void valueChanged(ListSelectionEvent e)
{
if (!e.getValueIsAdjusting()) {
String fileName=
optionList.getSelectedValue().toString();
final JFrame dialog = new JFrame(fileName);
JEditorPane viewer = new JEditorPane();
viewer.setEditable(false);
JScrollPane jsp = new JScrollPane(viewer);
try {
viewer.setPage("file:/" + fileName);
} catch (Exception e) {
viewer.setText("Error opening " + fileName + ": " + e);
}
dialog.getContentPane().add(jsp, BorderLayout.CENTER);
JButton closeButton = new JButton("Close");
closeButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
dialog.setVisible(false);
dialog.dispose();
}
});
dialog.getContentPane().add(closeButton, BorderLayout.SOUTH);
dialog.setDefaultCloseOperation(DISPOSE_ON_CLOSE);
//dialog.pack();
dialog.setSize(600, 600);
dialog.show();
}
}