Vote count:
0
this is the codes when i press open another Window appears with JFileChooser to select the file again and when i try to select the file again it just gives me "open command canceled by user" message and the program stop working and ends with an unknown exception
import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import javax.swing.JFileChooser;
import javax.swing.JFrame;
import javax.swing.JOptionPane;
import javax.swing.JTextArea;
import javax.swing.filechooser.FileNameExtensionFilter;
public class FileC extends JFrame {
File file;
JFileChooser chooser = new JFileChooser();
JTextArea TArea = new JTextArea();
public FileC()
{
super.setLayout(new FlowLayout());
super.add(chooser);
super.add(TArea);
FileNameExtensionFilter filter = new FileNameExtensionFilter ( "text files " , "txt");
chooser.setFileFilter(filter);
ActionHandler handler = new ActionHandler ();
chooser.addActionListener(handler);
}
private class ActionHandler implements ActionListener
{
public void actionPerformed(ActionEvent e) {
int returnVal = chooser.showOpenDialog(FileC.this);
if (returnVal == JFileChooser.APPROVE_OPTION) {
file = chooser.getSelectedFile();
} else {
JOptionPane.showMessageDialog(null,"Open command cancelled by user.");
}
try {
BufferedReader in = new BufferedReader ( new FileReader (file));
String s = in.readLine();
while (s != null)
{
TArea.append(s);
s = in.readLine();
}
}catch (FileNotFoundException z){ JOptionPane.showMessageDialog(null, "file not found"); }
catch (IOException z) { z.printStackTrace(); }
}
}
}
asked 1 min ago
Aucun commentaire:
Enregistrer un commentaire