如何在SWT表标题中添加“(de)全选”复选框?

我有一个SWT表,我正在使用SWT.CHECK样式进行实例化,以便在每一行旁边显示一个复选框。 我的用户在表的标题行中请求了另一个复选框,以允许他们通过单击选择/取消选择所有行。

我看不出任何明显的方法,我只通过谷歌发现了Swing / JTable的例子。 有谁知道如何做到这一点? 我希望没有重新实现Table或退回头文件上下文菜单。

只需创建两个复选框图像。 第一个没有勾选,第二个没有勾选。现在将第一个图像添加到tableColumn标题。 之后,以第一次单击按钮的方式向tableColumn添加侦听器,应该触发table.selectALL()方法,同时将tableColumn标题图像更改为第二个。 再次单击按钮时,调用table.deSelectAll()方法并将tableColumn标头替换为第一个图像。

你可以使用这个条件:

单击复选框(图像)时,使用for循环检查是否已选中表中的任何复选框。 如果找到任何人检查然后激发table.deSelectAll()方法,否则激发table.selectAll()方法。

在表格/窗口大小调整期间,“复选框”不会有任何问题。

 tableColumn0.addListener(SWT.Selection, new Listener() { @Override public void handleEvent(Event event) { // TODO Auto-generated method stub boolean checkBoxFlag = false; for (int i = 0; i < table.getItemCount(); i++) { if (table.getItems()[i].getChecked()) { checkBoxFlag = true; } } if (checkBoxFlag) { for (int m = 0; m < table.getItemCount(); m++) { table.getItems()[m].setChecked(false); tableColumn0.setImage(new Image(Display.getCurrent(), "images/chkBox.PNG")); table.deselectAll(); } } else { for (int m = 0; m < table.getItemCount(); m++) { table.getItems()[m].setChecked(true); tableColumn0.setImage(new Image(Display.getCurrent(), "images/chkBox2.PNG")); table.selectAll(); } } } }); 

您可以使用FormLayout来允许堆叠对象,然后在表格顶部添加一个复选框,如下所示:

 FormData fd = new FormData(); fd.left = new FormAttachment(table, 5, SWT.LEFT); fd.top = new FormAttachment(table, 5, SWT.TOP); checkbox.setLayoutData(fd); checkbox.moveAbove(table); 

您可能会发现正确对齐复选框以使用table.getHeaderHeight()获取表标题行的高度很有用。

完整描述此代码:: de)选择SWT表头中的所有“复选框”


 public class TaskView extends ViewPart { public static TableItem std_item; public static List std=new ArrayList(); public static Table table; private TableColumn col_name_add; private TableColumn col_image_add; static int countcheck; static int staticno=1; static int check=0,uncheck=0; public TaskView() { setTitleImage(ResourceManager.getPluginImage("RCP_Demo", "icons/Tasksview.png")); } @Override public void createPartControl(Composite parent) { parent.setLayout(null); ////////// Table Create table = new Table(parent, SWT.BORDER | SWT.FULL_SELECTION|SWT.CHECK|SWT.CENTER); ////SWT.CHECK: Display first column check box table.addSelectionListener(new SelectionAdapter() { @Override public void widgetSelected(SelectionEvent e) { TableItem item = table.getItem(table.getSelectionIndex()); for(int col=1;col 

谢谢....