crhurley4
crhurley4

Reputation: 21

Listener to tell if a table has an element in it

I'm working on a Java project using SWT/JFace. I have a Table that is using a TableViewer. The table will only ever have 0 or 1 items in it (depending on the data it's viewing...). Is there a way to listen to this table to see if it has an item in it or not. I have been using ISelectionChangedListener on the viewer so far, but it doesn't work perfectly as the user must click on the item to register the change. Ideally the listener would be triggered every time an item is put into or removed from the table. I've looked around, but can't find what I need.

Anyone out there know what I am looking for?

Upvotes: 2

Views: 914

Answers (1)

Favonius
Favonius

Reputation: 13974

Strictly based on this, "table will only ever have 0 or 1 items in it", a simple work around is to use a PaintListener, this works even if the window is hidden behind other windows. The major drawback is when the content is not visible on the UI (like when TableItem is accessible via scrolling) then it won't work. The solution works on Win7 and with eclipse 3.7.2.

import org.eclipse.swt.SWT;
import org.eclipse.swt.events.PaintEvent;
import org.eclipse.swt.events.PaintListener;
import org.eclipse.swt.events.SelectionAdapter;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.widgets.Table;
import org.eclipse.swt.widgets.TableColumn;
import org.eclipse.swt.widgets.TableItem;

public class TableTest {
    static int counter = 0;
    public static void main(String[] args) {
        final Display display = new Display();
        Shell shell = new Shell(display);
        shell.setLayout(new GridLayout(1, false));
        shell.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true));

        final Table table = new Table(shell, SWT.BORDER);
        table.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true));
        table.setHeaderVisible(true);
        table.setLinesVisible(true);
        table.addPaintListener(new PaintListener() {
            public void paintControl(PaintEvent e) {
                System.out.println(((Table)e.widget).getItemCount() > 0);
            }
        });

        TableColumn column = new TableColumn(table, SWT.LEFT);
        column.setWidth(180);
        column.setText("Test Column");

        Button button = new Button(shell, SWT.PUSH);
        button.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, false));
        button.setText("Add item");
        button.addSelectionListener(new SelectionAdapter() {
            public void widgetSelected(SelectionEvent e) {
                TableItem item = new TableItem(table, SWT.NONE);
                item.setText("" + counter++);
                if(counter > 10) {
                    table.clearAll();
                    counter = 0;
                } 
            }
        });

        display.timerExec(1000, new Runnable() {
            public void run() {
                if(table.isDisposed()) return;
                TableItem item = new TableItem(table, SWT.NONE);
                item.setText("" + counter++);
                if(counter > 10) {
                    table.clearAll();
                    counter = 0;
                }

                display.timerExec(1000, this);
            }
        });

        shell.setSize(220, 300);
        shell.open();
        while (!shell.isDisposed()) {
            if (!display.readAndDispatch())
                display.sleep();
        }
        display.dispose();
    }
}

Note: The LVN_INSERTITEM is not supported in SWT Table widget. Check the windowProc method in the Table widget.

Upvotes: 1

Related Questions