自定义编辑器的SWT表在调整大小时搞砸了

时间:2014-07-25 11:40:06

标签: java widget swt editor

我使用表格显示5列。第3和第5个包含自定义小部件,第4个是可编辑的小部件。在窗口的第一次出现时,一切看起来都很好:

enter image description here

但是,如果我稍微调整窗口大小,表格内的小部件会移动到错误的位置。

enter image description here

为了让表格再次正确,只需将焦点设置到其中即可。

这是一个MCVE,可以重现这个问题:

import java.util.ArrayList;

import org.eclipse.jface.window.ApplicationWindow;
import org.eclipse.swt.SWT;
import org.eclipse.swt.custom.TableEditor;
import org.eclipse.swt.layout.FillLayout;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Combo;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Control;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Event;
import org.eclipse.swt.widgets.Listener;
import org.eclipse.swt.widgets.Table;
import org.eclipse.swt.widgets.TableColumn;
import org.eclipse.swt.widgets.TableItem;

public class TableTest
    extends ApplicationWindow
{
    private Table table;
    ArrayList<TableDescription> tableDescriptions = new ArrayList<TableTest.TableDescription>();

    public class TableDescription
    {
        private String name;
        private String type;
        public TableDescription(String name, String type)
        {
            this.name = name;
            this.type = type;
        }
        public String getName()
        {
            return name;
        }
        public String getType()
        {
            return type;
        }
    }

    public TableTest()
    {
        super(null);

        // Test data
        tableDescriptions.add(new TableDescription("Rev_Id", "bigint"));
        tableDescriptions.add(new TableDescription("Entity_Name", "varchar"));
    }

    @Override
    protected Control createContents(Composite parent)
    {
        Composite container = new Composite(parent, SWT.NONE);
        container.setLayout(new FillLayout(SWT.HORIZONTAL));

        table = new Table(container, SWT.SINGLE | SWT.BORDER | SWT.FULL_SELECTION);
        table.setLinesVisible(true);
        table.setHeaderVisible(true);
        GridLayout layout = new GridLayout();
        table.setLayout(layout);

        String[] titles = {"Column name", "Column type", "Visbility", "GUI name", "GUI type"};
        for (int i = 0; i < titles.length; i++)
        {
            final TableColumn column = new TableColumn(table, SWT.NONE);
            column.setText(titles[i]);
        }

        int[] columnsWidths = new int[]{100, 100, 50, 100, 95};

        for (TableDescription tableDescription : tableDescriptions)
        {
            TableItem item = new TableItem(table, SWT.NONE);
            item.setText(0, tableDescription.getName());
            item.setText(1, tableDescription.getType());
            item.setText(3, tableDescription.getName().replaceAll("_", " "));
        }

        for (int i = 0; i < table.getColumnCount(); i++)
        {
            table.getColumn(i).setWidth(columnsWidths[i]);
        }

        // Listener which sets the row heights. It adds editors on the
        // second event, to place them accordingly to the new cell sizes.
        // Then it removes itself.
        table.addListener(SWT.MeasureItem, new Listener()
        {
            boolean rowHeightSet;

            public void handleEvent(Event event)
            {
                if (!rowHeightSet)
                {
                    rowHeightSet = true;
                    event.height = 25;
                }
                else
                {
                    addEditors(tableDescriptions);
                    table.removeListener(SWT.MeasureItem, this);
                }
            }
        });

        return container;
    }

    private void addEditors(ArrayList<TableDescription> tableDescriptions)
    {
        for (int i = 0; i < tableDescriptions.size(); i++)
        {
            TableItem item = table.getItem(i);
            TableEditor editor = new TableEditor(table);
            Button checkButton = new Button(table, SWT.CHECK);
            checkButton.setSelection(true);
            checkButton.pack();
            editor.minimumWidth = checkButton.getSize().x;
            editor.horizontalAlignment = SWT.CENTER;
            editor.setEditor(checkButton, item, 2);
            item.setData("checkButtonEditor", editor);
            editor = new TableEditor(table);
            Combo box = new Combo(table, SWT.READ_ONLY);
            box.pack();
            editor.minimumWidth = box.getSize().x;
            editor.horizontalAlignment = SWT.LEFT;
            editor.setEditor(box, item, 4);
            item.setData("comboBoxEditor", editor);
        }
    }

    public static void main(String args[])
    {
        try
        {
            TableTest window = new TableTest();
            window.setBlockOnOpen(true);
            window.open();
            Display.getCurrent().dispose();
        }
        catch (Exception e)
        {
            e.printStackTrace();
        }
    }
}

关于如何解决这个问题的任何想法?

1 个答案:

答案 0 :(得分:2)

嘿,偶然找到了解决方案:)我正在查看你的代码并思考:&#34;他为什么要在桌面上使用GridLayout?&#34;

删除它和tada!它有效。

所以只需删除这两行就可以了:

GridLayout layout = new GridLayout();
table.setLayout(layout);