本文介绍了SWT TableViewer 标题高度的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在更改 SWT Tableviewer 的字体大小,但表格标题的高度没有改变,因此标签被切断.有什么办法可以解决这个问题吗?

I'm changing the fontsize of a SWT Tableviewer, but the height of the table-headers doesn't change, and thus the label is being cut off. Is there any way to fix this?

(这是在 Windows 上)

(This is on Windows)

推荐答案

Table Comes Under SWT 和另一个 TableViewer JFace.所以不清楚您是否需要TableViewerTable 的解决方案.此外,由于您没有任何代码片段,我选择为 JFaceTableViewer 演示它,尽管 Table 的概念将保持不变> 和 SWT.

Table Comes Under SWTand on the other TableViewer Comes Under JFace. So it is not clear whether you need a solution for TableViewer or Table. Also in absence of any code snippet from your side, I am choosing to demonstrate it for JFace and TableViewer, though the concept will remain same for Table and SWT.

>>设置字体前

>>设置字体后

您可以看到标题高度的差异.

You can see the difference in header heights.

>>代码

import org.eclipse.jface.viewers.*;
import org.eclipse.swt.SWT;
import org.eclipse.swt.events.*;
import org.eclipse.swt.graphics.*;
import org.eclipse.swt.layout.*;
import org.eclipse.swt.widgets.*;

public class TVHeaderTest 
{
    private class MyContentProvider implements IStructuredContentProvider {
        public Object[] getElements(Object inputElement) {
            return (MyModel[])inputElement;
        }
        public void dispose() {
        }
        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 "Item " + this.counter;
        }
    }

    private static Display display;

    public TVHeaderTest(final Shell shell) 
    {
        final Table table = new Table (shell, SWT.H_SCROLL|SWT.MULTI | SWT.BORDER | SWT.FULL_SELECTION|SWT.CHECK);
        table.setLinesVisible (true);
        table.setHeaderVisible (true);

        final String[] titles = {"!", "Description", "Resource", "In Folder", "Location"};
        for (int i=0; i<titles.length; i++) {
            TableColumn column = new TableColumn (table, SWT.NONE);
            column.setText (titles [i]);
            column.setWidth(100);
        }

        final TableViewer v = new TableViewer(table);
        v.setLabelProvider(new LabelProvider());
        v.setContentProvider(new MyContentProvider());


        MyModel[] model = createModel();
        v.setInput(model);
        v.getTable().setLinesVisible(true);


        Button button = new Button(shell, SWT.PUSH);
        button.setText("Set Font");

        // FOCUS ON THIS PART - START
        button.addSelectionListener(new SelectionListener() 
        {
            public void widgetSelected(SelectionEvent e) 
            {
                FontDialog d = new FontDialog(shell);
                FontData data = d.open();
                table.setFont(new Font(display, data));
                for (int i = 0; i < titles.length; i++) {
                    table.getColumn(i).pack();
                }

            }
            public void widgetDefaultSelected(SelectionEvent e) {
            }
        });
        // FOCUS ON THIS PART - END
        shell.pack();
    }

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

    public static void main(String[] args) 
    {
        display = new Display ();
        Shell shell = new Shell(display);
        shell.setLayout(new GridLayout());
        shell.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true));

        new TVHeaderTest(shell);
        shell.open ();

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

        display.dispose ();

    }

}

检查 //FOCUS ON THIS PART - START//FOCUS ON THIS PART - END 注释之间的代码.

Checkout the code between // FOCUS ON THIS PART - START and // FOCUS ON THIS PART - ENDcomments.

您也不能这样做来设置标题图像.请参阅表头图像错误

Also you can not do this for setting header image. See the Table Header Image Bug

这篇关于SWT TableViewer 标题高度的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-18 12:32