swt表对单元格中的文本长度有限制吗?

时间:2011-07-08 07:07:08

标签: java swt jface

我使用JFace TableViewer和数据绑定来显示数据库表的数据,有些列有很长的文本,我发现文本被剪掉了。如果我激活与该单元格关联的文本编辑器,我可以看到全文。

swt表是否限制单元格中的文本长度?或OS有这样的限制?(我使用eclipse 3.6和Windows 7 32位)

/*******************************************************************************
 * Copyright (c) 2006 Tom Schindl and others.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 *
 * Contributors:
 *     Tom Schindl - initial API and implementation
 *******************************************************************************/

package org.eclipse.jface.snippets.viewers;

import org.eclipse.jface.viewers.IStructuredContentProvider;
import org.eclipse.jface.viewers.LabelProvider;
import org.eclipse.jface.viewers.TableViewer;
import org.eclipse.jface.viewers.Viewer;
import org.eclipse.swt.layout.FillLayout;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Shell;

/**
 * A simple TableViewer to demonstrate usage
 * 
 * @author Tom Schindl <tom.schindl@bestsolution.at>
 *
 */
public class Snippet001TableViewer {
    private class MyContentProvider implements IStructuredContentProvider {

        /* (non-Javadoc)
         * @see org.eclipse.jface.viewers.IStructuredContentProvider#getElements(java.lang.Object)
         */
        public Object[] getElements(Object inputElement) {
            return (MyModel[])inputElement;
        }

        /* (non-Javadoc)
         * @see org.eclipse.jface.viewers.IContentProvider#dispose()
         */
        public void dispose() {

        }

        /* (non-Javadoc)
         * @see org.eclipse.jface.viewers.IContentProvider#inputChanged(org.eclipse.jface.viewers.Viewer, java.lang.Object, java.lang.Object)
         */
        public void inputChanged(Viewer viewer, Object oldInput, Object newInput) {

        }

    }

    public class MyModel {
        public int counter;

        public MyModel(int counter) {
            this.counter = counter;
        }

        public String toString() {
            **return "very loooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooog text" + this.counter;**
        }
    }

    public Snippet001TableViewer(Shell shell) {
        final TableViewer v = new TableViewer(shell);
        v.setLabelProvider(new LabelProvider());
        v.setContentProvider(new MyContentProvider());
        MyModel[] model = createModel();
        v.setInput(model);
        v.getTable().setLinesVisible(true);
    }

    private MyModel[] createModel() {
        MyModel[] elements = new MyModel[10];

        for( int i = 0; i < 10; i++ ) {
            elements[i] = new MyModel(i);
        }

        return elements;
    }

    /**
     * @param args
     */
    public static void main(String[] args) {
        Display display = new Display ();
        Shell shell = new Shell(display);
        shell.setLayout(new FillLayout());
        new Snippet001TableViewer(shell);
        shell.open ();

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

        display.dispose ();

    }

}

enter image description here

2 个答案:

答案 0 :(得分:7)

这是windows bug / feature(see bugzilla for details),这是证明(你的代码的linux截图)

enter image description here

我可以通过自我单元格渲染来解决此错误/功能(请参阅Custom Drawing Table and Tree Items教程)。

答案 1 :(得分:0)

我找到了一种简单的方法来显示整个文本。 您必须使用StyledCellLabelProvider并覆盖update-method。

这是一个显示StyledCellLabelProvider和ColumnLabelProvider之间差异的小例子。要覆盖ColumnLabelProvider的update-method是不必要的。我这样做是为了表明它取决于课程。

package tabletest;

import org.eclipse.jface.layout.GridDataFactory;
import org.eclipse.jface.viewers.ArrayContentProvider;
import org.eclipse.jface.viewers.ColumnLabelProvider;
import org.eclipse.jface.viewers.LabelProvider;
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.swt.SWT;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.widgets.TableColumn;

public class MyClass {

    private static final String LINE = "123456789A123456789B123456789C123456789D123456789E123456789F123456789G123456789H123456789I123456789J"// 100
            + "123456789K123456789L123456789M123456789N123456789O123456789P123456789Q123456789R123456789S123456789T" // 200
            + "123456789U123456789V123456789W123456789X123456789Y123456789Z" // 260
            + " a lot mor text";

    public MyClass(Shell shell){
        createControl(shell);
    }

    private void createControl(Composite parent){
        parent.setLayout(new GridLayout(1, true));

        TableViewer viewer = new TableViewer(parent, SWT.FULL_SELECTION | SWT.BORDER | SWT.V_SCROLL |SWT.H_SCROLL);
        viewer.getTable().setHeaderVisible(true);
        viewer.getTable().setLinesVisible(true);
        viewer.setContentProvider(ArrayContentProvider.getInstance());
        viewer.setLabelProvider(new LabelProvider());

        createColumn(viewer);

        viewer.setInput(new String[] { LINE });
        for(TableColumn col : viewer.getTable().getColumns()){
            col.pack();
        }

        GridDataFactory.fillDefaults().grab(true, true).applyTo(viewer.getControl());
    }

    private void createColumn(TableViewer viewer) {
        TableViewerColumn column1 = new TableViewerColumn(viewer, SWT.NONE);
        column1.getColumn().setText("ColumnLabelProvider");
        column1.setLabelProvider(new ColumnLabelProvider(){
            @Override
            public void update(ViewerCell cell) {
                cell.setText(cell.getElement().toString());
                super.update(cell);
            }
        });
        TableViewerColumn column2 = new TableViewerColumn(viewer, SWT.NONE);
        column2.getColumn().setText("StyledCellLabelProvider");
        column2.setLabelProvider(new StyledCellLabelProvider() {
            @Override
            public void update(ViewerCell cell) {
                cell.setText(cell.getElement().toString());
                super.update(cell);
            }
        }); 
    }

    public static void main(String[] args) {
        Display display = new Display();
        Shell shell = new Shell(display);
        new MyClass(shell); 
        shell.open();
        while(!shell.isDisposed()){
            if(!display.readAndDispatch()){
                display.sleep();
            }
        }
    }
}

当您将LabelProvider更改为StyledCellLabelProvider并覆盖与我的示例类似的更新方法时,它也适用于Snippet001TableViewer。