gpt4 book ai didi

Javafx:检测对 tableView 所做的所有更改,包括表行的添加/删除和任何表行上的单元格编辑

转载 作者:行者123 更新时间:2023-11-29 10:00:32 24 4
gpt4 key购买 nike

鉴于我有一个 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 类(class)

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() ;
}
}

最佳答案

您的听众应该响应被删除的项目;如果不是,则您未显示的代码可能有问题。

要使 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:检测对 tableView 所做的所有更改,包括表行的添加/删除和任何表行上的单元格编辑,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32050483/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com