问题描述
我是 javaFX 的新手.我在java中创建了一个自定义的搜索框(扩展TextField),检查图像:
我使用测试类对其进行了测试,并且可以正常工作.
我现在想知道是否可以创建其 FXML 文件而不是将此组件添加到场景构建器?怎么做 ?提前致谢.
如何将组件从 JAR 导入 SceneBuilder
您可以将组件放入 Jar 并将其导入 SceneBuilder.您无需为组件创建 FXML 文件即可将其添加到 SceneBuilder 库面板.
请参阅
尝试从命令行启动 Scene Builder,您应该会看到库导入的输出,包括添加自定义控件时可能出现的异常.
I'm new to javaFX. I created a customized Search box (extends TextField) in java, check image:
I tested it with a test class and it's working.
I want to know now if it's possible to create its FXML file than add this component to scene builder ? how to do it ? Thanks in advance.
How to Import a Component from a JAR into SceneBuilder
You can put your component in a Jar and import it into SceneBuilder. You don't need to create an FXML file for your component to add it to SceneBuilder Library Panel.
See the Adding Custom Components to the Library section of the JavaFX user guide.
Note, SceneBuilder also supports importing of FXML based components rather than just straight code components. This answer only discusses importing of code only components which do not contain FXML.
Sample Imported Component Usage
Here is a custom search field component that I imported into SceneBuilder using the method outlined above.
The top search panel is in the Scene Builder design pane, the bottom search panel is the result of using the Scene Builder preview function and searching for happiness.
Sample SceneBuilder Generated Code
The fxml file which was generated by SceneBuilder based on the design is included here. Note, this was just a test scene I created with SceneBuilder to test the already imported component - it was not part of the component import process itself.
<?xml version="1.0" encoding="UTF-8"?>
<?import javafx.scene.text.*?>
<?import org.jewelsea.*?>
<?import javafx.geometry.*?>
<?import javafx.scene.control.*?>
<?import java.lang.*?>
<?import javafx.scene.layout.*?>
<VBox maxHeight="-Infinity" maxWidth="-Infinity" minHeight="-Infinity" minWidth="-Infinity" spacing="10.0" xmlns="http://javafx.com/javafx/8" xmlns:fx="http://javafx.com/fxml/1">
<children>
<Label text="Search Field Import Test">
<font>
<Font size="16.0" />
</font>
</Label>
<SearchField />
</children>
<padding>
<Insets bottom="10.0" left="10.0" right="10.0" top="10.0" />
</padding>
</VBox>
Sample (importable) Component Code
The code for the search box which was imported is:
package org.jewelsea;
import javafx.geometry.Insets;
import javafx.scene.control.Button;
import javafx.scene.control.Label;
import javafx.scene.control.TextField;
import javafx.scene.layout.HBox;
import javafx.scene.layout.StackPane;
import javafx.scene.layout.VBox;
public class SearchField extends StackPane {
private final TextField textField;
private final Button searchButton;
private final Label searchResults;
public SearchField() {
textField = new TextField();
textField.setPromptText(
"Search Text"
);
searchButton = new Button("Search");
searchResults = new Label();
VBox layout = new VBox(
20,
new HBox(
10,
textField,
searchButton
),
searchResults
);
layout.setPadding(new Insets(10));
searchButton.setOnAction(event ->
searchResults.setText(
"Search result for " + textField.getText()
)
);
getChildren().setAll(
layout
);
}
}
Component Pre-requisites
In order for the process to work, there are a few things you need to ensure:
- Your component class extends Node.
- Your component class has a no argument constructor.
- Your component class and no argument constructor are public.
- Your component class is in a package (e.g. org.jewelsea) - it can't have no package set.
- Your component class is packaged in a JAR file which has been imported into SceneBuilder as described above.
Troubleshooting
If you are having issues importing the JAR, after you have attempted a JAR import, you can use the JAR analysis function documented below to help troubleshoot (which might help or might just provide some cryptic information to confuse you more).
Also, from this answer:
这篇关于java - 如何为Java中已经创建的新组件创建FXML文件而不是将其添加到场景构建器?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!