0

我正在尝试在 SWT 表中添加一个大字符串。如果您将任何大字符串放置得很长,则该列将占据整个区域,并且下一列甚至都不可见,您将别无选择。理想情况下,不应该显示太多最大值,或者应该有一些机制来处理。

这是具有普通视图的 SWT 表 - 我们可以看到 5 列。

在此处输入图像描述

现在我用一个很长的字符串修改了第 2 列,现在我们只能看到 3 列,其余列甚至不可见。

在此处输入图像描述

以下是相同的源代码。如果列的内容很长,我想知道如何避免这种调整大小的问题。

import org.eclipse.swt.*;
import org.eclipse.swt.layout.*;
import org.eclipse.swt.widgets.*;

public class Snippet181 {

    public static void main(String[] args) {
        Display display = new Display();
        Shell shell = new Shell(display);
        shell.setLayout(new RowLayout(SWT.HORIZONTAL));
        final Table table = new Table(shell, SWT.BORDER | SWT.CHECK);
        table.setLayoutData(new RowData(-1, 300));
        table.setHeaderVisible(true);
        TableColumn column = new TableColumn(table, SWT.LEFT);
        column.setText("Column 0");
        column = new TableColumn(table, SWT.CENTER);
        column.setText("Column 1");
        column = new TableColumn(table, SWT.CENTER);
        column.setText("Column 2");
        column = new TableColumn(table, SWT.CENTER);
        column.setText("Column 3");
        column = new TableColumn(table, SWT.CENTER);
        column.setText("Column 4");
        for (int i = 0; i < 100; i++) {
            TableItem item = new TableItem(table, SWT.NONE);
            String[] text=null;
            if(i >50 && i <55)
            text = new String[]{i+" 0", i+" 1", i+"282347378237895728975894758934895893458934789345834895792823473782378957289758947589348958934589347893458348957928234737823789572897589475893489589345893478934583489579282347378237895728975894758934895893458934789345834895792823473782378957289758947589348958934589347893458348957928234737823789572897589475893489589345893478934583489579282347378237895728975894758934895893458934789345834895792823473782378957289758947589348958934589347893458348957928234737823789572897589475893489589345893478934583489579282347378237895728975894758934895893458934789345834895792823473782378957289758947589348958934589347893458348957928234737823789572897589475893489589345893478934583489579282347378237895728975894758934895893458934789345834895792823473782378957289758947589348958934589347893458348957928234737823789572897589475893489589345893478934583489579", i+" 3", i+" 4"};
            else
                text = new String[]{i+" 0", i+" 1", i+" 282347", i+" 3", i+" 4"};
            item.setText(text);
        }
        Listener listener = new Listener() {
            public void handleEvent(Event e) {
                System.out.println("Move "+e.widget);
            }
        };
        TableColumn[] columns = table.getColumns();
        for (int i = 0; i < columns.length; i++) {
            columns[i].pack();
            columns[i].setMoveable(true);
            columns[i].addListener(SWT.Move, listener);
        }
        Button b = new Button(shell, SWT.PUSH);
        b.setText("invert column order");
        b.addListener(SWT.Selection, new Listener() {
            public void handleEvent(Event e) {
                int[] order = table.getColumnOrder();
                for (int i = 0; i < order.length / 2; i++) {
                    int temp = order[i];
                    order[i] = order[order.length - i - 1];
                    order[order.length - i - 1] = temp;
                }
                table.setColumnOrder(order);
            }
        });
        shell.pack();
        shell.open();
        while (!shell.isDisposed()) {
            if (!display.readAndDispatch())
                display.sleep();
        }
        display.dispose();
    }
}
4

1 回答 1

4

据我所知,使用 aTableViewer会让你的生活更轻松。

TableViewer是一个关于s的优秀教程。

我创建了一个示例,可以让您了解如何完成您想做的事情:

public static void main(String[] args)
{
    final Display display = new Display();
    final Shell shell = new Shell(display);
    shell.setLayout(new FillLayout());

    TableViewer viewer = new TableViewer(shell);

    /* Create the columns */
    for (int i = 0; i < 4; i++)
    {
        TableViewerColumn column = new TableViewerColumn(viewer, SWT.NONE);
        /* Set the trim label provider (will trim the text displayed) */
        column.setLabelProvider(new TrimLabelProvider());
        column.getColumn().setText("Column " + i);
    }

    /* Set the input from the content provider */
    viewer.setContentProvider(new ArrayContentProvider());
    viewer.setInput(DataModelProvider.getInstance().getItems());

    /* Style the table */
    Table table = viewer.getTable();
    table.setHeaderVisible(true);
    table.setLinesVisible(true);

    /* Pack the columns */
    for (TableColumn column : table.getColumns())
        column.pack();

    shell.pack();
    shell.open();

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

/**
 * This class holds the data displayed in the table viewer
 */
private static class DataModelProvider
{
    private static DataModelProvider    INSTANCE;
    private List<TableRow>              items;

    public static DataModelProvider getInstance()
    {
        if (INSTANCE == null)
            INSTANCE = new DataModelProvider();

        return INSTANCE;
    }

    private DataModelProvider()
    {
        /*
         * Create dummy content. One item with short text and one item with
         * long text
         */
        items = new ArrayList<TableRow>();
        items.add(new TableRow(new String[] { "first", "second", "third", "fourth" }));
        items.add(new TableRow(new String[] { "first", "second second second second second second second second", "third", "fourth" }));
    }

    public void addItem(TableRow item)
    {
        items.add(item);
    }

    public List<TableRow> getItems()
    {
        return items;
    }
}

/**
 * This class models the data items displayed in the table
 */
private static class TableRow
{
    private String[]    data;

    public TableRow(String[] input)
    {
        data = input;
    }

    public String getData(int index)
    {
        if (index < 0 || index > data.length - 1)
            throw new IllegalArgumentException("Invalid index: " + index + ". Minimum: 0, Maximum: " + (data.length - 1));
        return data[index];
    }
}

/**
 * This class takes care of how the data is displayed in the table
 */
private static class TrimLabelProvider extends CellLabelProvider
{

    @Override
    public void update(ViewerCell cell)
    {
        /*
         * This method is called by the TableViewer to get
         * the String to display in the cell
         */
        Object element = cell.getElement();
        if (element instanceof TableRow)
        {
            int columnIndex = cell.getColumnIndex();

            TableRow row = (TableRow) cell.getElement();

            /* Here we trim the text */
            cell.setText(shortenText(row.getData(columnIndex)));
        }
    }

    private String shortenText(String text)
    {
        /* This is the maximal length we allow */
        int value = 20;

        if (text.length() > value)
        {
            int index = text.indexOf(" ", value);
            if (index != -1)
                return text.substring(0, index) + " [...]";
        }
        return text;
    }
}

这是它的样子:

在此处输入图像描述

于 2013-05-25T10:11:07.560 回答