我有一个 SWT 表,它有零行和 6 列。
当我右击任意一个表头时,如何计算被点击的表列的索引?
最佳答案
我确实为 TableHeader
Menu
写了一个 CustomTable
上次右击。
此代码可帮助您在右键单击表格标题时检测 TableColumn
。但是,当列顺序更改时,此代码会中断。但是您可以修复它,比较重新排序的列顺序的索引 vs 原始列顺序。
addListener(SWT.MenuDetect, new Listener() {
@Override
public void handleEvent(Event e) {
Point pt = getShell().getDisplay().map(null, CustomTable.this, new Point(e.x, e.y));
Rectangle clientArea = CustomTable.this.getClientArea();
boolean header = clientArea.y <= pt.y && pt.y < (clientArea.y + CustomTable.this.getHeaderHeight());
//code to calculate column of Right click - START
int width = 0;
for(int i = 0; i< CustomTable.this.getColumns().length; i++){
TableColumn tc = CustomTable.this.getColumns()[i];
if(width < pt.x && pt.x < width + tc.getWidth()){
System.out.println("Right Click on " + tc.getText());
}
width += tc.getWidth();
}
//code to calculate column of Right click - END
if (header) {
if(tableMenu != null){
tableMenu.setVisible(false);
}
CustomTable.super.setMenu(headerMenu);
headerMenu.setLocation(e.x, e.y);
headerMenu.setVisible(true);
e.doit = false;
} else {
headerMenu.setVisible(false);
CustomTable.super.setMenu(tableMenu);
if(tableMenu != null){
tableMenu.setLocation(e.x, e.y);
tableMenu.setVisible(true);
}
}
}
});
关于java - 如何找到 SWT 表列的索引?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17289624/