2013-05-14 31 views
3

我是JavaFX的新手,我在stackoverflow和google中尝试过搜索解决方案,但找不到解决方案。如何从JavaFX中的字符串加载FXML

对不起,如果这是一个reporst。

这是我的问题。我试图从字符串中加载FXML,这可能吗?假设字符串结果包含FXML信息。

InputStream IS2 = new ByteArrayInputStream(result.getBytes("UTF-8")); 

FXMLLoader fxmlLoader = new FXMLLoader(); 
fxmlLoader.setBuilderFactory(new JavaFXBuilderFactory()); 

Parent root = (Parent) fxmlLoader.load(IS2); 

但代码不会工作。

感谢您的帮助。从字符串

+0

'但是代码不会work'是不是得到一个很好的答案有帮助的声明 - 总是解释为什么一些不工作,并提供错误输出,屏幕截图等不正确的行为。 – jewelsea 2013-05-15 08:33:47

回答

4

加载FXML对我的作品(Java的8b89,OS X 10.8)

FXMLLoader loader = new FXMLLoader(); 
AnchorPane layout = (AnchorPane) loader.load(
    new ByteArrayInputStream(FXML_STRING.getBytes()) 
); 

注意相对引用将无法工作。例如,在下面的示例代码中,css样式表包含被注释掉了,因为它需要一个完整的URL而不是一个相对的文件名来解析(因为从InputStream加载时没有相对路径来解决)。

下面是一个FXML sample的适应从字符串加载FXML而不是文件:

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 for 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 to load from a file rather than a 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 as FruitComboApplication.java\n" + 
     "  ensure build system copies the fxml file to the 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"; 
} 

输出(无CSS看起来难看,但仍具有功能)。为了完整

orangeimage

控制器类:

​​