【问题标题】:SWT TableViewer Header-HeightSWT TableViewer 标题高度
【发布时间】:2011-04-27 14:59:47
【问题描述】:

我正在更改 SWT Tableviewer 的字体大小,但表头的高度没有改变,因此标签被切断了。有没有办法解决这个问题?

(这是在 Windows 上)

【问题讨论】:

    标签: java winapi swt


    【解决方案1】:

    Table 降级 SWT和另一个TableViewer 降级 JFace。所以不清楚您是否需要TableViewerTable 的解决方案。同样,在您没有任何代码 sn-p 的情况下,我选择为JFaceTableViewer 演示它,尽管TableSWT 的概念将保持不变。

    >> 设置字体之前

    >>设置字体后

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

    >> 代码

    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 - ENDcmets 之间的代码。

    您也不能这样做来设置标题图像。见Table Header Image Bug

    【讨论】:

    • 感谢您的回复!是的,我指的是 JFace 中的 TableViewer(我不知道 JFace 和 SWT 之间有区别)。从我可以看到你的代码和我的代码一样,使用 .setFont()。但是在我的机器(Windows XP)上,标题的高度保持不变。当我运行您的代码时也会发生这种情况:abload.de/image.php?img=swttableviewerfzk5.png - 似乎是 Windows 问题。知道如何解决或解决此问题吗?
    • @Dexter:是的,这似乎是与操作系统相关的问题。根据我发布的 Bug 链接:This is a platform limitation. Windows won't grow the height of the header。众所周知,SWT 可以直接访问 OS 小部件。所以windows的限制扩展到了SWT。虽然,评论是在 2006 年发布的,但我最好的猜测是它是针对 XP 的。作为解决方法,请查看 sourceforge.net/projects/ktableeclipse.org/nebula/widgets/grid/grid.php
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-12-28
    • 1970-01-01
    • 1970-01-01
    • 2010-11-27
    • 1970-01-01
    相关资源
    最近更新 更多