JavaFX TableView:选择整个TableColumn并获取索引 [英] JavaFX TableView: Select the whole TableColumn and get the index

查看:1933
本文介绍了JavaFX TableView:选择整个TableColumn并获取索引的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

假设我们有一个简单的Tableview,有3列(A,B,C)。每列包含一些目前不重要的数据。

Assume that we have a simple Tableview with 3 columns (A, B, C). Each Column contains some data which is not important at the moment.

我想知道是否可以只选择一个完整的列(无论用户在列中的哪个位置点击)并检索该列的ID和/或索引用户选择的列?

I wondered if it would be possible to make only a whole column selectable (no matter where the user clicks in the column) and retrieve the ID and/or index of that column selected by the user?

例如,用户点击B列区域的某处。在这种情况下,应标记整列,并返回索引2 。

For example, the user clicks somewhere in the area of column B. In that case the whole column should be marked and index 2 should be returned.

任何帮助都将受到赞赏;)

Any help would be appreciated ;)

推荐答案

你可以尝试这样的事情:

You can try something like this:

table.getSelectionModel().setSelectionMode(SelectionMode.MULTIPLE);
table.getSelectionModel().setCellSelectionEnabled(true);

table.addEventFilter(MouseEvent.MOUSE_PRESSED, (event) -> {
    if(event.isShortcutDown() || event.isShiftDown())
        event.consume();
});


table.getFocusModel().focusedCellProperty().addListener((obs, oldVal, newVal) -> {

    if(newVal.getTableColumn() != null){
        table.getSelectionModel().selectRange(0, newVal.getTableColumn(), table.getItems().size(), newVal.getTableColumn());
        System.out.println("Selected TableColumn: "+ newVal.getTableColumn().getText());
        System.out.println("Selected column index: "+ newVal.getColumn());
    }
});

table.addEventFilter(MouseEvent.MOUSE_PRESSED, (event) -> {
    if(event.isShortcutDown() || event.isShiftDown())
        event.consume();
});

此片段:


  • 设置 selectionModeProperty TableView的选择模型 SelectionMode.MULTIPLE 使 TableView 能够选择多行。

  • sets the selectionModeProperty of the selection model of the TableView to SelectionMode.MULTIPLE to make the TableView able to select more than one row.

设置 cellSelectionEnabledProperty TableView 的选择模型到 true 来制作 TableView 能够选择单元格而不是行

sets the cellSelectionEnabledProperty of the selection model of the TableView to to true to make the TableView able to select cells rather than rows

将监听器附加到 focusedCellProperty TableView 的javafx / scene / control / TableView.html#focusModelProperty--rel =nofollow noreferrer>焦点模型哪个侦听器打印<$ c当前所选单元格的$ c> TableColumn 并选择所选列中的所有单元格

attaches a listener to the focusedCellProperty of the focus model of the TableView which listener prints the TableColumn of the currently selected cell and selects all of the cell in the selected column

消耗鼠标上的事件 TableView 如果按下修饰键以禁用例如 Shift +单击选择

consumes the mouse events on the TableView if a modifier key is pressed to disable for example Shift + Click selection

这篇关于JavaFX TableView:选择整个TableColumn并获取索引的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆