[英]JTextPane text not updatng probably from scope issues
I am trying to change the contents of a JTextPane. 我正在尝试更改JTextPane的内容。 I think the problem is scope issues. 我认为问题是范围问题。 You can't compile this code but you get the idea... This may be a tough one because of the multi-threading. 您无法编译此代码,但您会明白...由于多线程,这可能很难。 TYIA TYIA
C:\ucdhb2\gaia\inigui\inigui15\src\inigui13.java:259: error: local
variable theframe is accessed from within inner class; needs to be declared final
theframe.mainfield.getStyledDocumemt().insertString(1, "hellooo",
null);
^ C:\ucdhb2\gaia\inigui\inigui15\src\inigui13.java:259: error: cannot
find symbol
theframe.mainfield.getStyledDocumemt().insertString(1, "hellooo",
null);
^ symbol: method getStyledDocumemt() location: variable mainfield of type JTextField
2 errors
class inigui13 extends applet {
Socket echoSocket = null;
PrintWriter out = null;
BufferedReader in = null;
JTextField mainfield;
public void init() {
inigui13 theframe = new inigui13();
theframe.setSize(700, 525);
theframe.setVisible(true);
try {
echoSocket = new Socket("192.168.2.3", 9900);
out = new PrintWriter(echoSocket.getOutputStream(), true);
in = new BufferedReader(new InputStreamReader( echoSocket.getInputStream() ) );
} catch (UnknownHostException e) {
System.err.println("Sstemexit");
System.exit(1);
} catch (IOException e) {
System.err.println("IOFailed");
System.exit(1);
}
Runnable readsock = new Runnable() {
public void run() {
boolean recvread = true;
while( recvread ) {
BufferedReader stdIn = new BufferedReader( new InputStreamReader(System.in));
try {
theframe.mainfield.getStyledDocumemt().insertString(1, "hellooo", null);
} catch( IOException e ){}
}
}
try {
out.close();
in.close();
echoSocket.close();
} catch( IOException e ){}
}
};
Thread readsockt = new Thread(readsock);
readsockt.start();
}
public inigui13 {
mainfield = new JTextPane;
this.add(mainfield);
}
};
}
Do not, EVER , update, change or modify ANY UI component from any thread OTHER then the Event Dispatching Thread . 不这样做, 永远 ,更新,更改或修改任何线程其他然后在事件调度线程 任何 UI组件。 This just about covers the first 10 commandments of Swing. 这几乎涵盖了Swing的前10条诫命。
You code is a classic example of when to use a SwingWorker
. 您的代码是何时使用SwingWorker
的经典示例。
You might like to have read through Concurrency in Swing 您可能想通读Swing中的并发性
Example 例
public class StreamReaderWorker extends SwingWorker<Object, String> {
private JTextPane field;
public StreamReaderWorker(JTextPane field) {
this.field = field;
}
@Override
protected void process(List<String> chunks) {
for (String chunk : chunks) {
try {
field.getStyledDocument().insertString(1, "chunk", null);
} catch (BadLocationException ex) {
ex.printStackTrace();
}
}
}
@Override
protected String doInBackground() throws Exception {
boolean recvread = true;
while (recvread) {
BufferedReader stdIn = new BufferedReader(new InputStreamReader(System.in));
try {
publish("hellooo");
} catch (IOException e) {
}
}
return null;
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.