Search code examples
javacursorswt

SWT Table don't see items from other columns


I added columns on code:

String[] titles = {"Nazwa", "Uzytkownik", "Haslo"};

for(int i=0; i<titles.length; i++){
    TableColumn column = new TableColumn(table, SWT.NULL);
    column.setText(titles[i]);
}

Next, I added items to said columns:

String[][] a = new String[passwords.length][3];
for(int i=0; i<passwords.length; i++){
    a[i] = passwords[i].split(":");
}
for(int i=0; i<passwords.length; i++){
    TableItem item = new TableItem(table, SWT.NULL);
    //item.setText(Integer.toString(i));            
    for(int j=0; j<a[i].length; j++){
        item.setText(j, a[i][j]);
    }
}

Now, I want get selected items from the second column. I added TableCursor and KeyListener:

tableCursor.addKeyListener(new KeyAdapter() {
    @Override
    public void keyPressed(KeyEvent e) {
        if(e.character == 'c') {
            Point p = tableCursor.getLocation();
            //for(int i=0; i<selection.length; i++){
                TableItem item = table.getItem(p);
                if(item != null){
                MessageBox message = new MessageBox(shell, SWT.ICON_ERROR);
                message.setMessage(item.toString());
                message.open();
            }

        }
    }
});

And now, for example, when I select item from the second column, it doesn't see anything from the second column, only item from the first column. What's the problem?


Solution

  • The problem is the way you try to find the selected item/column in the table.

    Here is an example that does exactly what you want:

    public static void main(String[] args)
    {
        Display display = Display.getDefault();
        final Shell shell = new Shell(display);
        shell.setLayout(new FillLayout());
    
        final Table table = new Table(shell, SWT.NONE);
        table.setHeaderVisible(true);
    
        /* Create columns */
        for (int i = 0; i < 3; i++)
        {
            TableColumn col = new TableColumn(table, SWT.NONE);
            col.setText("Col " + i);
        }
    
        /* Create items */
        for (int i = 0; i < 10; i++)
        {
            TableItem item = new TableItem(table, SWT.NONE);
            item.setText(new String[] { "Row" + i + "Col1", "Row" + i + "Col2", "Row" + i + "Col3" });
        }
    
        /* Pack columns */
        for (int i = 0; i < 3; i++)
        {
            table.getColumn(i).pack();
        }
    
        table.addListener(SWT.MouseUp, new Listener()
        {
            @Override
            public void handleEvent(Event event)
            {
                Point pt = new Point(event.x, event.y);
                TableItem item = table.getItem(pt);
                if (item != null)
                {
                    /* Iterate over all columns and check if event is contained */
                    for (int col = 0; col < table.getColumnCount(); col++)
                    {
                        Rectangle rect = item.getBounds(col);
                        if (rect.contains(pt))
                        {
                            System.out.println(item.getText(col));
                        }
                    }
                }
            }
        });
    
        shell.pack();
        shell.open();
        while (!shell.isDisposed())
        {
            if (!display.readAndDispatch())
                display.sleep();
        }
        display.dispose();
    }