Java SWT(这是一个基本的java错误):为什么这段代码不起作用?

时间:2015-08-06 06:56:06

标签: java swt jface tableviewer

我在学习java swt时正在构建一个示例表。有人可以帮助我,为什么这段代码不起作用。运行代码时没有任何反应。

我想我没有正确地写出主要方法。需要改变什么。

如果你有一些链接,我可以学习Java swt非常好,请提供相同的。我找不到swt的帮助

import java.util.ArrayList;
import java.util.List;


import org.eclipse.jface.dialogs.Dialog;
import org.eclipse.jface.viewers.ArrayContentProvider;
import org.eclipse.jface.viewers.ColumnViewer;
import org.eclipse.jface.viewers.ColumnViewerToolTipSupport;
import org.eclipse.jface.viewers.StyledCellLabelProvider;
import org.eclipse.jface.viewers.TableViewer;
import org.eclipse.jface.viewers.TableViewerColumn;
import org.eclipse.jface.viewers.ViewerCell;
import org.eclipse.jface.viewers.ViewerColumn;
import org.eclipse.swt.SWT;
import org.eclipse.swt.layout.FillLayout;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.widgets.Composite;
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;

public class TestTable extends Dialog {

    private TableViewer tableviewer;
    private List<TestModel> entities;
    private TestTable testTable;

    public TestTable(Shell parentShell) {
        super(parentShell);
    }

    @Override
    public void create() {

        super.create();

        loadData();

    }

    @Override
    protected Control createDialogArea(Composite parent) {

        GridData dataLayout;

        Composite area = (Composite) super.createDialogArea(parent);

        dataLayout = new GridData(GridData.FILL_BOTH);
        dataLayout.heightHint = 150;
        dataLayout.widthHint = 500;


        Composite wrapper = new Composite(area, SWT.NONE);
        wrapper.setLayoutData(dataLayout);
        wrapper.setLayout(new FillLayout());

        tableviewer = new TableViewer(wrapper, SWT.BORDER | SWT.MULTI);
        tableviewer.setContentProvider(new ArrayContentProvider());

        ColumnViewerToolTipSupport.enableFor(tableviewer);

        final Table table = tableviewer.getTable();
        table.setLinesVisible(true);
        table.setHeaderVisible(true);

        createColumns(wrapper);

        return area;

    }

    private void createColumns(Composite wrapper) {

        TableViewerColumn firstnameColumn = new TableViewerColumn(tableviewer, SWT.NONE);
        firstnameColumn.getColumn().setText("Vorname");
        firstnameColumn.setLabelProvider(new StyledCellLabelProvider(StyledCellLabelProvider.COLORS_ON_SELECTION) {

            @Override
            public void initialize(ColumnViewer viewer, ViewerColumn column) {
                super.initialize(viewer, column);
                this.setOwnerDrawEnabled(false);
            }

            @Override
            public void update(ViewerCell cell) {
                TestModel model = (TestModel) cell.getElement();
                cell.setText(model.getFirstName());
                cell.setBackground(Display.getDefault().getSystemColor(SWT.COLOR_DARK_GREEN));
            }

            @Override
            public String getToolTipText(Object element) {
                TestModel model = (TestModel) element;
                return "USE THIS AS TOOLTIP";
            }

        });

        TableViewerColumn lastnameColumn = new TableViewerColumn(tableviewer, SWT.NONE);
        lastnameColumn.getColumn().setText("Nachname");
        lastnameColumn.setLabelProvider(new StyledCellLabelProvider(StyledCellLabelProvider.COLORS_ON_SELECTION) {

            @Override
            public void initialize(ColumnViewer viewer, ViewerColumn column) {
                super.initialize(viewer, column);
                this.setOwnerDrawEnabled(false);
            }

            @Override
            public void update(ViewerCell cell) {
                TestModel model = (TestModel) cell.getElement();
                cell.setText(model.getLastName());
                cell.setBackground(Display.getDefault().getSystemColor(SWT.COLOR_DARK_GREEN));
            }

            @Override
            public String getToolTipText(Object element) {
                TestModel model = (TestModel) element;
                return "USE THIS AS TOOLTIP";
            }

        });

        for (TableColumn c : tableviewer.getTable().getColumns()) {
            c.pack();
        }

    }

    private void loadData() {

        entities = new ArrayList<TestModel>();

        entities.add(new TestModel("___Firstname1___", "Lastname1", "Username1", "Kommentar"));
        entities.add(new TestModel("___Firstname2___", "Lastname2", "Username2", "Kommentar"));
        entities.add(new TestModel("___Firstname3___", "Lastname3", "Username3", "Kommentar"));
        entities.add(new TestModel("___Firstname4___", "Lastname4", "Username4", "Kommentar"));
        entities.add(new TestModel("___Firstname5___", "Lastname5", "Username5", "Kommentar"));

        tableviewer.setInput(entities);
        tableviewer.refresh();

    }
    public static void main(String[] args){
        Shell shell = new Shell();
        /*TestTable testTable = */new TestTable(shell);

    }


}

1 个答案:

答案 0 :(得分:2)

尝试:

public static void main(String[] args){
    Shell shell = new Shell();
    TestTable testTable = new TestTable(shell);
    testTable.open();
}

Vogella通常有很好的教程:

http://www.vogella.com/tutorials/EclipseDialogs/article.html