2015-07-10 95 views
0

我想直接在我的fxml文件中加載自定義組件。但是當我運行我的示例時出現錯誤。javafx自定義組件問題

MyFXML

<?xml version="1.0" encoding="UTF-8"?> 
<?import javafx.scene.layout.VBox?> 
<?import javafx.scene.layout.GridPane?> 
<?import javafx.scene.layout.ColumnConstraints?> 
<?import javafx.scene.layout.RowConstraints?> 
<?import br.com.prati.collaboration.iem.components.PratiLabel?> 
<?import javafx.geometry.Insets?> 
<?import de.jensd.fx.glyphs.fontawesome.FontAwesomeIcon?> 
<?import br.com.prati.collaboration.iem.components.FillEmoticon?> 
<?import java.lang.*?> 
<VBox xmlns:fx="http://javafx.com/fxml/1" fx:controller="br.prati.tim.collaboration.iem.view.MetasDisplay" style="-fx-background-color: white;" prefWidth="800" prefHeight="600"> 
    <padding> 
    <Insets top="8" left="5" right="5" /> 
    </padding> 
    <GridPane> 
    <columnConstraints> 
     <ColumnConstraints hgrow="SOMETIMES" minWidth="10.0" prefWidth="400.0" /> 
     <ColumnConstraints hgrow="SOMETIMES" minWidth="10.0" /> 
    </columnConstraints> 
    <rowConstraints> 
     <RowConstraints vgrow="SOMETIMES" minHeight="10" /> 
     <RowConstraints vgrow="SOMETIMES" minHeight="10" /> 
    </rowConstraints> 
    <children> 
     <PratiLabel title="Lote Programado" GridPane.rowIndex="0" GridPane.columnIndex="0" /> 
     <PratiLabel title="Lote Realizado" GridPane.rowIndex="1" GridPane.columnIndex="0" /> 
     <PratiLabel fx:id="statusPane" title="Status Lote" GridPane.rowIndex="0" GridPane.columnIndex="1" GridPane.rowSpan="2" alignment="CENTER"> 
     <nodes> 
      <FillEmoticon sizeEM="10" fill="YELLOW" /> 
     </nodes> 
     </PratiLabel> 
    </children> 
    </GridPane> 
</VBox> 

片段FXML與我的自定義組件FillEmoticon當我運行

... 
<nodes> 
    <FillEmoticon sizeEM="10" fill="YELLOW"/> 
</nodes> 

package br.com.prati.collaboration.iem.components; 
import javafx.scene.layout.StackPane; 
import javafx.scene.paint.Paint; 
import javafx.scene.shape.Circle; 
import de.jensd.fx.glyphs.GlyphIcon; 
import de.jensd.fx.glyphs.GlyphsBuilder; 
import de.jensd.fx.glyphs.fontawesome.FontAwesomeIcon; 
import de.jensd.fx.glyphs.fontawesome.FontAwesomeIcons; 

public class FillEmoticon extends StackPane { 

    private static final double SCALE_EM_TO_PX = 4.5; 

    private GlyphIcon<?> icon; 
    private Circle filled; 

    private double sizeEM = 4.5; 

    public FillEmoticon() { 


     String iconStyle = "-fx-font-size: 4em; -fx-fill: black; -fx-stroke-width: 2px;"; 

     icon = GlyphsBuilder.create(FontAwesomeIcon.class) 
       .glyph(FontAwesomeIcons.SMILE_ALT) 
       .style(iconStyle) 
       .build(); 

     filled = new Circle(4 * sizeEM, Paint.valueOf("GREEN")); 

     getChildren().addAll(filled, icon); 

    } 

    public void setName(String name){ 
     this.icon.setGlyphName(name); 
    } 

    public void setFill(String paint){ 
     filled.setFill(Paint.valueOf(paint)); 
    } 

    public String getFill(String paint){ 
     return filled.getFill().toString(); 
    } 

    public void setSizeEM(double value){ 
     icon.setStyle("-fx-font-size: " + value + "em; -fx-font-family: Fontawesome;"); 
     filled.setRadius(value * SCALE_EM_TO_PX); 
    } 

    public double getSizeEM(){ 
     return sizeEM; 
    } 

} 

錯誤

Exception in Application start method 
java.lang.reflect.InvocationTargetException 
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) 
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62) 
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) 
    at java.lang.reflect.Method.invoke(Method.java:497) 
    at com.sun.javafx.application.LauncherImpl.launchApplicationWithArgs(LauncherImpl.java:389) 
    at com.sun.javafx.application.LauncherImpl.launchApplication(LauncherImpl.java:328) 
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) 
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62) 
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) 
    at java.lang.reflect.Method.invoke(Method.java:497) 
    at sun.launcher.LauncherHelper$FXHelper.main(LauncherHelper.java:767) 
Caused by: java.lang.RuntimeException: Exception in Application start method 
    at com.sun.javafx.application.LauncherImpl.launchApplication1(LauncherImpl.java:917) 
    at com.sun.javafx.application.LauncherImpl.lambda$launchApplication$152(LauncherImpl.java:182) 
    at com.sun.javafx.application.LauncherImpl$$Lambda$50/1463801669.run(Unknown Source) 
    at java.lang.Thread.run(Thread.java:745) 
Caused by: javafx.fxml.LoadException: 
/C:/externo/Iem/inspecao.embalagem-jar/target/classes/fxml/Metas.fxml:37 

    at javafx.fxml.FXMLLoader.constructLoadException(FXMLLoader.java:2605) 
    at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:2583) 
    at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:2445) 
    at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:3218) 
    at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:3179) 
    at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:3152) 
    at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:3128) 
    at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:3108) 
    at javafx.fxml.FXMLLoader.load(FXMLLoader.java:3101) 
    at br.prati.tim.collaboration.iem.view.MetasDisplay.start(MetasDisplay.java:30) 
    at com.sun.javafx.application.LauncherImpl.lambda$launchApplication1$159(LauncherImpl.java:863) 
    at com.sun.javafx.application.LauncherImpl$$Lambda$53/606827964.run(Unknown Source) 
    at com.sun.javafx.application.PlatformImpl.lambda$runAndWait$172(PlatformImpl.java:326) 
    at com.sun.javafx.application.PlatformImpl$$Lambda$45/1681433494.run(Unknown Source) 
    at com.sun.javafx.application.PlatformImpl.lambda$null$170(PlatformImpl.java:295) 
    at com.sun.javafx.application.PlatformImpl$$Lambda$48/1144405258.run(Unknown Source) 
    at java.security.AccessController.doPrivileged(Native Method) 
    at com.sun.javafx.application.PlatformImpl.lambda$runLater$171(PlatformImpl.java:294) 
    at com.sun.javafx.application.PlatformImpl$$Lambda$47/1685538367.run(Unknown Source) 
    at com.sun.glass.ui.InvokeLaterDispatcher$Future.run(InvokeLaterDispatcher.java:95) 
    at com.sun.glass.ui.win.WinApplication._runLoop(Native Method) 
    at com.sun.glass.ui.win.WinApplication.lambda$null$145(WinApplication.java:101) 
    at com.sun.glass.ui.win.WinApplication$$Lambda$36/2058534881.run(Unknown Source) 
    ... 1 more 
Caused by: com.sun.javafx.fxml.PropertyNotFoundException: Property "fill" does not exist or is read-only. 
    at javafx.fxml.FXMLLoader$Element.processValue(FXMLLoader.java:344) 
    at javafx.fxml.FXMLLoader$Element.processPropertyAttribute(FXMLLoader.java:321) 
    at javafx.fxml.FXMLLoader$Element.processInstancePropertyAttributes(FXMLLoader.java:231) 
    at javafx.fxml.FXMLLoader$ValueElement.processEndElement(FXMLLoader.java:763) 
    at javafx.fxml.FXMLLoader.processEndElement(FXMLLoader.java:2827) 
    at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:2536) 
    ... 22 more 
Exception running application br.prati.tim.collaboration.iem.view.MetasDisplay 

我的自定義組件有什麼問題?

回答

1

我不知道爲什麼,但似乎你需要構建這樣的:

public class FillEmoticon { 

    public StringProperty fill = new SimpleStringProperty(); 

    public StringProperty fillProperty() { 
     return fill ; 
    } 

    public final String getFill() { 
     return fillProperty().get(); 
    } 

    public final void setFill(String fill) { 
     fillProperty().set(fill); 
    } 

    public FillEmoticon() { 
     // ... 
     fill.addListener((obs, oldFill, newFill) -> filled.setFill(Paint.valueOf(newFill))); 

    } 

    // ... 
} 

,然後你必須使用一個屬性元素的FXML,而不是一個屬性:

<FillEmoticon> 
    <fill>YELLOW</fill> 
</FillEmoticon> 

請注意,由於Paint有一個valueOf方法,您可以直接訪問它,而不是間接通過String。這使事情變得更加容易:

public class FillEmoticon { 

    // code exactly as you have, except for get/setFill 

    public ObjectProperty<Paint> fillProperty() { 
     return filled.fillProperty(); 
    } 

    public final void setFill(Paint fill) { 
     fillProperty().set(fill); 
    } 

    public final Paint getFill() { 
     return fillProperty().get(); 
    } 

} 

然後您的當前FXML似乎工作。

+0

絕對精彩!非常感謝您的幫助。 –