java - JavaFX 8 update UI with Platfrom.runLater() still returns null -
i'm writing hexeditor application javafx 8 various reasons. (important is, have can't use hexeditor)
my problem when want update ui example with
textarea.settext(line); table.setitems(getlistfortable());
i nullpointerexception because textarea
(textarea) , table
(tableview) null, if have data before intialize stage , set components works.
in other questions read platfrom.runlater()
should solve somehow doesn`t work me, still nullpointerexception. i'm sure i'm in applications main thread, because application uses 1 thread.
here full code method:
public void openfile() //this called when user presses "open"-menuitem { filechooser fc = new filechooser(); fc.getextensionfilters().addall( new extensionfilter("text files", "*.txt"), new extensionfilter("all files", "*.*")); file = fc.showopendialog(primarystage); if(file != null) { create(); platform.runlater(() -> textarea.settext(line)); //npe } }
also tried using button update ui works if user presses button , not button.fire()
method, thats not workaround if have press button everytime did see changes.
edit: here methods initializing code in start()
method. , said: i`m sure there getting assigned correctly, because can use them setting listeners , such while starting program.
private textarea textarea; private tableview<row> table; //row own class used contents table public static void main(string[] args){ launch(args); } @override public void start(stage primarystage) { this.primarystage = primarystage; initrootlayout(); showcontentscene(); } private void initrootlayout() { //initializes root layout frame content try { // load root layout fxml file. fxmlloader loader = new fxmlloader(); loader.setlocation(editor.class.getresource("rootlayout.fxml")); rootlayout = (borderpane) loader.load(); // show scene containing root layout. scene scene = new scene(rootlayout); primarystage.setscene(scene); primarystage.show(); } catch (ioexception e) { e.printstacktrace(); } } private void showcontentscene() { //initializes content view try { // load content (panel). fxmlloader loader = new fxmlloader(); loader.setlocation(editor.class.getresource("content.fxml")); contentpane = (anchorpane) loader.load(); table = (tableview<row>) loader.getnamespace().get("table"); textarea = (textarea) loader.getnamespace().get("textarea"); // set content center of root layout. rootlayout.setcenter(contentpane); } catch (ioexception e) { e.printstacktrace(); } }
edit: overworked code structure have exception loader telling me "root cannot null".
edit: viewing this: structure problem, make sure every .fxml-file use has own controller.
your structure unusual. either construct nodes in code , don't use , fxmlloader or let fxmlloader construct nodes you. in case doing both, can work, tricky right (as have discovered).
so, given using fxml, use constructing of nodes , place references constructed nodes in controller, references annotated @fxml fxmlloader initializes them. then, never assign references new nodes create.
ensure references , controller in different classes application class, because never want make main application class controller, main application class should invoke fxmlloader, not need node references.
you never need use platform.runlater unless running on non-javafx application thread , want execute code on javafx application thread. example not case, because comment openfile this called when user presses "open"-menuitem
. javafx framework handles input event user pressing open menu item , ensures event handler invoked on javafx application thread, trying use platform.runlater in such situation both confusing , redundant.
even if address of issues mentioned in answer, program may still not work expect may have other issues, cannot verified current code in question. instance, question code includes neither fxml or controller code. required find issues code , address this.
Comments
Post a Comment