Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/321.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 空表/树上的SWT自定义行高_Java_Swt - Fatal编程技术网

Java 空表/树上的SWT自定义行高

Java 空表/树上的SWT自定义行高,java,swt,Java,Swt,我需要为我的表格/树行设置自定义高度。当我的表中有项目时,监听SWT.MeasureItem事件会起作用,但当我的表为空时,它就不起作用了。 有什么想法吗? 提前谢谢 viewer.getTree().addListener(SWT.MeasureItem, new Listener() { public void handleEvent(Event event) { event.height = 30; } }); 您是要在表格行上设置高度,还是在表格

我需要为我的表格/树行设置自定义高度。当我的表中有项目时,监听SWT.MeasureItem事件会起作用,但当我的表为空时,它就不起作用了。 有什么想法吗? 提前谢谢

viewer.getTree().addListener(SWT.MeasureItem, new Listener() {
     public void handleEvent(Event event) {
        event.height = 30;
     }
  });

您是要在表格行上设置高度,还是在表格本身上设置高度

如果您试图设置表行的高度,
SWT.MeasureItem
事件不会被激发,除非有要测量的项-因此不会为空表激发它们。请参阅(看起来有点可疑)

在内部,
有一个
setItemHeight(int)
,但您需要使用反射来访问它,因为它受包保护。是一种非常有用的高级语言功能,特别是当您需要支持多个版本的Java运行时,或多个版本的SWT(其中添加了新方法或删除了旧方法)时。您可以动态查询类、方法、字段等的可用性,并仅在它们存在时调用它们。此外,您还可以访问调用方通常保护的方法和字段。我不建议经常使用反射,但在没有其他选择的情况下使用反射是很好的

很好地解释了通常如何调用私有方法,但下面是一个获取
setItemHeight
方法的方法签名并调用它的具体示例:

final Table table = new Table(parent, SWT.BORDER);

/* Set up table columns, etc. */

table.pack();

try
{
    /*
     * Locate the method setItemHeight(int). Note that if you do not
     * have access to the method, you must use getDeclaredMethod(). If
     * setItemHeight(int) were public, you could simply call
     * getDeclaredMethod.
     */
    Method setItemHeightMethod =
        table.getClass().getDeclaredMethod("setItemHeight", int.class);

    /*
     * Set the method as accessible. Again, this would not be necessary
     * if setItemHeight(int) were public.
     */
    setItemHeightMethod.setAccessible(true);

    /*
     * Invoke the method. Equivalent to table.setItemHeight(50).
     */
    setItemHeightMethod.invoke(table, 50);
}
catch (Exception e)
{
    /*
     * Reflection failed, it's probably best to swallow the exception and
     * degrade gracefully, as if we never called setItemHeight.  Maybe
     * log the error or print the exception to stderr?
     */
    e.printStackTrace();
}

然而,如果你真的只是想设置桌子本身的高度,那么最好使用你的布局。例如,为
GridLayout

设置
GridData.heightHint
,对于包含通过
ColumnLabelProvider
方法
getImage
提供的图像的表,我遇到了相同的问题。我想要更高的排。
我注意到,您使用的图像的最大高度设置了表格行的高度,因此一个相当愚蠢但有效的解决方法是使用更高的图像,向其添加一些透明区域。

您好,抱歉,我不够清楚,我正在尝试设置表格行的高度。我想我可以为表使用一个虚拟输入,然后将输入重置为空作为一种解决方法,但我真的希望我能找到一个更优雅的解决方案。我明白了。我认为添加(然后删除)一些输入将保留行中的高度,但我认为需要绘制才能调用度量项目侦听器,在这种情况下,可能会闪烁。我想,如果标签装饰器返回空字符串,它将最小化这一点,但这似乎仍然是一个不太理想的解决方案。您是否愿意通过反射调用
表.setItemHeight(int)
?(我不确定这是不是或多或少的黑客行为。)对不起,我以前从未使用过反射,所以我真的不知道你的意思。