SWT复合 - 重绘问题 [英] SWT composite - redraw problem

查看:404
本文介绍了SWT复合 - 重绘问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个复合元素,最初有一个Label。现在我调用dispose(标签)并在同一个容器(复合榆树)中创建另一个标签,但我没有看到新文本。它让我质疑如何在复合上启用重绘,以便新标签(或我可能创建的任何其他组件)将代替旧标签。
这是我的代码(分成重组复合的单元测试)

I have a composite element, that initially has a Label. Now I call dispose on the it (the label) and create another label in the same container (composite elm), but I don't see the new text. It brings me to question how do I enable redraw on the composite, so that the new label (or any other component I might create) will render in place of the old one. Here is the code I have (separated into a unit test for redraw a composite)

private Label createLabel( Composite parent) {
    Label label = new Label(parent, SWT.NONE);
    label.setAlignment(SWT.CENTER);
    label.setLayoutData( new GridData( SWT.CENTER, SWT.CENTER, true, true) );
    return label;
}

private void changeText() {
    assert testCell != null : "Please initialize test cell";
    testCell.getChildren()[0].dispose();
    Label l = createLabel(testCell);
    l.setText("New TexT");
    testCell.redraw();
}

private void draw() {
    Display display = new Display();
    shell = new Shell(display);
    shell.setLayout(new GridLayout(2,false));

    testCell = new Composite(shell,SWT.BORDER);
    testCell.setLayout(new GridLayout());
    Label l = createLabel(testCell);
    l.setText("Old Text");
    Composite btnCell = new Composite(shell,SWT.NONE);
    btnCell.setLayout(new GridLayout());
    Button b = new Button(btnCell, SWT.PUSH);
    b.setText("Change");
    b.addListener(SWT.MouseDown, new Listener() {
        public void handleEvent(Event e) {
            changeText();
        }
    });

正如您所看到的,我在添加新元素后在复合材料上调用redraw。另外,我已经验证在调用dispose之后,testCell.getChildren()。length按预期返回0,当我创建一个新标签时,我得到相同的表达式返回1,验证新元素确实得到了添加到其父复合容器
我在这里遗漏了什么?

As you can see, I am calling redraw on the composite after I add a new element. Also, I have verified that after the call to dispose, testCell.getChildren().length returns 0, as expected, and when I create a new label, I get the same expression to return 1, verifying that the new element is indeed getting added to its parent composite container Am I missing something here ?

推荐答案

changeText中()函数,


testCell.redraw();

行应替换为


testCell.layout();

或者,如果你想要要正确调整大小,你应该使用

Or, if you want to resize it correctly you should use


shell.layout();

这篇关于SWT复合 - 重绘问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆