SWT表行mousehandler和复选框检测

时间:2014-04-03 17:34:11

标签: java swt

点击一行时,会弹出一个窗口。

单击第一列中的单元格时没有任何反应,因为复选框位于第一列,而mouselistener检测到第一列中的单元格。

但是选中该复选框后,窗口会再次弹出。

如何在选中复选框时阻止窗口弹出?

public class Testtable {
static Display display;
static Shell shell;
static Font small_font;

public Testtable(){}
public static void main(String args[]){
display = new Display();
small_font = new Font(Display.getDefault(), Display.getDefault().getSystemFont().getFontData() );
shell = new Shell(display, SWT.BORDER|SWT.PRIMARY_MODAL|SWT.RESIZE);
            shell.setText(" Test table ");
            shell.setLayout(new GridLayout(1, true));
            final Table table = new Table(shell, SWT.BORDER | SWT.CHECK | SWT.V_SCROLL | SWT.FULL_SELECTION);
            table.setHeaderVisible(true);
            table.setFont(small_font);
            GridData griddata = new GridData(SWT.FILL, SWT.FILL,false,false);
                        griddata.horizontalSpan=5;
                        griddata.heightHint = table.getItemHeight()*15;
                        table.setLayoutData(griddata);
                        TableColumn checkbox = new TableColumn(table,SWT.NONE,0);
                        TableColumn column_one = new TableColumn(table,SWT.NONE,1);
                        column_one.setText("column one");
                        TableColumn column_two = new TableColumn(table,SWT.NONE,2);
                        column_two.setText("column_two");
                        TableColumn column_three = new TableColumn(table,SWT.NONE,3);
                        column_three.setText("column_three");
                        TableColumn column_four = new TableColumn(table,SWT.NONE,4);
                        column_four.setText("column_four");
                        for(int i=0;i<10;i++){
                            TableItem item=new TableItem(table,SWT.NONE);
                                item.setText(new String [] {"column 2 item-"+i,"column 3 item-"+i,"column 4 item-"+i});
                        }
                        Label labelfiller=new Label(shell, SWT.SHADOW_OUT); /* Filler */
                        labelfiller.setLayoutData(new GridData(GridData.HORIZONTAL_ALIGN_FILL | GridData.VERTICAL_ALIGN_BEGINNING,0, true,true,4,2));
                        Button close = new Button(shell, SWT.PUSH);
                            close.setText("Done");
                            shell.setDefaultButton(close);
                            close.setLayoutData(new GridData(GridData.HORIZONTAL_ALIGN_FILL));
                            close.addSelectionListener(new SelectionAdapter() {
                                @Override
                                public void widgetSelected(SelectionEvent e) {                  
                                        shell.dispose();
                                      }
                                    });
                            for(int i=0;i<table.getColumnCount();i++){
                                table.getColumn(i).pack();
                                }   
                            table.addListener(SWT.MouseUp,new Listener(){
                                public void handleEvent(Event e){                                   
                                        if(table.getItem(new Point(e.x, e.y)).getBounds(0).contains(new Point(e.x, e.y))){
                                            //if((e.widget.getStyle()&32) == SWT.CHECK){
                                        System.out.println("returned cause selected column 0, wich is checkbox column.");
                                        return;
                                    }//}
                                    showFrame();
                                }
                            });
                            table.pack();
                            shell.pack();
                            shell.open();
                            while (!shell.isDisposed()) {
                              if (!shell.getDisplay().readAndDispatch())
                                  shell.getDisplay().sleep();
                            }   
            }
static void showFrame(){
    final Shell dialog = new Shell(shell,SWT.BORDER|SWT.PRIMARY_MODAL|SWT.RESIZE);
    dialog.setText("Test shell.. ");
    dialog.setLayout(new GridLayout(3, true));
    Label label = new Label(dialog,SWT.NONE);
    label.setText("Row clicked: ");
    Button ok = new Button(dialog, SWT.PUSH);
    ok.setText("Close");
    dialog.setDefaultButton(ok);
    ok.setLayoutData(new GridData(GridData.HORIZONTAL_ALIGN_FILL));
    ok.addSelectionListener(new SelectionAdapter() {
        @Override
        public void widgetSelected(SelectionEvent e) {
                dialog.dispose();
              }
            });
    dialog.pack();
    dialog.open();
    while (!dialog.isDisposed()) {
      if (!dialog.getDisplay().readAndDispatch())
            dialog.getDisplay().sleep();
                }   
        }

}

1 个答案:

答案 0 :(得分:1)

好的,找到了解决方案。

问题是,没有办法(我知道)会返回复选框的界限。但是,由于复选框始终是表格中的第一件事(如果您使用带有Table的{​​{1}}),您只需检查点击事件是否位于第一个单元格的左侧。 / p>

我冒昧地“优化”了你的其他一些代码(我的主观意见):

SWT.CHECK