How to load FXML from string in JavaFX -


i new javafx, , tried search solution problem in stackoverflow , google, not find solution.

sorry, if reporst.

here question. trying load fxml string, possible? let's string result contains fxml information.

inputstream is2 = new bytearrayinputstream(result.getbytes("utf-8"));  fxmlloader fxmlloader = new fxmlloader(); fxmlloader.setbuilderfactory(new javafxbuilderfactory());  parent root = (parent) fxmlloader.load(is2);   

but code wont work.

thanks help.

loading fxml string works me (java 8b89, os x 10.8)

fxmlloader loader = new fxmlloader(); anchorpane layout = (anchorpane) loader.load(   new bytearrayinputstream(fxml_string.getbytes()) ); 

note relative references won't work. example, in sample code below, css stylesheet inclusion commented out requires full url rather relative filename resolve (as there no relative path resolve against when loading inputstream).

here adaption of fxml sample load fxml string rather file:

import javafx.application.application; import javafx.fxml.fxmlloader; import javafx.scene.scene; import javafx.scene.image.image; import javafx.scene.layout.anchorpane; import javafx.stage.stage;  import java.io.bytearrayinputstream; import java.io.ioexception;  /** main application class fruit combo fxml demo application */ public class fruitcombostringapplication extends application {   public static void main(string[] args) { launch(args); }   @override public void start(stage stage) throws ioexception {     stage.settitle("choices");     stage.geticons().add(new image("http://files.softicons.com/download/application-icons/pixelophilia-icons-by-omercetin/png/32/apple-green.png"));     fxmlloader loader = new fxmlloader();     anchorpane layout = (anchorpane) loader.load(new bytearrayinputstream(fxml.getbytes())); // alternate code load file rather string: //    anchorpane layout = fxmlloader.load( //      new url(fruitcombostringapplication.class.getresource("fruitcombo.fxml").toexternalform()) //    );     stage.setscene(new scene(layout));     stage.show();   }    private static final string fxml =       "<?xml version=\"1.0\" encoding=\"utf-8\"?>\n" +       "\n" +       "<!-- fruitcombo.fxml\n" +       "     place in same directory fruitcomboapplication.java\n" +       "     ensure build system copies fxml file build output path -->\n" +       "<?import java.lang.*?>\n" +       "<?import java.net.*?>\n" +       "<?import java.util.*?>\n" +       "<?import javafx.collections.*?>\n" +       "<?import javafx.scene.control.*?>\n" +       "<?import javafx.scene.image.*?>\n" +       "<?import javafx.scene.layout.*?>\n" +       "<?import javafx.scene.paint.*?>\n" +       "<?scenebuilder-stylesheet fruitcombo.css?>\n" +       "\n" +       "<anchorpane maxheight=\"-infinity\" maxwidth=\"-infinity\" minheight=\"-infinity\" minwidth=\"-infinity\" prefheight=\"318.9998779296875\" prefwidth=\"168.0\" styleclass=\"layout\" xmlns:fx=\"http://javafx.com/fxml\" fx:controller=\"fruit.fruitcombocontroller\">\n" +       "  <children>\n" +       "    <label id=\"fruitselectorlabel\" layoutx=\"15.0\" layouty=\"10.0\" styleclass=\"bold-label\" text=\"fruit selector\" />\n" +       "    <combobox fx:id=\"fruitcombo\" layoutx=\"15.0\" layouty=\"33.0\" prefwidth=\"139.0\" prompttext=\"choose\">\n" +       "      <items>\n" +       "        <fxcollections fx:factory=\"observablearraylist\">\n" +       "          <string fx:value=\"apple\" />\n" +       "          <string fx:value=\"orange\" />\n" +       "          <string fx:value=\"pear\" />\n" +       "        </fxcollections>\n" +       "      </items>\n" +       "    </combobox>\n" +       "    <vbox alignment=\"top_center\" layoutx=\"14.0\" layouty=\"62.0\" prefheight=\"134.0\" prefwidth=\"140.0\" spacing=\"8.0\">\n" +       "      <children>\n" +       "        <stackpane id=\"selected-fruit-frame\" minheight=\"100.0\" minwidth=\"118.0\" prefheight=\"108.0\" prefwidth=\"140.0\">\n" +       "          <children>\n" +       "            <imageview fx:id=\"orangeimage\" fitheight=\"91.99999237060547\" fitwidth=\"122.66666035739114\" pickonbounds=\"true\" preserveratio=\"true\" visible=\"false\">\n" +       "              <image>\n" +       "                <image url=\"http://i.i.com.com/cnwk.1d/i/tim/2011/03/10/orange_istock_000001331357x_540x405.jpg\" preserveratio=\"false\" smooth=\"false\" />\n" +       "              </image>\n" +       "            </imageview>\n" +       "            <imageview fx:id=\"pearimage\" fitheight=\"93.0\" fitwidth=\"124.0\" pickonbounds=\"true\" preserveratio=\"true\" visible=\"false\">\n" +       "              <image>\n" +       "                <image url=\"http://smoothiejuicerecipes.com/pear.jpg\" preserveratio=\"false\" smooth=\"false\" />\n" +       "              </image>\n" +       "            </imageview>\n" +       "            <imageview fx:id=\"appleimage\" fitheight=\"93.0\" fitwidth=\"124.0\" pickonbounds=\"true\" preserveratio=\"true\" visible=\"false\">\n" +       "              <image>\n" +       "                <image url=\"http://uhallnyu.files.wordpress.com/2011/11/green-apple.jpg\" preserveratio=\"false\" smooth=\"false\" />\n" +       "              </image>\n" +       "            </imageview>\n" +       "          </children>\n" +       "        </stackpane>\n" +       "        <label fx:id=\"selectedfruit\" textalignment=\"center\" />\n" +       "      </children>\n" +       "    </vbox>\n" +       "    <button fx:id=\"savevaluebutton\" layoutx=\"14.0\" layouty=\"219.0\" mnemonicparsing=\"false\" onaction=\"#savevalue\" text=\"save value\" />\n" +       "    <label fx:id=\"valuelabel\" layoutx=\"15.0\" layouty=\"273.0\" text=\"\" />\n" +       "  </children>\n" + //      "  <stylesheets>\n" + //      "    <url value=\"@fruitcombo.css\" />\n" + //      "  </stylesheets>\n" +       "</anchorpane>\n"; } 

output (looks ugly without css, still functional).

orangeimage

controller class completeness:

package fruit;  import java.net.url; import java.util.resourcebundle; import javafx.beans.value.changelistener; import javafx.beans.value.observablevalue; import javafx.fxml.fxml; import javafx.fxml.initializable; import javafx.scene.control.combobox; import javafx.scene.control.label; import javafx.scene.image.imageview;  /** javafx fxml controller fruit combo fxml demo application. */ public class fruitcombocontroller implements initializable {    @fxml //  fx:id="appleimage"   private imageview appleimage; // value injected fxmlloader    @fxml //  fx:id="fruitcombo"   private combobox<string> fruitcombo; // value injected fxmlloader    @fxml //  fx:id="orangeimage"   private imageview orangeimage; // value injected fxmlloader    @fxml //  fx:id="pearimage"   private imageview pearimage; // value injected fxmlloader    @fxml //  fx:id="selectedfruit"   private label selectedfruit; // value injected fxmlloader    @override // method called fxmlloader when initialization complete   public void initialize(url fxmlfilelocation, resourcebundle resources) {     assert appleimage != null : "fx:id=\"appleimage\" not injected: check fxml file 'fruitcombo.fxml'.";     assert fruitcombo != null : "fx:id=\"fruitcombo\" not injected: check fxml file 'fruitcombo.fxml'.";     assert orangeimage != null : "fx:id=\"orangeimage\" not injected: check fxml file 'fruitcombo.fxml'.";     assert pearimage != null : "fx:id=\"pearimage\" not injected: check fxml file 'fruitcombo.fxml'.";     assert selectedfruit != null : "fx:id=\"selectedfruit\" not injected: check fxml file 'fruitcombo.fxml'.";      // bind selected fruit label selected fruit in combo box.     selectedfruit.textproperty().bind(fruitcombo.getselectionmodel().selecteditemproperty());      // listen changes fruit combo box selection , update displayed fruit image accordingly.     fruitcombo.getselectionmodel().selecteditemproperty().addlistener(new changelistener<string>() {       @override public void changed(observablevalue<? extends string> selected, string oldfruit, string newfruit) {         if (oldfruit != null) {           switch(oldfruit) {             case "apple":  appleimage.setvisible(false);  break;             case "orange": orangeimage.setvisible(false); break;             case "pear":   pearimage.setvisible(false);   break;           }         }         if (newfruit != null) {           switch(newfruit) {             case "apple":  appleimage.setvisible(true);   break;             case "orange": orangeimage.setvisible(true);  break;              case "pear":   pearimage.setvisible(true);    break;           }         }         }     });   } } 

Comments

Popular posts from this blog

jquery - How can I dynamically add a browser tab? -

node.js - Getting the socket id,user id pair of a logged in user(s) -

keyboard - C++ GetAsyncKeyState alternative -