一旦列值和表格最大值达到,SWT表调整大小

时间:2013-05-25 08:29:59

标签: java swt

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

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

enter image description here

现在我使用非常长的String修改了第2列,现在我们只能看到3列,其余的列甚至都不可见。

enter image description here

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

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

1 个答案:

答案 0 :(得分:3)

据我所知,使用TableViewer可以让您的生活更轻松。

Here是关于TableViewer的优秀教程。

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

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

这就是它的样子:

enter image description here