将JavaFX自定义控件放在GridPane中

时间:2018-07-21 06:30:41

标签: javafx custom-controls fxml

我遵循了Oracle的Mastering FXML教程,特别是tutorial for making a custom control,它可以独立运行。但是,如果我想将此自定义控件放置在GridPane中,如何将其与GridPane的column和row属性一起放置?尝试将自定义控件放置在FXML文件中只会给我一个错误,该类不是有效的类型。

我的自定义控件FXML(view_navigation.fxml):

<?xml version="1.0" encoding="UTF-8"?>
<?import javafx.scene.*?>
<?import javafx.scene.control.*?>
<?import javafx.scene.layout.*?>
<?import javafx.scene.image.Image?>
<?import javafx.scene.image.ImageView?>

<fx:root type="javafx.scene.layout.HBox" xmlns:fx="http://javafx.com/fxml">
<Button>
    <graphic>
        <ImageView>
            <Image url="@/toolbarButtonGraphics/navigation/Back24.gif"/>
        </ImageView>
    </graphic>
</Button>
<Button text="Add Entry"  />
<Button text="Change Entry"  />
<Button text="View List"  />
<Button>
    <graphic>
        <ImageView>
            <Image url="@/toolbarButtonGraphics/navigation/Forward24.gif"/>
        </ImageView>
    </graphic>
</Button>
</fx:root> 

其关联的类(ViewNavigationControl.java):

import java.io.IOException;
import javafx.fxml.FXMLLoader;
import javafx.scene.layout.HBox;

public class ViewNavigationControl extends HBox {

public ViewNavigationControl() {
    FXMLLoader fxmlLoader = new FXMLLoader(getClass().getResource("view_navigation.fxml"));
    fxmlLoader.setRoot(this);
    fxmlLoader.setController(this);

    try {
        fxmlLoader.load();
    } catch (IOException exception) {
        throw new RuntimeException(exception);
    }

    }
}

根据教程的最后一段,将其放置在父视图的GridPane的底部:

....
    <HBox GridPane.columnIndex="0" GridPane.rowIndex="3" GridPane.columnSpan="2">
        <ViewNavigationControl />
    </HBox>
</GridPane>

我得到javafx.fxml.LoadException: ViewNavigationControl is not a valid type.

非常感谢您提供帮助!

0 个答案:

没有答案