SWT composite - redraw problem

25,076

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

Share:
25,076

Related videos on Youtube

Amit
Author by

Amit

Updated on January 12, 2021

Comments

  • Amit
    Amit over 3 years

    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 ?

  • mykola
    mykola about 12 years
    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. :)
  • gregn3
    gregn3 over 10 years
    @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.
  • titou10
    titou10 about 7 years
    Given the javadoc on layout(), testCell.requestLayout(); would even be better