gpt4 book ai didi

JavaFX 从第二列中选择文本与单击行中的位置无关

转载 作者:行者123 更新时间:2023-11-30 06:53:31 25 4
gpt4 key购买 nike

我有一个小问题,我需要在行中单击的位置独立地选择行的一列(单元格)。

例如我有 3 列表,我点击第一列但我每次都需要第二列的文本。

我有什么办法可以做到这一点。在 swing 中有行和列的索引,但我在 JavaFX 中找不到类似的东西。

我的双击选择代码是:

@Override
public void handle(MouseEvent click) {
if (click.getClickCount() == 2) {
@SuppressWarnings("rawtypes")
TablePosition pos = searchResult.getSelectionModel().getSelectedCells().get(0);
int row = pos.getRow();
int col = pos.getColumn();
@SuppressWarnings("rawtypes")
TableColumn column = pos.getTableColumn();
String val = column.getCellData(row).toString();
System.out.println("Selected Value, " + val + ", Column: " + col + ", Row: " + row);
try {
if (button1.isSelected()) {
Runtime.getRuntime().exec("explorer.exe /select," + val);
} else {
if (button2.isSelected()) {
File file = new File(val);
Desktop.getDesktop().open(file);
}
}
} catch (IOException e) {
e.printStackTrace();
}

}
}

感谢您的任何回答。

最佳答案

如果你真的想选择单元格

您可以设置 cellSelectionEnabledProperty of the selection model of your table viewtrue

你可以听到focused cell property of the focus model of your table view的变化.

然后在更改监听器中,您可以选择任何 TableColumn你想要的在同一行。

示例:

// Enable cell selection
tableView.getSelectionModel().setCellSelectionEnabled(true);

// On focused property change
tableView.getFocusModel().focusedCellProperty().addListener(new ChangeListener<TablePosition>() {

@Override
public void changed(ObservableValue<? extends TablePosition> arg0, TablePosition arg1, TablePosition arg2) {
// Select TableColumn "col2" in the same row
tableView.getSelectionModel().select(arg2.getRow(), col2);

}
});

这将生成一个表格 View ,该表格 View 独立于行中选择的单元格选择所需列中的单元格。

如果您只想获取行选择中单元格的值

// Just row selection
tableView.getSelectionModel().setCellSelectionEnabled(false);

tableView.getFocusModel().focusedCellProperty().addListener(new ChangeListener<TablePosition>() {

@Override
public void changed(ObservableValue<? extends TablePosition> arg0, TablePosition arg1, TablePosition arg2) {
// Get the data from the desired cell (tableview displays Data objects)
Data item = tableView.getItems().get(arg2.getRow());
String data = (String) col2.getCellObservableValue(item).getValue();
System.out.println(data);
}
});

此示例独立于所选列从所需列获取并打印值。

关于JavaFX 从第二列中选择文本与单击行中的位置无关,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37459586/

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