java - 表中 SWT Combo 的性能不佳

标签 java performance memory-leaks combobox swt

我在表中的组合方面遇到性能问题。 当我启动应用程序时,一切都很好。 我可以更改组合的值,并且表格的 react 还可以。 但是当我清理 table 并重新填充它时,似乎出现了灾难性的性能损失。 当我尝试从表中选择一行时,大约需要 1-2 秒才能选择它。 当我点击“重置表”按钮 5-10 次时,行选择需要 5-6 秒。

首先我想我必须在使用后处理掉所有东西。但这并没有改变任何事情。 我也用文本框尝试过。一样的问题。 第一次性能达到可接受的水平。按重置按钮 --> 性能非常糟糕

这里是这个问题的一个例子:

import org.eclipse.swt.SWT;
import org.eclipse.swt.custom.TableEditor;
import org.eclipse.swt.events.ModifyEvent;
import org.eclipse.swt.events.ModifyListener;
import org.eclipse.swt.events.SelectionAdapter;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.events.SelectionListener;
import org.eclipse.swt.graphics.Rectangle;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Combo;
import org.eclipse.swt.widgets.Control;
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 Test {
private static Table table;

public static void main(String[] args) {
    Display display = new Display();
    Shell shell = new Shell(display);
    table = new Table(shell, SWT.BORDER | SWT.V_SCROLL | SWT.H_SCROLL | SWT.FULL_SELECTION );
    table.setHeaderVisible(true);
    TableColumn column = new TableColumn(table, SWT.NONE);
    column.setText("1");
    column.setWidth(70);
    column = new TableColumn(table, SWT.NONE);
    column.setText("2");
    column.setWidth(70);
    column = new TableColumn(table, SWT.NONE);
    column.setText("3");
    column.setWidth(70);
    fillTable();
    Rectangle clientArea = shell.getClientArea();
    table.setBounds(clientArea.x, clientArea.y, 250, 300);
    shell.setSize(400, 400);
    Button button = new Button(shell, SWT.PUSH);
    button.setBounds(10, 320, 150, 25);
    button.setText("Reset Table");
    button.addSelectionListener(new SelectionListener(){

        @Override
        public void widgetDefaultSelected(SelectionEvent e) {
            // TODO Auto-generated method stub
        }

        @Override
        public void widgetSelected(SelectionEvent e) {
            resetTable();
        }

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

public static void addComboToCell(final int intgPEditCell) {
    final TableEditor editor = new TableEditor(table);
    // The editor must have the same size as the cell and must
    // not be any smaller than 50 pixels.
    editor.horizontalAlignment = SWT.LEFT;
    editor.grabHorizontal = true;
    editor.minimumWidth = 50;
    // editing the second column
    table.addSelectionListener(new SelectionAdapter() {
        @Override
        public void widgetSelected(SelectionEvent e) {
            // Clean up any previous editor control
            Control oldEditor = editor.getEditor();
            if (oldEditor != null)
                oldEditor.dispose();

            // Identify the selected row
            TableItem item = (TableItem) e.item;
            if (item == null)
                return;

            // The control that will be the editor must be a child of the
            // Table
            Combo newEditor = new Combo(table, SWT.NONE);
            // SWT.NONE);
            newEditor.setText(item.getText(intgPEditCell));
            newEditor.addModifyListener(new ModifyListener() {
                public void modifyText(ModifyEvent me) {
                    Combo combo = (Combo) editor.getEditor();
                    editor.getItem()
                            .setText(intgPEditCell, combo.getText());
                }
            });
             newEditor.setFocus();
            editor.setEditor(newEditor, item, intgPEditCell);
        }
    });
}

public static void resetTable(){
    TableItem[] tableItems = table.getItems();
    for (int i = 0; i < tableItems.length; i++) {
        tableItems[i].dispose();
    }
    Control[] children = table.getChildren();
    for (int i = 0 ; i < children.length; i++) {
        children[i].dispose();
    }
    table.removeAll();
    fillTable();
}

public static void fillTable(){
    for (int i = 0; i < 50; i++) {
        TableItem item = new TableItem(table, 0);
        item.setText(0, "0 Item " + i);
        item.setText(1, "1 Item " + i);
        item.setText(2, "2 Item " + i);
        addComboToCell(0);
    }
}
}

所以我的问题是如何在重新填充表格后提高性能?! 我是不是忘记了什么或者做错了什么? 谢谢

最佳答案

性能下降是由您添加的TableEditor引起的。每行一个,它们都在监听表选择事件。因此,如果选择一个项目,则会激活 50 个 TableEditor,并相互堆叠。除此之外,TableEditor 不会在 resetTable 中删除,因此每次选择重置按钮都会增加 50 个。

SWT 中 TableEditors 的概念是每列或每种编辑器类型都有一个编辑器。在适当的情况下,编辑器会移动到正确的位置,使用正确的值进行初始化,然后显示。

在您的情况下,需要将 addComboToCell ( 0 ) 调用移至 main 方法(位于创建表之后的某个位置)。

关于java - 表中 SWT Combo 的性能不佳,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28720822/

相关文章:

c# - 多态性的类型。超过一个?

performance - 是否有任何工具可以优化 JMS 队列上的消费者和生产者线程的数量?

version-control - 程序员如何跨多台计算机工作?

apache-flex - Flex 应用程序中的内存泄漏

java - JVM中如何定义内存泄漏?

java - 根据 2 个属性对 ArrayList 进行排序

java - Spring Redis缓存实现

java - ServletContext 和 ServletActionContext 有什么区别

java - oracle中使用java的性能开销

c++ - 如何安全地填充 Boosts 的指针容器?