为SceneBuilder(JavaFX)构建自定义组件
我遵循这个博客,构建了自己的自定义组件 FXML文件:为SceneBuilder(JavaFX)构建自定义组件,java,javafx,custom-controls,Java,Javafx,Custom Controls,我遵循这个博客,构建了自己的自定义组件 FXML文件: <?xml version="1.0" encoding="UTF-8"?> <?import java.lang.*?> <?import java.net.*?> <?import java.util.*?> <?import javafx.scene.*?> <?import javafx.scene.control.*?> <?import javafx
<?xml version="1.0" encoding="UTF-8"?>
<?import java.lang.*?>
<?import java.net.*?>
<?import java.util.*?>
<?import javafx.scene.*?>
<?import javafx.scene.control.*?>
<?import javafx.scene.layout.*?>
<fx:root id="AnchorPane" maxHeight="-Infinity" prefHeight="25.0" prefWidth="400.0" styleClass="mainFxmlClass" type="AnchorPane" xmlns="http://javafx.com/javafx/8" xmlns:fx="http://javafx.com/fxml/1">
<stylesheets>
<URL value="@style.css" />
</stylesheets>
<children>
<HBox fx:id="border" prefHeight="25.0" prefWidth="400.0" styleClass="textbox-container" AnchorPane.leftAnchor="0.0" AnchorPane.rightAnchor="0.0">
<children>
<Label fx:id="label" maxHeight="1.7976931348623157E308" maxWidth="1.7976931348623157E308" prefHeight="25.0" prefWidth="200.0" styleClass="textbox-label" text="Label" HBox.hgrow="ALWAYS" />
<HBox minWidth="200.0" prefWidth="200.0" HBox.hgrow="ALWAYS">
<children>
<TextField fx:id="textbox" prefWidth="176.0" styleClass="textbox" HBox.hgrow="ALWAYS" />
<Button fx:id="clear" focusTraversable="false" mnemonicParsing="false" styleClass="button-controller" text="X" />
</children>
</HBox>
</children>
</HBox>
</children>
</fx:root>
输出:
现在我的问题是,我无法从场景生成器更改label的值。我们是否可以创建一个自定义字段,该字段将显示在场景生成器上,并有助于更改标签文本?为了在场景生成器上具有可编辑字段,您应该使用属性。否则,将使用
ReadOnlyStringWrapper
例如:
private final BooleanProperty editable = new SimpleBooleanProperty();
public boolean isEditable() {
return editable.get();
}
public void setEditable(boolean value) {
editable.set(value);
}
public final BooleanProperty editableProperty() {
return editable;
}
private final StringProperty textFromTextBox = new SimpleStringProperty();
public String getTextFromTextBox() {
return textFromTextBox.get();
}
public void setTextFromTextBox(String value) {
textFromTextBox.set(value);
}
public final StringProperty textFromTextBoxProperty() {
return textFromTextBox;
}
private final StringProperty labelText = new SimpleStringProperty();
public String getLabelText() {
return labelText.get();
}
public void setLabelText(String value) {
labelText.set(value);
}
public final StringProperty labelTextProperty() {
return labelText;
}
然后将控件绑定到这些属性:
public CellTextFieldClear(){
...
textbox.editableProperty().bind(editableProperty());
textbox.textProperty().bind(textFromTextBoxProperty());
label.textProperty().bind(labelTextProperty());
}
private final BooleanProperty editable = new SimpleBooleanProperty();
public boolean isEditable() {
return editable.get();
}
public void setEditable(boolean value) {
editable.set(value);
}
public final BooleanProperty editableProperty() {
return editable;
}
private final StringProperty textFromTextBox = new SimpleStringProperty();
public String getTextFromTextBox() {
return textFromTextBox.get();
}
public void setTextFromTextBox(String value) {
textFromTextBox.set(value);
}
public final StringProperty textFromTextBoxProperty() {
return textFromTextBox;
}
private final StringProperty labelText = new SimpleStringProperty();
public String getLabelText() {
return labelText.get();
}
public void setLabelText(String value) {
labelText.set(value);
}
public final StringProperty labelTextProperty() {
return labelText;
}
public CellTextFieldClear(){
...
textbox.editableProperty().bind(editableProperty());
textbox.textProperty().bind(textFromTextBoxProperty());
label.textProperty().bind(labelTextProperty());
}