java - 监听器判断表中是否包含元素

标签 java swt jface tableviewer

我正在使用 SWT/JFace 开发一个 Java 项目。我有一个使用 TableViewerTable。该表将永远只有 0 或 1 个项目(取决于它正在查看的数据......)。有没有办法听这个表,看看它是否有一个项目。到目前为止,我一直在查看器上使用 ISelectionChangedListener,但效果不佳,因为用户必须单击项目才能注册更改。理想情况下,每次将项目放入表中或从表中删除时都会触发监听器。我环顾四周,但找不到我需要的东西。

有人知道我在找什么吗?

最佳答案

严格基于此,“表格中只会有 0 或 1 个项目”,一个简单的解决方法是使用 PaintListener,即使窗口隐藏在其他窗口后面也能正常工作. 主要 的缺点是当内容在 UI 上不可见时(例如当 TableItem 可通过滚动访问时)然后它不会 工作。该解决方案适用于 Win7 和 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();
    }
}

注意: LVN_INSERTITEM SWT Table 小部件不支持。检查 Table 小部件中的 windowProc 方法。

关于java - 监听器判断表中是否包含元素,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16197229/

相关文章:

java - SWT 分区表和滚动

java - 使用向导和 ScrolledForm(jface 和 forms api)

java - Eclipse插件: How to add items to a TableViewer asynchronously?

java - 如何从 ViewPager 保存图像?

java - Windows Mobile 的 SWT : UI Architecture

java - Imageloader.save 到 ByteArrayOutputStream 的速度非常慢

java - JFace 向导 : How to have the Progress Bar not disabling the Wizard GUI

java - 一个数据类型只能有 2 个值吗?或者只能有 n 个值?

java - Java 的 java.util.concurrent 包的 .NET 等价物是什么?

java - 在 java 中使用自定义信任库以及默认信任库