【问题标题】:Javafx: Detect ALL changes made to tableView including addition/deletion of table rows and cell edits on any table rowsJavafx:检测对 tableView 所做的所有更改,包括添加/删除表行和任何表行上的单元格编辑
【发布时间】:2015-11-10 02:17:51
【问题描述】:

鉴于我有一个tableView,我如何跟踪所有更改,即添加/删除新行,其中一个表格单元格的值已更改,然后关闭当检测到任何这些更改时发生相同的事件?

目前,我有以下代码,如果我连续添加,它只会设法检测到更改。当我编辑任何行的表格单元格和删除一行时,它无法检测到更改。

    ObservableList<Trade> observableListOfTrades =FXCollections.observableArrayList();


    observableListOfTrades.add(newTrade);  
fxTransactionLog.getItems().add(observableListOfTrades.get(observableListOfTrades.size()-1));


        observableListOfTrades.addListener(new ListChangeListener() {
            @Override
            public void onChanged(ListChangeListener.Change change) {
                System.out.println("Detected a change! ");
            }
        });

根据要求,我发布了我的Trade 班级

public class Trade{

// properties
private ObjectProperty<LocalDate> transactionDate;
private StringProperty itemName;
private StringProperty buySell;
private DoubleProperty volume;
private DoubleProperty price;
private DoubleBinding transactionFee;


public Trade(BuySell buySell, LocalDate transactionDate, double volume, double price){
    this.buySell = new SimpleStringProperty(buySell.toString());
    this.itemName = new SimpleStringProperty("testing");
    this.transactionDate = new SimpleObjectProperty<LocalDate>(transactionDate);
    this.volume = new SimpleDoubleProperty(volume);
    this.price = new SimpleDoubleProperty(price);
}

// getters
public String getBuySell(){
    return this.buySell.get();
}

// return Property Object
public StringProperty buySellProperty(){
    return this.buySell;
}

// setters
public void setBuySell(String buySell){
    this.buySell.set(buySell);
}

public LocalDate getTransactionDate(){
    return this.transactionDate.getValue();
}

public ObjectProperty<LocalDate> transactionDateProperty(){
    return this.transactionDate;
}

public void setTransactionDate(LocalDate transactionDate){
    this.transactionDate.set(transactionDate);
}


public double getVolume(){
    return this.volume.get();
}

public DoubleProperty volumeProperty(){
    return this.volume;
}

public void setVolume(double volume){
    this.volume.set(volume);
}


public double getPrice(){
    return this.price.get();
}   

public DoubleProperty priceProperty(){
    return this.price;
}

public void setPrice(double price){
    this.price.set(price);
}

public String getItemName(){
    return this.itemName.getValue();
}


public double getTransactionFee(){
        this.transactionFee = this.price.multiply(this.volume).multiply(0.15);
        return this.transactionFee.getValue();
}

public DoubleBinding transactionFeeProperty(){
    return this.transactionFee;
}



public String toString(){
    return "Buy: " + getBuySell() + ", Transasction date: " + getTransactionDate() + ", Volume: " + getVolume() + ", Price: " + getPrice() + ", Transaction fee: " + getTransactionFee() ;
}
}

【问题讨论】:

  • 您的Trade 类是否使用JavaFX 属性? (您可以发布该课程的代码吗?)
  • 是的。 Trade 类包含使用 JavaFX 属性的变量。例如,private ObjectProperty&lt;LocalDate&gt; transactionDate; private StringProperty itemName; private StringProperty buySell; private DoubleProperty volume; private DoubleProperty price;
  • 简短的回答是使用提取器创建您的“ObservableList”。当我回到电脑上时会发布一个真正的答案。
  • 非常感谢!期待看到你的答案!

标签: java javafx


【解决方案1】:

您的听众应该对被删除的项目做出响应;如果不是,那么您没有显示的代码可能有问题。

要让ListChangeListener 响应对属于列表元素的属性的更新,您需要创建指定extractor 的列表。

提取器是一个函数,它接受列表的一个元素,并返回一个可观察值的数组。然后列表会观察该数组中的所有值,如果它们发生变化,则会向列表的侦听器触发更新事件。

因此,如果您希望在 任何 属性发生更改时通知您的侦听器,您可以这样做

ObservableList<Trade> observableListOfTrades =FXCollections.observableArrayList(trade ->
    new Observable[] {
        trade.transactionDateProperty(),
        trade.itemNameProperty(),
        trade.buySellProperty(),
        trade.volumeProperty(),
        trade.priceProperty().
        trade.transactionFeeProperty()
    });

这是一个完整的示例,使用通常的“联系人表”示例:

import java.util.function.Function;

import javafx.application.Application;
import javafx.beans.Observable;
import javafx.beans.binding.Bindings;
import javafx.beans.property.SimpleStringProperty;
import javafx.beans.property.StringProperty;
import javafx.beans.value.ObservableValue;
import javafx.collections.FXCollections;
import javafx.collections.ListChangeListener.Change;
import javafx.collections.ObservableList;
import javafx.geometry.HPos;
import javafx.geometry.Pos;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.Label;
import javafx.scene.control.TableColumn;
import javafx.scene.control.TableView;
import javafx.scene.control.TextField;
import javafx.scene.control.cell.TextFieldTableCell;
import javafx.scene.layout.ColumnConstraints;
import javafx.scene.layout.GridPane;
import javafx.scene.layout.Priority;
import javafx.scene.layout.VBox;
import javafx.stage.Stage;

public class TableViewWithUpdateListenerExample extends Application {

    @Override
    public void start(Stage primaryStage) {

        // Create an observable list with an extractor. This will ensure
        // listeners on the list receive notifications if any of the
        // properties returned by the extractor belonging to a list element
        // are changed:

        ObservableList<Person> data = FXCollections.observableArrayList(person ->
            new Observable[] {
                    person.firstNameProperty(),
                    person.lastNameProperty(),
                    person.emailProperty()
            });

        data.addListener((Change<? extends Person> c) -> {
           while (c.next()) {
               if (c.wasAdded()) {
                   System.out.println("Added:");
                   c.getAddedSubList().forEach(System.out::println);
                   System.out.println();
               }
               if (c.wasRemoved()) {
                   System.out.println("Removed:");
                   c.getRemoved().forEach(System.out::println);
                   System.out.println();
               }
               if (c.wasUpdated()) {
                   System.out.println("Updated:");
                   data.subList(c.getFrom(), c.getTo()).forEach(System.out::println);
                   System.out.println();
               }
           }
        });

        data.addAll(
                new Person("Jacob", "Smith", "jacob.smith@example.com"),
                new Person("Isabella", "Johnson", "isabella.johnson@example.com"),
                new Person("Ethan", "Williams", "ethan.williams@example.com"),
                new Person("Emma", "Jones", "emma.jones@example.com"),
                new Person("Michael", "Brown", "michael.brown@example.com")
        );

        TableView<Person> tableView = new TableView<>();
        tableView.setEditable(true);
        tableView.setItems(data);

        tableView.getColumns().add(column("First Name", Person::firstNameProperty));
        tableView.getColumns().add(column("Last Name", Person::lastNameProperty));
        tableView.getColumns().add(column("Email", Person::emailProperty));

        TextField firstNameTF = new TextField();
        TextField lastNameTF = new TextField();
        TextField emailTF = new TextField();
        Button addButton = new Button("Add");
        addButton.setOnAction(e -> {
            Person person = new Person(firstNameTF.getText(), lastNameTF.getText(), emailTF.getText());
            firstNameTF.setText("");
            lastNameTF.setText("");
            emailTF.setText("");
            data.add(person);
        });

        GridPane editPane = new GridPane();
        editPane.addRow(0,  new Label("First Name:"), firstNameTF);
        editPane.addRow(1,  new Label("Last Name:"), lastNameTF);
        editPane.addRow(2,  new Label("Email:"), emailTF);
        editPane.add(addButton, 0, 3, 2, 1);
        ColumnConstraints leftCol = new ColumnConstraints();
        leftCol.setHalignment(HPos.RIGHT);
        leftCol.setHgrow(Priority.NEVER);
        editPane.setHgap(10);
        editPane.setVgap(5);
        editPane.getColumnConstraints().addAll(leftCol, new ColumnConstraints());

        Button deleteButton = new Button("Delete");
        deleteButton.setOnAction(e -> data.remove(tableView.getSelectionModel().getSelectedItem()));
        deleteButton.disableProperty().bind(Bindings.isEmpty(tableView.getSelectionModel().getSelectedItems()));

        VBox root = new VBox(10, tableView, editPane, deleteButton);
        root.setAlignment(Pos.CENTER);

        primaryStage.setScene(new Scene(root, 800, 600));
        primaryStage.show();
    }

    private TableColumn<Person, String> column(String title, Function<Person, ObservableValue<String>> property) {
        TableColumn<Person, String> col = new TableColumn<>(title);
        col.setCellValueFactory(cellData -> property.apply(cellData.getValue()));
        col.setCellFactory(TextFieldTableCell.forTableColumn());
        return col ;
    }

    public static class Person {
        private final StringProperty firstName = new SimpleStringProperty() ;
        private final StringProperty lastName = new SimpleStringProperty() ;
        private final StringProperty email = new SimpleStringProperty() ;

        public Person(String firstName, String lastName, String email) {
            setFirstName(firstName);
            setLastName(lastName);
            setEmail(email);
        }

        public final StringProperty firstNameProperty() {
            return this.firstName;
        }

        public final java.lang.String getFirstName() {
            return this.firstNameProperty().get();
        }

        public final void setFirstName(final java.lang.String firstName) {
            this.firstNameProperty().set(firstName);
        }

        public final StringProperty lastNameProperty() {
            return this.lastName;
        }

        public final java.lang.String getLastName() {
            return this.lastNameProperty().get();
        }

        public final void setLastName(final java.lang.String lastName) {
            this.lastNameProperty().set(lastName);
        }

        public final StringProperty emailProperty() {
            return this.email;
        }

        public final java.lang.String getEmail() {
            return this.emailProperty().get();
        }

        public final void setEmail(final java.lang.String email) {
            this.emailProperty().set(email);
        }

        @Override
        public String toString() {
            return getFirstName() + " " + getLastName() + " (" + getEmail() +")";
        }

    }

    public static void main(String[] args) {
        launch(args);
    }
}

【讨论】:

  • 谢谢!很好的答案!我还是 javafx 的新手,我有几个问题,1. ? 在 ? extends Person 中是什么意思? 2.deleteButton.disableProperty().bind(Bindings.isEmpty(tableView.getSelectionModel().getSelectedItems()));是什么意思? 3.cellData -&gt; property.apply(cellData.getValue()) 是做什么的,那段代码到底在做什么?
  • 这些与您的原始问题无关。 1) ? extends Person 是 bounded wildcard。 2)选择选择为空时按钮。请参阅 Javadocs here 和 here 3) 为列设置单元格值工厂。见Javadocs
猜你喜欢
  • 2014-02-02
  • 2020-11-18
  • 1970-01-01
  • 1970-01-01
  • 2018-10-11
  • 1970-01-01
  • 1970-01-01
  • 2012-09-12
  • 2014-02-22
相关资源
最近更新 更多