简体   繁体   English

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

[英]SWT Table Resize, once column value and Table Max size reaches

I am trying to add a big big string in a SWT table. 我试图在SWT表中添加一个大的大字符串。 If you put any big String very long, then that column occupies the entire area and next columns are not even visible and you are left with no option. 如果将任何大String放置得很长,则该列将占据整个区域,并且下一列甚至都不可见,并且您别无选择。 Ideally that much max value should not be shown or some mechanism should be there to handle. 理想情况下,不应显示太大的最大值,或者应该有某种机制可以处理。

Here is the SWT table with normal view- We can see 5 columns . 这是带有普通视图的SWT表-我们可以看到5列。

在此处输入图片说明

Now I modified Column 2 with a very long String and now we can see only 3 columns and rest of the columns are not even visible. 现在,我用很长的String修改了第2列,现在我们只能看到3列,其余的列甚至都不可见。

在此处输入图片说明

Following is the source code for the same. 以下是相同的源代码。 I want to know how can I avoid this resizing problem if the content of the column is very long. 我想知道如果该列的内容很长,如何避免调整大小的问题。

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

From what I can tell, using a TableViewer would make your life much easier. 据我所知,使用TableViewer将使您的生活更加轻松。

Here is an excellent tutorial on TableViewer s. 是有关TableViewer的出色教程。

I created an example that should give you an idea on how to accomplish what you want to do: 我创建了一个示例,该示例应为您提供有关如何完成您想做的事情的想法:

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

This is what it looks like: 看起来是这样的:

在此处输入图片说明

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM