Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

SWT composite - redraw problem

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();
        }
    });

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 ?

like image 568
Amit Avatar asked Jun 21 '09 20:06

Amit


1 Answers

In the changeText() function, the

testCell.redraw();

line should be replaced by

testCell.layout();

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

shell.layout();.

like image 78
Csaba_H Avatar answered Nov 09 '22 01:11

Csaba_H