在下面的代码中,我想写入JEditorPane的数字0到10000.但是,JEditorPane不会显示任何东西,除非它完全完成(一次打印0到10000)或者os给它时间来刷新和显示内容。无论是哪种情况,应用程序在一段时间内都没有响应,而用户认为应用程序崩溃了。无论如何,我们可以强制jEditorPane显示更新的内容,即使它仍然很忙?我尝试了invokeAndWait,但在这里没有帮助,因为我们不能从事件分派器线程调用invokeAndWait。快速写入JEditorPane
换句话说,如果我将Thread.sleep(0)
替换为Thread.sleep(50)
,它可以正常工作,并在结果发生时显示结果打印,但添加50毫秒的延迟会使其非常缓慢。我所需要的就是不时更新JEditorPane,以便用户不会认为应用程序崩溃。 我更喜欢只修改updateMessages()。 这里是我的代码:
import javax.swing.JEditorPane;
import javax.swing.JFrame;
import javax.swing.JScrollPane;
import javax.swing.SwingUtilities;
import javax.swing.text.Document;
import javax.swing.text.html.HTMLEditorKit;
import javax.swing.text.html.StyleSheet;
public class CMessageWindowStack {
private static final String MESSAGE = "msg";
private JScrollPane scrollPane;
public JEditorPane editorPane;
private HTMLEditorKit kit;
private String msgBuffer=new String("");
private static CMessageWindowStack window=null;
private static JFrame frameContainer=null;
private CMessageWindowStack()
{
editorPane = new JEditorPane();
editorPane.setEditable(false);
editorPane.setContentType("text/html");
kit = new HTMLEditorKit();
editorPane.setEditorKit(kit);
StyleSheet styleSheet = kit.getStyleSheet();
styleSheet.addRule("."+MESSAGE+" {font: 10px monaco; color: black; }");
Document doc = kit.createDefaultDocument();
editorPane.setDocument(doc);
scrollPane = new JScrollPane(editorPane);
}
public static CMessageWindowStack getInstance(){
if (null==window)
{window=new CMessageWindowStack();}
return window;
}
/**
* The core
* @param sMessage
* @param sType
*/
private void updateMessages(final String sMessage, final String sType)
{
SwingUtilities.invokeLater(new Runnable() {
public void run() {
String sMessageHTML="";
String sTypeText="";
if (!sMessage.equals("\r\n")){
sTypeText = sType+": ";
}
sMessageHTML = sMessage.replaceAll("(\r\n|\n)", "<br/>");
if (!sMessageHTML.equals("<br/>"))
{
sMessageHTML = "<SPAN CLASS="+sType+">"+ sTypeText+sMessageHTML + "</SPAN>";
}
msgBuffer=msgBuffer.concat(sMessageHTML);
editorPane.setText(msgBuffer);
if ((editorPane.getDocument()).getLength()>1){
editorPane.setCaretPosition((editorPane.getDocument()).getLength()-1);
}
}
});
}
public void setContainerFrame(JFrame jFrame){
frameContainer = jFrame;
javax.swing.GroupLayout layout = new javax.swing.GroupLayout(frameContainer.getContentPane());
frameContainer.getContentPane().setLayout(layout);
layout.setHorizontalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addComponent(scrollPane)
);
layout.setVerticalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addComponent(scrollPane, javax.swing.GroupLayout.DEFAULT_SIZE, 217, Short.MAX_VALUE))
);
}
public void setVisible(boolean bVisible){
editorPane.setVisible(bVisible);
scrollPane.setVisible(bVisible);
}
public void printMsg(String sMessage){
String sType = MESSAGE;
updateMessages(sMessage,sType);
}
public void printlnMsg(String sMessage){
sMessage=sMessage.concat("\r\n");
printMsg(sMessage);
}
public static void main(String args[]){
CMessageWindowStack m_LogMgr;
JFrame frame = new JFrame();
m_LogMgr=CMessageWindowStack.getInstance();
m_LogMgr.setContainerFrame(frame);
frame.setVisible(true);
frame.setSize(500, 500);
for(int i=0;i<10000;++i){
try {
Thread.sleep(0);//becomes unresponsive if sleep(0)
} catch (Exception e) {
}
m_LogMgr.printlnMsg(i+"-----------------------");
}
}
}
可能会有一些提示http://java-sl.com/JEditorPanePerformance.html 可以帮助 – StanislavL 2013-05-14 17:25:19