12

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 ?

Amit
  • 805
  • 1
  • 8
  • 11

2 Answers2

27

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

Csaba_H
  • 8,215
  • 1
  • 41
  • 42
  • 2
    Sorry for reviving this topic, but i was recently seeking for solution of the similar problem and this topic helped a lot but to resize my window i had to use `shell.pack()` because `shell.layout()` didn't do anything. May be it'll be helpful for someone. :) – mykola Mar 21 '12 at 13:37
  • @mykola: `shell.pack()` made my window smaller and redrawed it, but `shell.layout()` redrawed it and kept its size. I was adding controls to a window after `shell.open()` and the controls weren't showing up. `shell.layout()` solved it. – gregn3 Dec 03 '13 at 19:29
  • 1
    Given the javadoc on layout(), `testCell.requestLayout();` would even be better – titou10 Apr 19 '17 at 00:01
-5

I would say add a selectionListener on the label.

.addSelectionListener(new SelectionAdapter() {
    @Override
    public void widgetSelected(final SelectionEvent e) {
        //Change text by Label.setText();
    }
}
Sergey Glotov
  • 20,200
  • 11
  • 84
  • 98