一段时间以来,我尝试根据FXCollections
中的属性值更改行颜色(在编辑单元格后为true->绿色行,false->红色行)。在一个SO问题中,有一些信息可以使用Observable
。我仍然不知道如何使用它,现在我尝试理解@kleopatra的帖子。我在choiceBox
中添加了3个项目,然后单击按钮后,我想更改其中之一。每次点击都会在choiceBox
中添加一行。为什么?我需要刷新此内容吗?我开始显示所有单击的项目,即使更改了此项目,项目的数量也保持不变。谁能解释我如何以正确的方式进行编辑?
Main.java:
public class Main extends Application {
public static int i = 0;
@Override
public void start(Stage primaryStage) throws Exception {
FXMLLoader loader = new FXMLLoader(getClass().getResource("sample.fxml"));
Parent root = loader.load();
Controller controller = loader.getController();
primaryStage.setTitle("Hello World");
ObservableList<Test> items = FXCollections.observableArrayList(e -> new Observable[]{e.nameProperty()});
Test test1 = new Test("test1");
Test test2 = new Test("test2");
Test test3 = new Test("test3");
Button button = controller.getButton();
button.setOnAction(e -> {
test1.setName("name changed" + ++i);
});
items.addAll(test1, test2, test3);
ChoiceBox choiceBox = controller.getChoiceBox();
choiceBox.setItems(items);
StringConverter<Test> converter = new StringConverter<Test>() {
@Override
public String toString(Test album) {
return album != null ? album.getName() : null;
}
@Override
public Test fromString(String string) {
return null;
}
};
choiceBox.setConverter(converter);
Scene scene = new Scene(root, 300, 275);
primaryStage.setScene(scene);
primaryStage.show();
}
public static void main(String[] args) {
launch(args);
}
}
sample.fxml:
<GridPane fx:controller="sample.Controller"
xmlns:fx="http://javafx.com/fxml" alignment="center" hgap="10" vgap="10">
<ChoiceBox fx:id="choiceBox" GridPane.columnIndex="0" GridPane.rowIndex="0"></ChoiceBox>
<Button fx:id="button" GridPane.columnIndex="0" GridPane.rowIndex="1" text="CHANGE"></Button>
</GridPane>
Controller.java:
public class Controller {
@FXML
private ChoiceBox choiceBox;
@FXML
private Button button;
public ChoiceBox getChoiceBox() {
return choiceBox;
}
public void setChoiceBox(ChoiceBox choiceBox) {
this.choiceBox = choiceBox;
}
public Button getButton() {
return button;
}
public void setButton(Button button) {
this.button = button;
}
}
Test.java:
class Test {
StringProperty name;
public Test(String name) {
setName(name);
}
public StringProperty nameProperty() {
if (name == null) name = new SimpleStringProperty(this, "name");
return name;
}
public void setName(String name) {
nameProperty().set(name);
}
public String getName() {
return nameProperty().get();
}
}