Invece della progressBarInputStream ho provato ad usare JProgressBar.
Mi compare la dialog con la progress bar che aumenta, ma non mi scrive nulla nell'editor.
Posto il codice del metodo openFile e
Le classi LonkTask e SwingWorker sono quelle prese dal sito della sun
Forse sbaglio il punto in cui chiamo il metodo doRead, forse la sua chiamata va messa dentro la classe LongTask?
codice:
OpenAction() {
super(openAction);
}
public void actionPerformed(ActionEvent e) {
Frame frame = getFrame();
if (fileDialog == null) {
fileDialog = new FileDialog(frame);
}
fileDialog.setMode(FileDialog.LOAD);
fileDialog.show();
String file = fileDialog.getFile();
if (file == null) {
return;
}
String directory = fileDialog.getDirectory();
final File f = new File(directory, file);
if (f.exists()) {
Document oldDoc = getEditor().getDocument();
if (oldDoc != null)
oldDoc.removeUndoableEditListener(undoHandler);
if (elementTreePanel != null) {
elementTreePanel.setEditor(null);
}
getEditor().setDocument(new PlainDocument());
final Document doc = getEditor().getDocument();
frame.setTitle(file);
//Thread loader = new FileLoader(f, editor.getDocument());
//loader.start();
System.out.println("long size: " + f.length());
System.out.println("int size: " + (int) f.length());
task = new LongTask((int) f.length());
//Create the demo's UI.
progressBar = new JProgressBar(0, task.getLengthOfTask());
progressBar.setValue(0);
progressBar.setStringPainted(true);
taskOutput = new JTextArea(5, 20);
taskOutput.setMargin(new Insets(5, 5, 5, 5));
taskOutput.setEditable(false);
taskOutput.setCursor(null); //inherit the panel's cursor
//see bug 4851758
final JDialog dialog = new JDialog(frame, "Loading text file", true);
JPanel panel = new JPanel();
panel.add(progressBar);
dialog.getContentPane().add(panel, BorderLayout.PAGE_START);
dialog.getContentPane().add(new JScrollPane(taskOutput), BorderLayout.CENTER);
//dialog.setBorder(BorderFactory.createEmptyBorder(20, 20, 20,
// 20));
//Create a timer.
timer = new Timer(ONE_SECOND, new ActionListener() {
public void actionPerformed(ActionEvent evt) {
progressBar.setValue(task.getCurrent());
String s = task.getMessage();
if (s != null) {
taskOutput.append(s + newline);
taskOutput.setCaretPosition(taskOutput.getDocument().getLength());
}
if (task.isDone()) {
Toolkit.getDefaultToolkit().beep();
timer.stop();
dialog.getContentPane().setCursor(null); //turn off
// the wait
// cursor
progressBar.setValue(progressBar.getMinimum());
dialog.dispose();
}
}
});
dialog.getContentPane().setCursor(Cursor.getPredefinedCursor(Cursor.WAIT_CURSOR));
task.go();
timer.start();
dialog.setSize(400, 350);
dialog.setVisible(true);
Thread t = new Thread() {
public void run() {
doRead(f, doc);
}
};
}
}
private void doRead(File f, Document doc) {
FileReader fr;
try {
fr = new FileReader(f);
BufferedReader br = new BufferedReader(fr);
String input = br.readLine();
while (input != null) {
input = br.readLine();
doc.insertString(doc.getLength(), input, null);
}
br.close();
} catch (Exception e1) {
e1.printStackTrace();
}
}
}