Pregunta

Tengo un elemento compuesto, que inicialmente tiene una etiqueta. Ahora llamo a disponer en él (la etiqueta) y creo otra etiqueta en el mismo contenedor (olmo compuesto), pero no veo el nuevo texto. Me lleva a cuestionar cómo habilito el redibujo en el compuesto, de modo que la nueva etiqueta (o cualquier otro componente que pueda crear) se represente en lugar de la anterior. Aquí está el código que tengo (separado en una prueba unitaria para volver a dibujar un compuesto)

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

Como puede ver, estoy llamando a redibujar en el compuesto después de agregar un nuevo elemento. Además, he verificado que después de la llamada a eliminar, testCell.getChildren (). Length devuelve 0, como se esperaba, y cuando creo una nueva etiqueta, obtengo la misma expresión para devolver 1, verificando que el nuevo elemento realmente está obteniendo agregado a su contenedor compuesto principal ¿Me estoy perdiendo algo aquí?

¿Fue útil?

Solución

En la función changeText () , el

  

testCell.redraw();

la línea debe ser reemplazada por

  

testCell.layout();

O, si desea cambiar el tamaño correctamente, debe usar

  

shell.layout (); .

Otros consejos

Yo diría que agregue una selecciónListener en la etiqueta.

.addSelectionListener(new SelectionAdapter() {
    @Override
    public void widgetSelected(final SelectionEvent e) {
        //Change text by Label.setText();
    }
}
Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top