JavaFXで独自のカスタムコントロールをどのように定義しますか?
私はカスタムコントロールを持っているとします。 VBox
2人で Button
その中に、そして全部を呼ぶ CustomButton
.
CustomButton.fxml
<?import javafx.scene.control.Button?>
<?import javafx.scene.layout.VBox?>
<VBox>
<Button text="Custom Button A"/>
<Button text="Custom Button B"/>
</VBox>
今私のルートレイアウトはこのように見えると言う、私は特に使用できるようにしたいです CustomButton
まるでそれが Label
または定期的に Button
.
MainView.fxml
<?import javafx.scene.layout.HBox?>
<?import javafx.scene.control.Button?>
<HBox>
<Button text="Main Button"></Button>
<CustomButton />
</HBox>
Main.java
import javafx.application.Application;
import javafx.fxml.FXMLLoader;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.stage.Stage;
public class Main extends Application {
@Override
public void start(Stage primaryStage) throws Exception{
Parent root = FXMLLoader.load(getClass().getResource("MainView.fxml"));
primaryStage.setScene(new Scene(root));
primaryStage.show();
}
public static void main(String[] args) {
launch(args);
}
}
現在、これは
Exception in thread "main" 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$155(LauncherImpl.java:182)
at java.lang.Thread.run(Thread.java:745)
Caused by: javafx.fxml.LoadException: CustomButton is not a valid type.
使用するには何を変更しなければなりません CustomButton
適切なコントロールとして?
回答:
回答№1は1この質問にはすでに答えがあると思います CustomControl答えとして、あなたのFXMLでカスタムコンポーネントを使うためには、それを式と一緒に含める必要があります fx:include
例としてあなたのコンテナの中に
<fx:include source="CustomButton.fxml"/>
この場合、コンポーネントは追加されます グラフィカルに あなたは彼自身のコントローラーでそれを扱うことができます fx:含める