Java SWT表格行鼠标手柄和复选框检测

Java SWT表格行鼠标手柄和复选框检测,java,swt,Java,Swt,单击一行时,会弹出一个窗口 单击第一列中的单元格时,不会发生任何情况,因为复选框位于第一列中,并且鼠标侦听器检测到第一列中的单元格 但是选中复选框,窗口将再次弹出 如何防止选中复选框时弹出窗口 public class Testtable { static Display display; static Shell shell; static Font small_font; public Testtable(){} public static void main(String args[])

单击一行时,会弹出一个窗口

单击第一列中的单元格时,不会发生任何情况,因为复选框位于第一列中,并且鼠标侦听器检测到第一列中的单元格

但是选中复选框,窗口将再次弹出

如何防止选中复选框时弹出窗口

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();
                }   
        }
公共类测试表{
静态显示;
静态壳体;
静态字体小字体;
公共测试表(){}
公共静态void main(字符串参数[]){
显示=新显示();
小字体=新字体(Display.getDefault(),Display.getDefault().getSystemFont().getFontData());
外壳=新外壳(显示,SWT.BORDER | SWT.PRIMARY | SWT.RESIZE);
shell.setText(“测试表”);
setLayout(新的GridLayout(1,true));
最终表格=新表格(shell,SWT.BORDER | SWT.CHECK | SWT.V_SCROLL | SWT.FULL_SELECTION);
表.setheadervible(true);
表.setFont(小字体);
GridData GridData=新的GridData(SWT.FILL,SWT.FILL,false,false);
griddata.horizontalSpan=5;
griddata.heightHint=table.getItemHeight()*15;
表.setLayoutData(griddata);
TableColumn复选框=新建TableColumn(表,SWT.NONE,0);
TableColumn column_one=新的TableColumn(表,SWT.NONE,1);
第1列。setText(“第1列”);
TableColumn column_two=新的TableColumn(表,SWT.NONE,2);
column_two.setText(“column_two”);
TableColumn column_three=新的TableColumn(表,SWT.NONE,3);
第三列。setText(“第三列”);
TableColumn column_four=新的TableColumn(表,SWT.NONE,4);
列四.setText(“列四”);

对于(inti=0;i好的,找到了一个解决方案

问题是,没有办法(我知道)返回复选框的边界。但是,由于复选框始终是表中的第一项(如果使用带有
SWT.CHECK的
table
),您可以简单地检查单击事件是否在第一个单元格的左侧

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

publicstaticvoidmain(字符串参数[])
{
显示=新显示();
最终外壳=新外壳(显示,SWT.BORDER | SWT.PRIMARY | SWT.RESIZE);
shell.setText(“测试表”);
setLayout(新的GridLayout(1,true));
最终表格=新表格(shell,SWT.BORDER | SWT.CHECK | SWT.V_SCROLL | SWT.FULL_SELECTION);
表.setheadervible(true);
setLayoutData(新的GridData(SWT.FILL,SWT.FILL,true,true));
for(int col=0;col<5;col++)
{
新的TableColumn(表,SWT.NONE).setText(“列”+col);
}
对于(int行=0;行<10;行++)
{
TableItem项目=新的TableItem(表,SWT.NONE);
for(int col=1;col0)
{
矩形rect=table.getItem(新点(e.x,e.y)).getBounds(0);
如果(矩形包含(新点(e.x,e.y))| | e.x
public static void main(String args[])
{
    Display display = new Display();
    final Shell 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.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true));

    for(int col = 0; col < 5; col++)
    {
        new TableColumn(table, SWT.NONE).setText("column " + col);
    }
    for (int row = 0; row < 10; row++)
    {
        TableItem item = new TableItem(table, SWT.NONE);

        for(int col = 1; col < table.getColumnCount(); col++)
        {
            item.setText(col, "cell " + row + " " + col);
        }
    }
    for(int col = 0; col < table.getColumnCount(); col++)
    {
        table.getColumn(col).pack();
    }

    Button close = new Button(shell, SWT.PUSH);
    close.setText("Done");
    shell.setDefaultButton(close);
    close.setLayoutData(new GridData(SWT.FILL, SWT.END, true, false));

    close.addListener(SWT.Selection, new Listener()
    {
        @Override
        public void handleEvent(Event e)
        {
            shell.dispose();
        }
    });
    table.addListener(SWT.MouseUp, new Listener()
    {
        public void handleEvent(Event e)
        {
            if (table.getItemCount() > 0)
            {
                Rectangle rect = table.getItem(new Point(e.x, e.y)).getBounds(0);
                if (rect.contains(new Point(e.x, e.y)) || e.x <= rect.x)
                    System.out.println("first column or checkbox clicked");
                else
                    System.out.println("other column clicked");
            }
        }
    });

    shell.pack();
    shell.open();
    while (!shell.isDisposed())
    {
        if (!shell.getDisplay().readAndDispatch())
            shell.getDisplay().sleep();
    }
}