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

时间:2022-03-25 01:02:16

I use JFace TableViewer and databinding to display data of a database table, some columns have very long text, I found the text is cut out. if I activate the text editor associated with that cell, I can see the full text.

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

Does swt table has limitation on text length in a cell ? or the OS has such limitation ?(I am using eclipse 3.6 and windows 7 32 bit)

swt表对单元格中的文本长度有限制吗?或者操作系统有这样的限制(我使用的是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 ();

    }

}

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

2 个解决方案

#1


7  

It's windows bug/feature (see bugzilla for details), here is the proof (linux screenshot of your code)

它是windows bug/特性(详细信息请参见bugzilla),以下是证明(您的代码的linux屏幕截图)

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

I may be possible to workaround this bug/feature by self cell rendering (see Custom Drawing Table and Tree Items tutorial).

我可以通过自单元格渲染来解决这个bug/特性(参见自定义绘图表和树项目教程)。

#2


0  

I found a simple way to get the whole text shown. You have to use a StyledCellLabelProvider and override the update-method.

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

Here is a little example which shows the differences between a StyledCellLabelProvider and a ColumnLabelProvider. To override the update-method of ColumnLabelProvider is unnecessary. I did it to show that it depends on the class.

这里有一个小例子,说明了StyledCellLabelProvider和ColumnLabelProvider之间的区别。重写ColumnLabelProvider的更新方法是不必要的。我这么做是为了表明这取决于课程。

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

It also works in the Snippet001TableViewer when you change the LabelProvider to a StyledCellLabelProvider and override the update-method similar to my example.

当您将LabelProvider更改为StyledCellLabelProvider并覆盖与我的示例类似的update-method时,它也可以在Snippet001TableViewer中工作。

#1


7  

It's windows bug/feature (see bugzilla for details), here is the proof (linux screenshot of your code)

它是windows bug/特性(详细信息请参见bugzilla),以下是证明(您的代码的linux屏幕截图)

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

I may be possible to workaround this bug/feature by self cell rendering (see Custom Drawing Table and Tree Items tutorial).

我可以通过自单元格渲染来解决这个bug/特性(参见自定义绘图表和树项目教程)。

#2


0  

I found a simple way to get the whole text shown. You have to use a StyledCellLabelProvider and override the update-method.

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

Here is a little example which shows the differences between a StyledCellLabelProvider and a ColumnLabelProvider. To override the update-method of ColumnLabelProvider is unnecessary. I did it to show that it depends on the class.

这里有一个小例子,说明了StyledCellLabelProvider和ColumnLabelProvider之间的区别。重写ColumnLabelProvider的更新方法是不必要的。我这么做是为了表明这取决于课程。

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

It also works in the Snippet001TableViewer when you change the LabelProvider to a StyledCellLabelProvider and override the update-method similar to my example.

当您将LabelProvider更改为StyledCellLabelProvider并覆盖与我的示例类似的update-method时,它也可以在Snippet001TableViewer中工作。