gpt4 book ai didi

java - 如何为 TableView 中的所有单元格添加事件处理程序,而不为每列创建新的单元格工厂?

转载 作者:行者123 更新时间:2023-12-02 05:59:53 24 4
gpt4 key购买 nike

我试图允许用户在 TableView 中的任何单元格上单击鼠标中键并将其文本复制到系统剪贴板。但是,我不知道如何循环遍历 TableView 中的所有单元格

有没有办法获取行或列中所有TableCellsList,以便我可以应用setOnMouseClicked()方法他们?

我知道这可以通过 CellFactory 轻松完成;但我已经将一些自定义工厂应用于某些列,并且我需要避免覆盖这些工厂或为其余列创建 20 多个单元工厂。

这是一个 MCVE:

import javafx.application.Application;
import javafx.beans.property.DoubleProperty;
import javafx.beans.property.SimpleDoubleProperty;
import javafx.beans.property.SimpleStringProperty;
import javafx.beans.property.StringProperty;
import javafx.geometry.Insets;
import javafx.geometry.Pos;
import javafx.scene.Scene;
import javafx.scene.control.TableCell;
import javafx.scene.control.TableColumn;
import javafx.scene.control.TableView;
import javafx.scene.input.MouseButton;
import javafx.scene.layout.VBox;
import javafx.stage.Stage;

import java.text.NumberFormat;

public class TableCellClickListener extends Application {

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

@Override
public void start(Stage primaryStage) {

// Simple Interface
VBox root = new VBox(10);
root.setAlignment(Pos.CENTER);
root.setPadding(new Insets(10));

TableView<Person> tableView = new TableView<>();

TableColumn<Person, String> colName = new TableColumn<>("Name");
colName.setCellValueFactory(tf -> tf.getValue().nameProperty());

TableColumn<Person, Number> colBalance = new TableColumn<>("Balance");
colBalance.setCellValueFactory(tf -> tf.getValue().balanceProperty());

tableView.getColumns().addAll(colName, colBalance);

// Format the balance column
colBalance.setCellFactory(t -> new TableCell<Person, Number>() {
@Override
protected void updateItem(Number item, boolean empty) {
super.updateItem(item, empty);
if (empty) {
setText(null);
} else {
setText(NumberFormat.getCurrencyInstance().format(item));
}
}
});

// This is where I am stuck; is there such a list of cells in the API?
for (TableCell cell : listOfCells) {
setCopyListener(cell);
}

// Show the stage
primaryStage.setScene(new Scene(root));
primaryStage.setTitle("Sample");
primaryStage.show();
}

private void setCopyListener(TableCell cell) {
cell.setOnMouseClicked(event -> {
if (!cell.isEmpty() && event.getButton().equals(MouseButton.MIDDLE)) {
System.out.println(cell.getText());
}
});
}
}

class Person {

private final StringProperty name = new SimpleStringProperty();
private final DoubleProperty balance = new SimpleDoubleProperty();

public Person(String name, double balance) {
this.name.set(name);
this.balance.set(balance);
}

public String getName() {
return name.get();
}

public void setName(String name) {
this.name.set(name);
}

public StringProperty nameProperty() {
return name;
}

public double getBalance() {
return balance.get();
}

public void setBalance(double balance) {
this.balance.set(balance);
}

public DoubleProperty balanceProperty() {
return balance;
}
}
<小时/>

我已经准备好了 setCopyListener(TableCell cell) 方法,但我无法找到一种方法来循环遍历所有单元格,而每列都没有 CellFactory 。我浏览了 Java 文档,但找不到访问行或列中所有单元格列表的方法。

我错过了吗?

最佳答案

单元格由外观创建,在第一次布局之前它们将不可用。此外,调整 TableView 的大小可能会导致创建额外的单元格。您可以为 TableView 创建自定义外观,也可以使用 MouseEvent 提供的 pickResult 属性,其中包含鼠标下的节点优势。

注意:pickResult 中包含的节点可能是 TableCell 的后代,也可能根本不是单元格的一部分(例如列标题或滚动条)。您需要遍历节点层次结构,直到找到单元格或 TableView

tableView.addEventHandler(MouseEvent.MOUSE_CLICKED, evt -> {
if (evt.getButton() == MouseButton.MIDDLE) {
TableCell<Person, ?> cell = findCell(evt, tableView);
if (cell != null && !cell.isEmpty()) {
System.out.println(cell.getText());
evt.comsume();
}
}
});
private static <T> TableCell<T, ?> findCell(MouseEvent event, TableView<T> table) {
Node node = event.getPickResult().getIntersectedNode();

// go up in node hierarchy until a cell is found or we can be sure no cell was clicked
while (node != table && !(node instanceof TableCell)) {
node = node.getParent();
}
return node instanceof TableCell ? (TableCell<T, ?>) node : null;
}

关于java - 如何为 TableView 中的所有单元格添加事件处理程序,而不为每列创建新的单元格工厂?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55984169/

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