2016-05-03 18 views
1

Ich habe versucht, Elemente über die FXML-Datei zu einer Combobox hinzuzufügen, aber ich erhalte diesen Fehler.Kann Combobox keine Elemente hinzufügen

javafx.fxml.LoadException: String is not a valid type. 
/C:/Users/kim/Desktop/JavaFX/ComboboxFx/bin/application/Main.fxml:13 

    at javafx.fxml.FXMLLoader.constructLoadException(Unknown Source) 
    at javafx.fxml.FXMLLoader.createElement(Unknown Source) 
    at javafx.fxml.FXMLLoader.processStartElement(Unknown Source) 
    at javafx.fxml.FXMLLoader.loadImpl(Unknown Source) 
    at javafx.fxml.FXMLLoader.loadImpl(Unknown Source) 
    at javafx.fxml.FXMLLoader.loadImpl(Unknown Source) 
    at javafx.fxml.FXMLLoader.loadImpl(Unknown Source) 
    at javafx.fxml.FXMLLoader.loadImpl(Unknown Source) 
    at javafx.fxml.FXMLLoader.loadImpl(Unknown Source) 
    at javafx.fxml.FXMLLoader.loadImpl(Unknown Source) 
    at javafx.fxml.FXMLLoader.load(Unknown Source) 
    at application.Main.start(Main.java:13) 
    at com.sun.javafx.application.LauncherImpl.lambda$launchApplication1$162(Unknown Source) 
    at com.sun.javafx.application.PlatformImpl.lambda$runAndWait$175(Unknown Source) 
    at com.sun.javafx.application.PlatformImpl.lambda$null$173(Unknown Source) 
    at java.security.AccessController.doPrivileged(Native Method) 
    at com.sun.javafx.application.PlatformImpl.lambda$runLater$174(Unknown Source) 
    at com.sun.glass.ui.InvokeLaterDispatcher$Future.run(Unknown Source) 
    at com.sun.glass.ui.win.WinApplication._runLoop(Native Method) 
    at com.sun.glass.ui.win.WinApplication.lambda$null$148(Unknown Source) 
    at java.lang.Thread.run(Unknown Source) 

Hier ist meine FXML

<?xml version="1.0" encoding="UTF-8"?> 

<?import javafx.collections.FXCollections?> 
<?import javafx.scene.control.ComboBox?> 
<?import javafx.scene.control.Label?> 
<?import javafx.scene.layout.AnchorPane?> 

<AnchorPane prefHeight="300.0" prefWidth="300.0" xmlns="http://javafx.com/javafx/8.0.65" xmlns:fx="http://javafx.com/fxml/1" > 
    <children> 
     <ComboBox fx:id="comboBox"> 
     <items> 
      <FXCollections fx:factory="observableArrayList"> 
       <String fx:value = "Gilbert" /> 
       <String fx:value = "John" /> 
      </FXCollections> 
     </items> 
     </ComboBox> 
     <Label fx:id="selection" layoutX="34.0" layoutY="65.0" prefHeight="17.0" prefWidth="102.0" text="Label" /> 
    </children> 
</AnchorPane> 

Der Haupt

package application; 

import javafx.application.Application; 
import javafx.fxml.FXMLLoader; 
import javafx.stage.Stage; 
import javafx.scene.Parent; 
import javafx.scene.Scene; 

public class Main extends Application { 
    @Override 
    public void start(Stage primaryStage) { 
     try { 
      Parent root = FXMLLoader.load(getClass().getResource("/application/Main.fxml")); 
      Scene scene = new Scene(root,400,400); 
      scene.getStylesheets().add(getClass().getResource("application.css").toExternalForm()); 
      primaryStage.setScene(scene); 
      primaryStage.show(); 
     } catch(Exception e) { 
      e.printStackTrace(); 
     } 
    } 

    public static void main(String[] args) { 
     launch(args); 
    } 
} 

ich diesen Fehler, wenn ich es durch Szene-Builder öffnen

java.io.IOException: javafx.fxml.LoadException: Invalid attribute. 

Ich habe doppelt geprüft alles aber der Code scheint in Ordnung zu sein. Der Fehler verschwindet, wenn ich die Werte entferne, die ich der FXML hinzufügen möchte, aber meine Combobox wird leer. Es besagt, dass String kein gültiger Typ ist. Wie kann ich dieses Problem lösen? Und warum ist es ungültig? Ich habe ähnliche Fragen hier geprüft, aber die Fehler werden dadurch verursacht, dass FXCollections nicht importiert werden. Ich habe es importiert, daher denke ich, dass der Fehler nicht durch einen schlechten Import verursacht wurde.

Antwort

4

<?import java.lang.String?>

zu Ihrem fxml hinzufügen.

Verwandte Themen