而不是使用TableColumn#getWidth()
你應該TableItem#getBounds()
工作,以確定列你在
下面是一個例子:
public static void main(String[] args)
{
Display display = new Display();
Shell shell = new Shell(display);
shell.setText("StackOverflow");
shell.setLayout(new FillLayout());
ScrolledComposite comp = new ScrolledComposite(shell, SWT.V_SCROLL | SWT.H_SCROLL | SWT.BORDER);
comp.setLayout(new FillLayout());
final Table table = new Table(comp, SWT.BORDER | SWT.FULL_SELECTION);
table.setHeaderVisible(true);
table.setLinesVisible(true);
for (int i = 0; i < 4; i++)
{
TableColumn column = new TableColumn(table, SWT.NONE);
column.setText("Column " + i);
}
for (int i = 0; i < 64; i++)
{
TableItem item = new TableItem(table, SWT.NONE);
for (int j = 0; j < table.getColumnCount(); j++)
{
item.setText(j, "Item " + i + "-" + j);
}
}
for (int i = 0; i < table.getColumnCount(); i++)
{
table.getColumn(i).pack();
}
table.addListener(SWT.MouseDown, new Listener()
{
public void handleEvent(Event event)
{
Point pt = new Point(event.x, event.y);
TableItem item = table.getItem(pt);
if (item == null)
return;
for (int i = 0; i < table.getColumnCount(); i++)
{
Rectangle rect = item.getBounds(i);
if (rect.contains(pt))
{
int index = table.indexOf(item);
System.out.println("Item " + index + "-" + i);
}
}
}
});
comp.setContent(table);
comp.setExpandHorizontal(true);
comp.setExpandVertical(true);
comp.setMinSize(table.computeSize(SWT.DEFAULT, SWT.DEFAULT));
shell.pack();
shell.setSize(400, 600);
shell.open();
while (!shell.isDisposed())
{
if (!display.readAndDispatch())
display.sleep();
}
display.dispose();
}
它將顯示選定的表格單元格(行和列)。
UPDATE
如果你想監聽的表格標題點擊事件,更換一次for
環路與此:
for (int i = 0; i < 4; i++)
{
final TableColumn column = new TableColumn(table, SWT.NONE);
column.setText("Column " + i);
column.addListener(SWT.Selection, new Listener()
{
@Override
public void handleEvent(Event arg0)
{
System.out.println(column.getText());
}
});
}
更新2
檢測在Table
標題上點擊右鍵會有點棘手。有一個bug報告here說SWT.MenuDetect
不能TableColumn
小號
但是可以使用,有一種變通方法:
public static void main(String[] args)
{
final Display display = new Display();
Shell shell = new Shell(display);
shell.setText("StackOverflow");
shell.setLayout(new FillLayout());
ScrolledComposite comp = new ScrolledComposite(shell, SWT.V_SCROLL | SWT.H_SCROLL | SWT.BORDER);
comp.setLayout(new FillLayout());
final Table table = new Table(comp, SWT.BORDER | SWT.FULL_SELECTION);
table.setHeaderVisible(true);
table.setLinesVisible(true);
for (int i = 0; i < 4; i++)
{
final TableColumn column = new TableColumn(table, SWT.NONE);
column.setText("Column " + i);
}
for (int i = 0; i < 64; i++)
{
TableItem item = new TableItem(table, SWT.NONE);
for (int j = 0; j < table.getColumnCount(); j++)
{
item.setText(j, "Item " + i + "-" + j);
}
}
for (int i = 0; i < table.getColumnCount(); i++)
{
table.getColumn(i).pack();
}
table.addListener(SWT.MenuDetect, new Listener()
{
@Override
public void handleEvent(Event event)
{
Point pt = display.map(null, table, new Point(event.x, event.y));
Rectangle clientArea = table.getClientArea();
boolean header = clientArea.y <= pt.y && pt.y < (clientArea.y + table.getHeaderHeight());
if (header)
{
TableItem item = table.getItem(0);
for (int i = 0; i < table.getColumnCount(); i++)
{
Rectangle rect = item.getBounds(i);
if (pt.x >= rect.x && pt.x <= rect.x + rect.width)
{
System.out.println("Header " + i);
}
}
}
}
});
comp.setContent(table);
comp.setExpandHorizontal(true);
comp.setExpandVertical(true);
comp.setMinSize(table.computeSize(SWT.DEFAULT, SWT.DEFAULT));
shell.pack();
shell.setSize(400, 600);
shell.open();
while (!shell.isDisposed())
{
if (!display.readAndDispatch())
display.sleep();
}
display.dispose();
}
來源
2014-03-05 12:07:41
Baz
你這樣做是爲了顯示上下文(右鍵)菜單? –
否。顯示上下文菜單。在這裏,你可以激活一個列。這個列表應該在執行了右鍵單擊之後添加。如上所述,這在沒有任何滾動的情況下起作用 – Mirco