Question

When clicking a row a window pops up.

When clicking the cell in the first column nothing happens because a checkbox is located in the first column and a mouselistener detects the cell in the first column.

But selecting the checkbox the window pops up again.

How can i prevent the window popping up when selecting the checkbox?

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();
                }   
        }

}

Was it helpful?

Solution

Alright, found a solution.

The problem is, that there is no way (that I'm aware of) which will return the bounds of the checkbox. However, since the checkbox will always be the first thing in the table (if you use a Table with SWT.CHECK), you can simply check if the click event is to the left of the first cell.

I took the liberty to "optimize" some of your other code (my subjective opinion):

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();
    }
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top