SWT:表列调整大小的监听器

时间:2017-05-15 06:18:32

标签: swt listener

我有以下布局

其中的想法是通过在顶部的过滤器文本字段中键入文本来使表格可过滤(此代码部分未在下面描述)。

用法应该是过滤器匹配相应的宽度列,当水平滚动时,表和过滤器字段都应移动,以便过滤器与其列之间的对应关系保持不变。

我有以下问题:

  1. 如果用户调整列的大小,我需要附加哪种类型的侦听器才能获得通知,以便调整过滤器字段的大小以使它们与新的列宽相匹配(因此可以调用alignFilter())?

  2. 我对过滤器字段的高度和宽度进行了破解(" // hack"几乎在底部),如何使其变得更干净?

  3. 最后,当滚动表格时,过滤器字段将从可见区域滚动,因此为了过滤文本,必须向上滚动。如何更改布局以使过滤器字段保持可见?

  4. public class FilteredTable {
    
    private final static int numColumns = 20;
    private static Text filter[];
    private static Table table;
    
    public static void main(String[] args) {
    
        final Display display = new Display();
        final Shell shell = new Shell(display);
        shell.setLayout(new GridLayout());
    
        // Outer area with scrollbars
        final ScrolledComposite scrollArea = new ScrolledComposite(shell, SWT.V_SCROLL | SWT.H_SCROLL);
        scrollArea.setLayout(new GridLayout());
        scrollArea.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true));
    
        // Inner area to hold in its first row the filter fields and in its second row the table
        final Composite content = new Composite(scrollArea, SWT.NONE);
        final GridLayout gridLayout = new GridLayout(numColumns, false);
        gridLayout.marginLeft = 0;
        gridLayout.horizontalSpacing = 0;
        content.setLayout(gridLayout);
        content.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true));
    
        // First row: text filters
        filter = new Text[numColumns];
        for (int i=0; i < numColumns; i++) {
    
            filter[i] = new Text(content, SWT.BORDER);
            filter[i].setText("Filter " + i);
        }
    
        // Second row: table
        table = new Table(content, SWT.NO_SCROLL);
        GridDataFactory.fillDefaults()
        .grab(true, true)
        .span(numColumns, 1)
        .applyTo(table);
    
        String heading = "";
        for (int col = 0; col < numColumns; col++) {
            heading += "o";
            TableColumn column = new TableColumn(table, SWT.NONE);
            column.setText(heading);
        }
        table.setHeaderVisible(true);
    
        scrollArea.setContent(content);
        scrollArea.setExpandHorizontal(true);
        scrollArea.setExpandVertical(true);
        scrollArea.setAlwaysShowScrollBars(true);
        scrollArea.setMinSize(table.computeSize(SWT.DEFAULT, SWT.DEFAULT));
    
        // Test data
        for (int row = 0; row < 20; row++)  {
            TableItem item = new TableItem(table, SWT.NONE);
    
            for (int col = 0; col < table.getColumnCount(); col++) {
                item.setText(col, row + "." + col);
            }
        }
    
        for (int col = 0; col < table.getColumnCount(); col++) {
            table.getColumn(col).pack();
        }
    
        scrollArea.setMinSize(table.computeSize(SWT.DEFAULT, SWT.DEFAULT));
    
        alignFilter();
    
        shell.pack();
        shell.setSize(400, 300);
    
        shell.open();
        while (!shell.isDisposed())
        {
            if (!display.readAndDispatch())
                display.sleep();
        }
        display.dispose();
    }
    
    private static void alignFilter() {
        for (int i=0; i < filter.length; i++) {
            GridDataFactory.fillDefaults()
            .grab(false, false)
            .hint(table.getColumn(i).getWidth()-(i==0 ? 11 : 12), 20) // Hack
            .applyTo(filter[i]);
            filter[i].pack();
        }
    }
    }
    

0 个答案:

没有答案