从相应的列标题节点获取javafx TableColumn [英] Get javafx TableColumn from corresponding column-header Node
问题描述
我正在替换表列的javafx上下文菜单。上下文菜单将为用户提供excel的访问权限,例如过滤表视图。
一切正常,但我现在不想在列标题上的任何鼠标点击事件上打开上下文菜单。不仅仅是标准的右键单击。
I'm replacing the javafx context menus of a table's columns. The context menu will give the user access to excel like filtering of the table view. Everything is working, but i now wan't to open the context menu on any mouse click event on the column header. Not just the standard right click.
问题是,您只能通过父表视图中的lookupAll()获取标题节点。
由于事实,只有在表视图呈现后才能进行查找,我使用Platform.runLater(..)调用此方法。
The problem hereby is, that you can only get the header node by a lookupAll() inside the parent tableview. Due to the fact, that a lookup is only possible after the table view has been rendered, i call this method with a Platform.runLater(..).
TableView类:
TableView Class:
private void setHeaderNodesEventHandler() {
int i = 0;
for (final Node headerNode : lookupAll(".column-header")) {
((AbstractFilterableTableColumn<E, ?>) getColumns().get(i)).setHeaderNodeEventHandler(headerNode);
i++;
}
}
TableColumn类:
TableColumn Class:
public void setHeaderNodeEventHandler(final Node headerNode) {
headerNode.addEventHandler(MouseEvent.MOUSE_CLICKED, new EventHandler<MouseEvent>(){
@Override
public void handle(final MouseEvent e) {
contextMenu.show(headerNode, e.getScreenX(), e.getScreenY());
}
});
}
这里的问题是,我必须相信,找到的顺序headerNodes与列相同。我真的不需要在这里发生不好的事情。
Problem here is, that i have to believe, that the order of the found headerNodes is the same as the columns. And i really don't need something bad happen here.
我宁愿有类似的东西(见下文),这是不可能的,因为我不能从Parent转换为我的AbstractFilterableTableColumn。
I would rather have something like that (see below), which is not possible, because i'm not able to cast from Parent to my AbstractFilterableTableColumn.
private void setHeaderNodesEventHandler() {
for (final Node headerNode : lookupAll(".column-header")) {
final AbstractFilterableTableColumn<E, ?> column = (AbstractFilterableTableColumn<E, ?>) headerNode.getParent();
column.setHeaderNodeEventHandler(headerNode);
}
}
您有什么想法获取headerNodes及其相应的表格列而不信任订购?
Do you have any ideas to get the headerNodes and their respective table column without trusting the ordering?
所以,如果有人有我的问题的解决方案..我会非常感激。 :)
So if anyone has a solution for my problem.. i would really appreciate it. :)
推荐答案
创建列时,使用标签
作为图形而不是设置文本。然后,您可以使用 Label
注册所需的鼠标监听器。我没有对此进行测试,但以下几行中的内容应该有效:
When you create your columns, use a Label
as a graphic instead of setting the text. Then you can register the mouse listener you need with the Label
. I haven't tested this, but something along the following lines should work:
TableColumn<S, T> column = new TableColumn<S, T>();
Label columnHeader = new Label("Column Name");
column.setGraphic(columnHeader);
// note you can pass your TableColumn to the setHeaderNodeEventHandler(...)
// method too, so you can access the actual column represented if you need
setHeaderNodeEventHandler(columnHeader);
这篇关于从相应的列标题节点获取javafx TableColumn的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!