How to make custom Java/JavaFX console? -
it's necessary make custom console. have following code:
public class console extends outputstream{ private console(resourcebundle resourcebundle) throws ioexception { fxmlloader loader = new fxmlloader(this.getclass().getresource("console.fxml"), resourcebundle); controller = loader.getcontroller(); scene scene = new scene((parent) loader.load()); stage = new stage(); stage.setscene(scene); show(); } @override public void write(int b) throws ioexception { controller.append(b); } public static console getinstance(resourcebundle resourcebundle) { if (console == null) { try { console = new console(resourcebundle); } catch (ioexception e) { e.printstacktrace(); //to change body of catch statement use file | settings | file templates. } } return console; } public void show() { stage.show(); } private static console console = null; private consolecontroller controller; private stage stage; }
controller file:
public class consolecontroller implements initializable { @override public void initialize(url url, resourcebundle resourcebundle) { } @fxml public void append(int i) { textarea.appendtext(string.valueof((char) i)); } @fxml private textarea textarea; }
and these called 'start()':
@override public void start(stage primarystage) throws ioexception { ... system.seterr(new printstream(console.getinstance(rb))); }
during exception nothing printed in textarea
of console.fxml
, following exception thrown:
exception in thread "javafx application thread" exception: java.lang.nullpointerexception thrown uncaughtexceptionhandler in thread "javafx application thread"
what doing wrong?
------edit--------
after understanding it's necessary use several threads have following code:
public class console{ private console(resourcebundle resourcebundle) throws ioexception { fxmlloader loader = new fxmlloader(this.getclass().getresource("console.fxml"), resourcebundle); parent root = (parent) loader.load(); controller = loader.getcontroller(); scene scene = new scene(root); stage = new stage(); stage.setscene(scene); show(); if (erroroutputthread != null) { erroroutputthread.interrupt(); try { erroroutputthread.join(); } catch (interruptedexception e) { e.printstacktrace(); //to change body of catch statement use file | settings | file templates. } erroroutputthread = null; } if (outoutputthread != null) { outoutputthread.interrupt(); try { outoutputthread.join(); } catch (interruptedexception e) { e.printstacktrace(); //to change body of catch statement use file | settings | file templates. } outoutputthread = null; } system.err.flush(); system.out.flush(); outpipedinputstream = new pipedinputstream(); outpipedoutputstream = new pipedoutputstream(outpipedinputstream); system.setout(new printstream(outpipedoutputstream)); errorpipedinputstream = new pipedinputstream(); errorpipedoutputstream = new pipedoutputstream(errorpipedinputstream); system.seterr(new printstream(errorpipedoutputstream)); outoutputthread = new thread(new consolestream(outpipedinputstream, "out")); outoutputthread.setdaemon(true); outoutputthread.start(); erroroutputthread = new thread(new consolestream(errorpipedinputstream, "error")); erroroutputthread.setdaemon(true); erroroutputthread.start(); controller.appendtext("start console"); } public static console getinstance(resourcebundle resourcebundle) { if (console == null) { try { console = new console(resourcebundle); } catch (ioexception e) { e.printstacktrace(); //to change body of catch statement use file | settings | file templates. } } return console; } public void show() { stage.show(); } private class consolestream implements runnable { private consolestream(inputstream in, string type) { inputstream = in; this.type = type; } public void run() { try { inputstreamreader = new inputstreamreader(inputstream); bufferedreader br = new bufferedreader(is); string read = null; read = br.readline(); while(read != null) { controller.appendtext(read + "\n"); read = br.readline(); } } catch (ioexception e) { e.printstacktrace(); //to change body of catch statement use file | settings | file templates. } controller.appendtext("thread" + type + "started"); } private final inputstream inputstream; private string type; } private static console console = null; private consolecontroller controller; private stage stage; private printstream printstream; private pipedoutputstream custompipedoutputstream; private pipedoutputstream errorpipedoutputstream; private pipedoutputstream outpipedoutputstream; private pipedinputstream custompipedinputstream; private pipedinputstream errorpipedinputstream; private pipedinputstream outpipedinputstream; private thread customoutputthread; private thread outoutputthread; private thread erroroutputthread; }
but result only: "start console", there no results of controller.appendtext("thread" + type + "started");
seems threads don't start. why?
i think have load form before can controller instance
fxmlloader loader = new fxmlloader(this.getclass().getresource("console.fxml"),resourcebundle); parent p = (parent) loader.load() controller = loader.getcontroller(); scene scene = new scene(p);
Comments
Post a Comment