在treeview中,当discdetails对象的“changed”布尔属性设置为“true”时,如何在treeitem显示文本的末尾添加星号(*)?
在textfield中进行任何更改时,“changed”布尔值设置为true
下面是一个简短的代码:
public class TreeBind extends Application {
public static void main(final String[] args) {
Application.launch(args);
}
@Override
public void start(Stage primaryStage) {
final TreeItem<DiscDetails> treeItemRoot = new TreeItem<DiscDetails>(new DiscDetails("root"));
final TreeView<DiscDetails> treeView = new TreeView<DiscDetails>(treeItemRoot);
final DiscDetails disc = new DiscDetails("Title 1");
treeItemRoot.getChildren().add(new TreeItem<DiscDetails>(disc));
treeView.setMaxWidth(150);
final HBox hBox = new HBox();
final Label lbTitle = new Label("Title");
final TextField txtTitle = new TextField();
txtTitle.textProperty().bindBidirectional(disc.textProperty());
createCommitBinding(txtTitle).addListener((obs, oldText, newText) -> {
disc.changedProperty().set(true);
System.out.printf("Text 1 changed from \"%s\" to \"%s\"%n", oldText, newText);
});
hBox.getChildren().addAll(lbTitle, txtTitle);
final HBox root = new HBox();
root.getChildren().addAll(treeView, hBox);
final Scene scene = new Scene(root, 400, 300);
primaryStage.setScene(scene);
primaryStage.show();
}
private StringBinding createCommitBinding(TextField textField) {
final StringBinding binding = Bindings.createStringBinding(() -> textField.getText());
textField.addEventHandler(ActionEvent.ACTION, evt -> binding.invalidate());
textField.focusedProperty().addListener((obs, wasFocused, isFocused) -> {
if (!isFocused)
binding.invalidate();
});
return binding;
}
}
还有discdetails类
public class DiscDetails {
private final StringProperty title;
private final StringProperty text;
private final BooleanProperty changed = new SimpleBooleanProperty(false);
DiscDetails(String title) {
this.title = new SimpleStringProperty(title);
text = new SimpleStringProperty("Some text...");
}
public StringProperty titleProperty() {
return title;
}
public StringProperty textProperty() {
return text;
}
public BooleanProperty changedProperty() {
return changed;
}
@Override
public String toString() {
return title.getValue();
}
}
暂无答案!
目前还没有任何答案,快来回答吧!