将自定义组件添加到SceneBuilder 2.0

21

我需要在一个面板上添加选择监听器和选择方法,以便在单击节点时监视并呈现高亮。

我做了以下操作:

public class PaneWithSelectionListener extends Pane {

    private ObjectProperty<Annotation> selectedAnnotation = new SimpleObjectProperty<>();

    public PaneWithSelectionListener() { 
        super();
        selectedAnnotation.addListener((obs, oldAnno, newAnno) -> {
            if (oldAnno != null) {
                oldAnno.setStyle("");
            }
            if (newAnno != null) {
                newAnno.setStyle("-fx-border-color: blue;-fx-border-insets: 5;-fx-border-width: 1;-fx-border-style: dashed;");
            }
        });

        setOnMouseClicked(e->selectAnnotation(null));
    }

    public void selectAnnotation(Annotation ann){
        selectedAnnotation.set(ann);
    }
}

这很有效-但是,由于我的FXML引用了PaneWithSelectionListener而不是Pane,我无法再使用SceneBuilder。我不知道如何将我的自定义面板放入SceneBuilder。我查看了其他问题,它们都是FXML和控制器的组合-其中只有一个Pane

有人知道如何做到这一点,或者在初始化时将Pane替换为PaneWithSelectionListener吗?

谢谢


你只需要单选吗? - James_D
是的 - 我只需要单选。我正在表示图像顶部的注释。如果用户点击其中一个,它应该被选择。我认为他们不需要多重选择。只需要指示哪一个将受到影响。谢谢。 - purring pigeon
2个回答

31
如果问题仅是使您的自定义类在SceneBuilder中可用,您可以按照以下步骤执行:
  1. 将您的自定义类(以及任何支持类,例如Annotation)作为jar文件捆绑
  2. 在SceneBuilder中,激活左侧窗格顶部“库”旁边的下拉按钮: enter image description here
  3. 选择“导入JAR / FXML文件...”
  4. 选择从第1步创建的Jar文件
  5. 确保您需要在SceneBuilder中访问的类(PaneWithSelectionListener)已被选中
  6. 点击“导入组件”
  7. PaneWithSelectionListener现在将出现在SceneBuilder的左侧窗格中的“自定义”下: enter image description here
您会注意到,在SceneBuilder中,下拉列表有一个“自定义库文件夹”选项,您可以从中打开存储jar文件的文件夹。对于快速选项,您可以将jar文件复制到此文件夹中,然后(经过短暂延迟),其中包含的类将出现在“自定义”列表中。

2
我昨天确实尝试过,但是当我尝试加载它时,选择项是空白的。 - purring pigeon
好的,这很奇怪。我把我的组件移动到另一个包中并导出了jar文件,然后它就可以工作了。 - purring pigeon
1
如果您将其放在默认包中,我认为这会导致它失败(FXML通常与默认包不兼容,尽管这只是针对非常快速的测试代码)。 - James_D
如果使用SceneBuilder 2.0或更低版本,当从eclipse打开时,此解决方案仍将无法加载使用自定义控件的fxml。为使其正常工作,您需要将jar添加到SceneBuilder的package.cfg文件中。请参见此已接受答案的底部部分这里。注意:Linux使用“:”而不是“;”来分隔路径上的jar。 - Olmstov
显示剩余2条评论

0

我创建了一个CustomCB,它是一个用户对象类型的组合框。在我的情况下,我使用<APerson>作为userObject。整个项目和测试器都在这里。第一个是CustomCB,该组件和由此代码生成的JAR文件已添加到库中。这可以在SCENE BUILDER中加载。然后它就可以用于场景设计。

当然,您还有一个测试器CustomCB2,它也可以使用FXML而不是我所做的方式。

实际上,我希望以我在ComboBox中输入的文本开头的项目出现在列表中。但我不知道如何做。因为PERSON类的FIRST NAME或LAST NAME可能以'Pe'开头。如果我找到解决方案,我会在这里发布。

这是自定义组件。

package customCB;
    /*
     * To change this license header, choose License Headers in Project Properties.
     * To change this template file, choose Tools | Templates
     * and open the template in the editor.
     */
    import javafx.beans.property.IntegerProperty;
    import javafx.beans.property.SimpleIntegerProperty;
    import javafx.beans.property.SimpleStringProperty;
    import javafx.beans.property.StringProperty;

    /**
     *
     * @author Hornigold Arthur
     */

    public class APerson {

    private final StringProperty firstName;
    private final StringProperty lastName;
    private final IntegerProperty familyID;
    private final IntegerProperty personID;

    public APerson() {
        this(null, null, 0,0);
    }

    /**
     * Constructor with some initial data.
     * 
     * @param familyID
     * @param familyName
     */

    public  APerson (String firstName, String lastName, int familyID, int personID) {
        this.firstName = new SimpleStringProperty(firstName);
        this.lastName = new SimpleStringProperty(lastName);
        this.familyID = new SimpleIntegerProperty(familyID);
        this.personID = new SimpleIntegerProperty(personID);
    }

    public int getFamilyID() {
        return familyID.get();
    }

    public void setFamilyID(int FamilyID) {
        this.familyID.set(FamilyID);
    }

    public IntegerProperty familyIDProperty() {
        return familyID;
    }

    public int getPersonID() {
        return personID.get();
    }

    public void setPersonID(int PersonID) {
        this.personID.set(PersonID);
    }

    public IntegerProperty personIDProperty() {
        return personID;
    }

    public String getFirstName() {
        return firstName.get();
    }

    public void setFirstName(String FirstName) {
        this.firstName.set(FirstName);
    }

    public StringProperty firstNameProperty() {
        return firstName;
    }

    public String getLastName() {
        return lastName.get();
    }

    public void setLastName(String LastName) {
        this.lastName.set(LastName);
    }

    public StringProperty lastNameProperty() {
        return lastName;
    }


    public String toString() {
        String name = getFirstName() + " " + getLastName()+ " [" + getFamilyID() +"]";
        return name;
    }
}

这是自定义组件的FXML。

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

<?import javafx.scene.control.ComboBox?>
<?import javafx.scene.layout.VBox?>

<fx:root stylesheets="@application.css" type="javafx.scene.layout.VBox" xmlns="http://javafx.com/javafx/10.0.1" xmlns:fx="http://javafx.com/fxml/1">
    <ComboBox fx:id="myCustomCombo" editable="true" onAction="#cbOnAction" prefWidth="300.0" style="-fx-background-color: white;" />
</fx:root>

这是FXML的控制器,但不要在FXML文件中提及它。否则会抛出错误。

package customCB;

import java.io.IOException;
import java.net.URL;
import java.util.ResourceBundle;

import org.controlsfx.control.textfield.TextFields;

import javafx.event.ActionEvent;
import javafx.fxml.FXML;
import javafx.fxml.FXMLLoader;
import javafx.scene.control.ComboBox;
import javafx.scene.control.ListView;
import javafx.scene.layout.VBox;

public class CustomComboController extends VBox{

    @FXML
    private ResourceBundle resources;

    @FXML
    private URL location;

    @FXML
    private ComboBox<APerson> myCustomCombo;

    @FXML
    void cbOnAction(ActionEvent event) {

    }

    @FXML
    void initialize() {
        assert myCustomCombo != null : "fx:id=\"myCustomCombo\" was not injected: check your FXML file 'CustomLvFXML.fxml'.";
    }

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

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

    public void setCBValues(javafx.collections.ObservableList<APerson> values) {
        myCustomCombo.setItems(values);
        myCustomCombo.setEditable(true);
        TextFields.bindAutoCompletion(myCustomCombo.getEditor(), myCustomCombo.getItems());
    }

}

从网站下载controlsfx-8.40.12.jar,并将其作为库包含在BUILD PATH中。

现在为该项目创建一个jar文件,名为“CustomCB.jar”。

这个JAR文件必须作为定制控件包含在Scene Builder中。我使用的是版本10.0。

既然它已经成为Scene builder的一部分,你就可以在设计中使用这个组件,除非你能像我在测试代码中所做的那样操作。你需要将“CustomCB.jar”作为构建的一部分包含进来。

这是测试者的代码。

package customCB2;


    import javafx.application.Application;
    import javafx.stage.Stage;
    import javafx.scene.Scene;
    import javafx.scene.layout.VBox;


    public class Main extends Application {

        static private javafx.collections.ObservableList<APerson> fathers = javafx.collections.FXCollections.observableArrayList();
        static private javafx.collections.ObservableList<APerson> mothers = javafx.collections.FXCollections.observableArrayList();



    @Override
    public void start(Stage stage) throws Exception {

        CustomComboController customControl2 = new CustomComboController();
        CustomComboController customControl3 = new CustomComboController();

        loadFathers();
        loadMothers();

        customControl2.setCBValues(fathers);
        customControl3.setCBValues(mothers);

        VBox root = new VBox();
        root.getChildren().addAll(customControl2, customControl3);

        stage.setScene(new Scene(root));
        stage.setTitle("Custom Control Combo box");
        stage.setWidth(300);
        stage.show();
    }

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

    private void loadFathers() {
        fathers.clear();
        fathers.add(new APerson("Hornigold","Arthur",1,63));
        fathers.add(new APerson("Andrews","Sundareson",2,60));
        fathers.add(new APerson("Christopher","Easweradoss",3,57));
        fathers.add(new APerson("Arthur","Kennedy",4,55));
    }

    private void loadMothers() {
        mothers.clear();
        mothers.add(new APerson("Victoria","Arthur",1,95));
        mothers.add(new APerson("Eliza", "Daniel",1,60));
        mothers.add(new APerson("Nesammal", "Rivington",2,57));
        mothers.add(new APerson("Ratnammal","Andews",1,55));
    }


}

它需要很多改进。如果有人能够改进,请在这里添加。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接