I have a SWT table which has zero rows and 6 columns.
When I right click on any one of the table header, how can I calculate the index of the table column that is clicked?
I did write a CustomTable
for TableHeader
Menu
on Right click sometime back.
This code helps you in detecting the TableColumn
on right click on table header. But, this code breaks when the column order is changed. But you can fix it comparing the indices of reordered column order vs original column order.
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);
}
}
}
});
Here is some code that does what you want:
private static Map<TableColumn, Integer> mapping = new HashMap<TableColumn, Integer>();
public static void main(String[] args)
{
Display display = new Display();
final Shell shell = new Shell(display);
shell.setText("StackOverflow");
shell.setLayout(new FillLayout());
Listener listener = new Listener()
{
@Override
public void handleEvent(Event arg0)
{
TableColumn column = (TableColumn) arg0.widget;
System.out.println(mapping.get(column));
}
};
Table table = new Table(shell, SWT.NONE);
table.setHeaderVisible(true);
for(int i = 0; i < 5; i++)
{
TableColumn column = new TableColumn(table, SWT.NONE);
column.setText("Column " + i);
column.addListener(SWT.Selection, listener);
column.pack();
mapping.put(column, i);
}
shell.pack();
shell.open();
while (!shell.isDisposed())
{
if (!display.readAndDispatch())
display.sleep();
}
display.dispose();
}
It basically generates a Map
storing the TableColumn
as key and the position as the value.
Alternatively you could iterate over all the columns of the Table
within the Listener
and compare them to the clicked column. The first approach (Map) is faster but uses more memory, whereas the second approach (iteration) is slower but uses less memmory.
Similar to the above example, but without using a map to store indices:
...
column.addSelectionListener(getSelectionAdapter1(tbl.getColumnCount()-1));
...
private SelectionAdapter getSelectionAdapter1(final int index) {
SelectionAdapter selectionAdapter = new SelectionAdapter() {
@Override
public void widgetSelected(SelectionEvent e) {
System.out.println(index);
}
};
return selectionAdapter;
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With