小弟刚学习swing
在编写一个把文件显示在JTextPane的程序遇到一个问题
该程序遇到文件比较大的时候,就不能够写入JTextPane
不知道为什么,还请高手指教具体代码如下:
package main;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.text.*;
import java.io.*;
public class ReadDocContent implements Runnable
{
JFrame mainframe;
JTextPane textpane=null;
JInternalFrame internalframe=null;
JFileChooser filechooser=null;
Thread athread;
SimpleAttributeSet attrset=new SimpleAttributeSet();
public ReadDocContent(JTextPane pane,JFrame mf,JInternalFrame current)
{
this.mainframe=mf;
this.textpane=pane;
this.internalframe=current;
filechooser=new JFileChooser();
athread=new Thread(this);
athread.start();
}
public void run()
{
File file=null;
int result=filechooser.showOpenDialog(mainframe);
textpane.setText("");
if(result==JFileChooser.APPROVE_OPTION)
{
file=filechooser.getSelectedFile();
internalframe.setTitle(file.getName());
}
else
{
internalframe.setTitle("you choose nothing");
}
BufferedReader in=null;
if(file!=null)
{
try
{
in=new BufferedReader(new FileReader(file));
}
catch(FileNotFoundException e)
{
System.out.println("error");
internalframe.setTitle("File Not Found");
return;
}
Document doc=textpane.getDocument();
String str;
try
{
while((str=in.readLine())!=null)
{
System.out.println(doc.getLength());
doc.insertString(doc.getLength(),str+"\n",attrset);
}
}
catch(IOException e)
{
System.out.println("error");
internalframe.setTitle("io error");
}
catch(BadLocationException ble)
{
System.out.println("error");
System.out.println(ble.getMessage());
}
finally
{
try
{
in.close();
}
catch(IOException e)
{
System.out.println("error");
}
}
}
}
}
在编写一个把文件显示在JTextPane的程序遇到一个问题
该程序遇到文件比较大的时候,就不能够写入JTextPane
不知道为什么,还请高手指教具体代码如下:
package main;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.text.*;
import java.io.*;
public class ReadDocContent implements Runnable
{
JFrame mainframe;
JTextPane textpane=null;
JInternalFrame internalframe=null;
JFileChooser filechooser=null;
Thread athread;
SimpleAttributeSet attrset=new SimpleAttributeSet();
public ReadDocContent(JTextPane pane,JFrame mf,JInternalFrame current)
{
this.mainframe=mf;
this.textpane=pane;
this.internalframe=current;
filechooser=new JFileChooser();
athread=new Thread(this);
athread.start();
}
public void run()
{
File file=null;
int result=filechooser.showOpenDialog(mainframe);
textpane.setText("");
if(result==JFileChooser.APPROVE_OPTION)
{
file=filechooser.getSelectedFile();
internalframe.setTitle(file.getName());
}
else
{
internalframe.setTitle("you choose nothing");
}
BufferedReader in=null;
if(file!=null)
{
try
{
in=new BufferedReader(new FileReader(file));
}
catch(FileNotFoundException e)
{
System.out.println("error");
internalframe.setTitle("File Not Found");
return;
}
Document doc=textpane.getDocument();
String str;
try
{
while((str=in.readLine())!=null)
{
System.out.println(doc.getLength());
doc.insertString(doc.getLength(),str+"\n",attrset);
}
}
catch(IOException e)
{
System.out.println("error");
internalframe.setTitle("io error");
}
catch(BadLocationException ble)
{
System.out.println("error");
System.out.println(ble.getMessage());
}
finally
{
try
{
in.close();
}
catch(IOException e)
{
System.out.println("error");
}
}
}
}
}
可以使用SwingWorker来实现: SwingWorker<Void, String> worker = new SwingWorker<Void, String>() {
@Override
protected Void doInBackground() throws Exception {
for (int i = 0; i < 100; i++) {
publish("DearMJ:" + i + System.getProperty("line.separator"));
Thread.sleep(1000);
}
return null;
} @Override
protected void process(List<String> chunks) {
Document doc = consolePane.getDocument();
for (String line : chunks) {
try {
doc.insertString(doc.getLength(), line, null);
} catch (BadLocationException ex) {
Logger.getLogger(TestFrame.class.getName()).log(Level.SEVERE, null, ex);
}
}
consolePane.setCaretPosition(doc.getLength());
}
};
worker.execute();还有个简单些的方法,使用应用程序框架Task类,它也是基于SwingWorker的。google一下吧。