java - 带有自定义编辑器的 SWT 表在调整大小时变得困惑

标签 java widget swt editor

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

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();
        }
    }
}

关于如何解决该问题有什么想法吗?

最佳答案

呵呵,偶然发现了解决方案:)我看着你的代码并想:“为什么他要在表格上应用GridLayout?”

删除它,tada!它有效。

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

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

关于java - 带有自定义编辑器的 SWT 表在调整大小时变得困惑,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24954934/

相关文章:

javascript - 使用 React 嵌入 JavaScript 小部件

javascript - 如何在单击时切换 AccordionContainer?

java - SWT 标签大小未正确更新

java - 使用 Jsoup 登录 Google 帐户

java - 寻找最高质因数的更优化方法

drop-down-menu - Flutter中如何自定义DropdownButtons和DropdownMenuItems?

java - 如何过滤可编辑组合框或组合查看器中的值

java.lang.RuntimeException : Unable to start activity ComponentInfo(Program crashes when started) 错误

java - 有没有在大括号之间跳转的热键?

java - SWT 线程 : GUI not responding even after calling thread. stop()