我有这个项目:
public class Person {
private SimpleStringProperty name;
public SimpleStringProperty nameProperty(){
if(this.name==null){
this.name=new SimpleStringProperty();
}
return this.name;
}
public final void setName(String value){
nameProperty().set(value);
}
public String getName(){
return nameProperty().get();
}
public Person(String value){
setName(value);
}
public String toString(){
return this.getName();
}
}
和一张桌子:
TableColumn nomePersona = new TableColumn("NOME")
nomePersona.setCellValueFactory(new PropertyValueFactory<Person,String>("name"));
nomePersona.setCellFactory(TextFieldTableCell.forTableColumn());
table.getColumns().setAll(nomePersona);
lista = FXCollections.<Person>observableArrayList();
lista.addAll(new Person("uno"),new Person("due"),new Person("tre"));
table.setItems(lista);
我还有一个按钮,而不是添加一个项目:
private void addPerson(ActionEvent event) {
Person p = new Person(text.getText());
lista.add(0, p);
}
另一个修改第一行的文本:
private void modifyPerson(ActionEvent event) {
lista.get(0).setName(TEST_TF.getText());
// lista.remove(0);
// lista.add(0, new Person(TEST_TF.getText()));
}
如果我按下 modifyPerson 按钮更新文本,我可以在第一行看到修改,但如果在我添加新项目之前按下 addPerson 按钮,表格不再刷新。我打印了可观察列表,并且该项目已正确更新,但单元格保持旧值。获得修改的唯一方法是删除并添加一个新项目(请参阅 modifyPerson() 上的 2 个注释行
提前致谢。
这里是完整的代码: 文件 Person.java
package table;
import javafx.beans.property.SimpleStringProperty;
public class Person {
private SimpleStringProperty name;
public SimpleStringProperty nameProperty(){
if(this.name==null){
this.name=new SimpleStringProperty();
}
return this.name;
}
public final void setName(String value){
nameProperty().set(value);
}
public String getName(){
return nameProperty().get();
}
public Person(String value){
setName(value);
}
public String toString(){
return this.getName();
}
}
文件FXMLDocumentController.java
package table;
import java.net.URL;
import java.util.ResourceBundle;
import javafx.beans.InvalidationListener;
import javafx.beans.Observable;
import javafx.collections.FXCollections;
import javafx.collections.ListChangeListener;
import javafx.collections.ObservableList;
import javafx.event.ActionEvent;
import javafx.fxml.FXML;
import javafx.fxml.Initializable;
import javafx.scene.control.Button;
import javafx.scene.control.ComboBox;
import javafx.scene.control.TableColumn;
import javafx.scene.control.TableView;
import javafx.scene.control.TextField;
import javafx.scene.control.cell.PropertyValueFactory;
import javafx.scene.control.cell.TextFieldTableCell;
public class FXMLDocumentController implements Initializable {
private ObservableList<Person> lista;
@FXML
private Button btn_add;
@FXML
private ComboBox<Person> combo;
@FXML
private TextField tfld_textAdd;
@FXML
private TableView<Person> tbl_table;
@FXML
private Button btn_upd;
@FXML
private TextField tfld_textUpdate;
@FXML
private Button btn_del;
@FXML
private void act_addItem(ActionEvent event) {
Person p = new Person(tfld_textAdd.getText());
lista.add(0, p);
System.out.println(lista.toString());
}
@FXML
private void act_updItem(ActionEvent event) {
lista.get(0).setName(tfld_textUpdate.getText());
// lista.remove(0);
// lista.add(0, new Person(tfld_textUpdate.getText()));
System.out.println(lista.toString());
}
@FXML
private void act_delItem(ActionEvent event) {
lista.remove(lista.size()-1);
System.out.println(lista.toString());
}
public void initialize(URL url, ResourceBundle rb) {
TableColumn nomePersona = new TableColumn("NOME");
nomePersona.setCellValueFactory(new PropertyValueFactory<Person,String>("name"));
nomePersona.setCellFactory(TextFieldTableCell.forTableColumn());
tbl_table.getColumns().setAll(nomePersona);
lista = FXCollections.<Person>observableArrayList();
lista.addAll(new Person("uno"),new Person("due"),new Person("tre"));
tbl_table.setItems(lista);
combo.setItems(lista);
lista.addListener(new InvalidationListener() {
public void invalidated(Observable observable) {
System.out.println("INVALIDATION");
System.out.println(observable.toString());
}
});
lista.addListener(new ListChangeListener<Person>() {
public void onChanged(ListChangeListener.Change<? extends Person> c) {
System.out.println("LIST CHANGER");
while (c.next()) {
if (c.wasPermutated()) {
for (int i = c.getFrom(); i < c.getTo(); ++i) {
System.out.println("Permutato");
}
} else if (c.wasUpdated()) {
System.out.println("Updatato: ");
} else {
for (Person remitem : c.getRemoved()) {
System.out.println("rimosso: " + remitem.getName());
}
for (Person additem : c.getAddedSubList()) {
System.out.println("addizionato: " + additem.getName());
}
}
}
}
});
}
}
这里的文件Table.java
package table;
import javafx.application.Application;
import javafx.fxml.FXMLLoader;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.stage.Stage;
public class Table extends Application {
public void start(Stage stage) throws Exception {
Parent root = FXMLLoader.load(getClass().getResource("FXMLDocument.fxml"));
Scene scene = new Scene(root);
stage.setScene(scene);
stage.show();
}
public static void main(String[] args) {
launch(args);
}
}
并且比 FXML FXMLDocumen.fxml
<?xml version="1.0" encoding="UTF-8"?>
<?import java.lang.*?>
<?import javafx.scene.control.*?>
<?import javafx.scene.layout.*?>
<AnchorPane id="AnchorPane" prefHeight="409.0" prefWidth="463.0" xmlns="http://javafx.com/javafx/8" xmlns:fx="http://javafx.com/fxml/1" fx:controller="table.FXMLDocumentController">
<children>
<Button fx:id="btn_add" layoutX="175.0" layoutY="10.0" onAction="#act_addItem" prefHeight="26.0" prefWidth="114.0" text="ADD ITEM 0" /><ComboBox fx:id="combo" layoutX="11.0" layoutY="94.0" prefHeight="26.0" prefWidth="200.0" /><TextField fx:id="tfld_textAdd" layoutX="14.0" layoutY="14.0" AnchorPane.leftAnchor="10.0" AnchorPane.topAnchor="10.0" /><TableView fx:id="tbl_table" layoutX="14.0" layoutY="123.0" prefHeight="275.0" prefWidth="200.0" AnchorPane.bottomAnchor="10.0" AnchorPane.leftAnchor="10.0" AnchorPane.topAnchor="124.0" /><Button fx:id="btn_upd" layoutX="232.0" layoutY="124.0" mnemonicParsing="false" onAction="#act_updItem" prefHeight="26.0" prefWidth="221.0" text="REWRITE ITEM 0" AnchorPane.leftAnchor="232.0" /><TextField fx:id="tfld_textUpdate" layoutX="232.0" layoutY="94.0" prefHeight="26.0" prefWidth="221.0" text="aaa" AnchorPane.leftAnchor="232.0" /><Button fx:id="btn_del" layoutX="298.0" layoutY="10.0" mnemonicParsing="false" onAction="#act_delItem" prefHeight="26.0" prefWidth="114.0" text="DEL ITEM 0" />
</children>
</AnchorPane>