简体   繁体   English

为什么Java SWT Selection侦听器中新创建的元素不会显示/触发绘制事件?

[英]Why do newly created elements in a Java SWT Selection listener not get displayed/trigger a paint event?

I've got a problem with SWT (standard widget toolkit) and redrawing. 我遇到了SWT(标准小部件工具包)和重绘问题。 Basically I want something to happen when I click a button eg new elements to appear. 基本上我想要点击按钮时会发生一些事情,例如要出现的新元素。 For the sake of simplicity let it be text here. 为了简单起见,请将其作为文字。

However when I click the button nothing happens, no element appears, no PaintEvent is triggered. 但是,当我单击按钮时没有任何反应,没有元素出现,没有触发PaintEvent。 When I resize the window, suddenly all of the elements are there (because that triggers a paintEvent/redrawing). 当我调整窗口大小时,突然所有元素都存在(因为它会触发paintEvent /重绘)。

The docs and blog posts tell me that a paint event is triggered whenever something needs to be redrawn. 文档和博客文章告诉我,只要需要重新绘制某些内容,就会触发绘制事件。 I thought that adding a new element to an application would fall into that category. 我认为向应用程序添加新元素将属于该类别。

So the question is: Am I doing something wrong here? 所以问题是:我在这里做错了吗? Am I supposed to call layout()/redraw() by hand to trigger the redraw? 我应该手动调用layout()/ redraw()来触发重绘吗? That seems tedious to me and to my understanding was that SWT handles that for me for the most cases. 这对我来说似乎很乏味,而且我的理解是SWT在大多数情况下为我处理这个问题。

Oh I'm running Linux x64 with SWT 4.2.2. 哦,我正在使用SWT 4.2.2运行Linux x64。 But we have this on pretty much any platform (and also on the old SWT 3.7). 但是我们在几乎任何平台上都有这个(以及旧的SWT 3.7)。

And here goes my little example code: 这里是我的小示例代码:

import org.eclipse.swt.*;
import org.eclipse.swt.widgets.*;
import org.eclipse.swt.events.*;
import org.eclipse.swt.layout.*;

public class MainExample {

    public static void createText(Shell shell) {
      Text text = new Text(shell, SWT.SINGLE);
      text.setText("here is some fancy text");
    }

    public static void main (String [] args) {
        Display display = new Display ();
        Shell shell = new Shell (display);
        Button button = new Button(shell, SWT.PUSH);
        button.setText("I am a button");

        button.addSelectionListener(new SelectionAdapter() {
            public void widgetSelected(SelectionEvent e) {
                System.out.println("Button Clicked");
                // Why do these texts not show up and not trigger a Paint Event?
                createText(e.display.getActiveShell());
            }
        });

        createText(shell);
        shell.setLayout (new RowLayout());
        shell.addPaintListener(new PaintListener() {
            public void paintControl(PaintEvent e) {
                // just triggered in the beginning and with resizes
                System.out.println("Hooray we got a Paint event");
            }
        });
        shell.open();

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

}

Please keep in mind, that this is not purely about making that example just work. 请记住,这并非纯粹是为了让这个例子正常工作。 This is a simple version of a problem for a much bigger project :-) 对于更大的项目,这是一个简单版本的问题:-)

Any help is well appreciated! 任何帮助都非常感谢!

If you using layout, you should call Composite.layout() (or layout(true)) after add a new child to composite. 如果使用布局,则应在将新子项添加到复合后调用Composite.layout()(或layout(true))。 Layout do not know (so do not places widget) until you call layout(). 在调用layout()之前,布局不知道(因此不要放置小部件)。

By the way, if you resize the shell(composite), layout automatically invalidates its internal caches and checks all the children, places them. 顺便说一下,如果你调整shell(复合)的大小,布局会自动使其内部缓存无效并检查所有子节点,放置它们。 that's why newly created text appears after you resized the shell. 这就是为什么在调整shell大小后会出现新创建的文本的原因。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM