将控制台输出重定向到JavaFX TextArea? [英] Redirect Console Output to JavaFX TextArea?
问题描述
我想在JavaFX TextArea中显示控制台输出...遗憾的是,我找不到任何工作示例JavaFX,但只为Java Swing,这似乎不适用于我的情况。
并扩展我的代码,如下所示。但是,在Eclipse IDE中不再有控制台输出,但在TextArea中没有输出。任何想法我在哪里做错了?
public class Activity extends OutputStream implements可初始化{
@FXML
public static TextArea taRecentActivity;
public Activity(){
// TODO自动生成的构造函数stub
}
@Override
public void initialize ,ResourceBundle resources){
OutputStream out = new OutputStream(){
@Override
public void write(int b)throws IOException {
updateTextArea(String.valueOf ((char)b));
}
@Override
public void write(byte [] b,int off,int len)throws IOException {
updateTextArea(new String(b,off, len));
}
@Override
public void write(byte [] b)throws IOException {
write(b,0,b.length);
}
};
System.setOut(new PrintStream(out,true));
System.setErr(new PrintStream(out,true));
}
private void updateTextArea(final String text){
SwingUtilities.invokeLater(new Runnable(){
public void run(){
taRecentActivity .appendText(text);
}
});
}
@Override
public void write(int arg0)throws IOException {
// TODO自动生成方法存根
}
}
工作。虽然大量的文本很慢。
public void appendText(String str){
Platform.runLater (() - > textField.appendText(str));
}
和
@Override
public void initialize(URL location,ResourceBundle resources){
OutputStream out = new OutputStream(){
@Override
public void write(int b)throws IOException {
appendText(String.valueOf((char)b));
}
};
System.setOut(new PrintStream(out,true));
}
I want to show the console output in a JavaFX TextArea... unfortunately I cannot find any working example for JavaFX but only for Java Swing, which not seems to work in my case.
EDIT:
I tried to follow this example: http://unserializableone.blogspot.ch/2009/01/redirecting-systemout-and-systemerr-to.html
and extended my code as shown below. However, there is no console output anymore in my Eclipse IDE but also no output in my TextArea. Any idea where I am doing wrong?
public class Activity extends OutputStream implements Initializable {
@FXML
public static TextArea taRecentActivity;
public Activity() {
// TODO Auto-generated constructor stub
}
@Override
public void initialize(URL location, ResourceBundle resources) {
OutputStream out = new OutputStream() {
@Override
public void write(int b) throws IOException {
updateTextArea(String.valueOf((char) b));
}
@Override
public void write(byte[] b, int off, int len) throws IOException {
updateTextArea(new String(b, off, len));
}
@Override
public void write(byte[] b) throws IOException {
write(b, 0, b.length);
}
};
System.setOut(new PrintStream(out, true));
System.setErr(new PrintStream(out, true));
}
private void updateTextArea(final String text) {
SwingUtilities.invokeLater(new Runnable() {
public void run() {
taRecentActivity.appendText(text);
}
});
}
@Override
public void write(int arg0) throws IOException {
// TODO Auto-generated method stub
}
}
I just did this and it worked. Though it was very slow with large amounts of text.
public void appendText(String str) {
Platform.runLater(() -> textField.appendText(str));
}
and
@Override
public void initialize(URL location, ResourceBundle resources) {
OutputStream out = new OutputStream() {
@Override
public void write(int b) throws IOException {
appendText(String.valueOf((char) b));
}
};
System.setOut(new PrintStream(out, true));
}
这篇关于将控制台输出重定向到JavaFX TextArea?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!