簡體   English   中英

SWT JFace moveBelow()之后如何重繪

[英]SWT JFace how to redraw after moveBelow()

我有一個視圖,其中顯示了元素,具體取決於以前的選擇。 dispose()和parent.layout()確實消除了預期的元素。 問題是,我如何找回他們? 我想,dispose()不會刪除元素,只是將其從元素列表中刪除以進行渲染。 那么,我該如何找回?

提前感謝,馬庫斯


編輯:

剛剛發現,dispose()釋放了對象並遞歸地將其作為子對象,因此,一旦將它們處理掉,就必須重新創建它們? 如果是這樣,如何在GridLayout中找到位置?


編輯(2):

根據下面的答案,我已經嘗試過了:

 public class TestTab extends Composite { org.eclipse.swt.layout.GridLayout layout = null; GridData griddata = null; Button upperButton = null; Text textInTheMiddle = null; Button lowerButton = null; public InvoiceTab(Composite parent, int arg2) { super(parent, arg2); final Composite myParent = parent; layout = new org.eclipse.swt.layout.GridLayout(GridData.FILL_BOTH, false); layout.numColumns = 1; this.setLayout(layout); upperButton = new Button(this, SWT.PUSH); upperButton.setText("upper Button"); textInTheMiddle = new Text(this, SWT.BORDER); lowerButton = new Button(this, SWT.PUSH); lowerButton.setText("lower Button"); upperButton.addSelectionListener(new SelectionAdapter() { public void widgetSelected(SelectionEvent e) { if (textInTheMiddle.isVisible()) { textInTheMiddle.setVisible(false); lowerButton.moveBelow(upperButton); } else { textInTheMiddle.setVisible(true); lowerButton.moveBelow(textInTheMiddle); } myParent.layout(); } }); } } 

文本字段gest符合預期,但是下部的按鈕不會更改位置? 再次感謝您的任何想法...

是的, dispose()釋放對象,並遞歸將其作為子對象 因此,您必須重新創建已處置的控件/小部件。 在像您這樣的情況下,建議隱藏控件並重新布局您的復合材料/容器。 有關示例, 請參見此處

代碼!

重要的是GridData::exclude屬性的用法。

import org.eclipse.swt.SWT;
import org.eclipse.swt.events.SelectionAdapter;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.widgets.Text;

class TestTab extends Composite 
{
    Button upperButton = null;
    Text textInTheMiddle = null;
    Button lowerButton = null;

    boolean state = true;

    public TestTab(Composite parent, int style) 
    {
        super(parent, style);


        GridLayout layout = new GridLayout(1, false);
        setLayout(layout);


        upperButton = new Button(this, SWT.PUSH);
        upperButton.setText("upper Button");

        textInTheMiddle = new Text(this, SWT.BORDER);
        GridData data = new GridData(SWT.FILL, SWT.FILL, true, false);
        data.exclude = false;
        textInTheMiddle.setLayoutData(data);

        lowerButton = new Button(this, SWT.PUSH);
        lowerButton.setText("lower Button");

        upperButton.addSelectionListener(new SelectionAdapter() 
        {
            public void widgetSelected(SelectionEvent e) {


                GridData griddata = (GridData) textInTheMiddle.getLayoutData();
                griddata.exclude = state;
                textInTheMiddle.setVisible(!state);
                textInTheMiddle.getParent().layout(false);
                state = !state;
            }
        });
    }   

}
public class Test
{
    public static void main(String[] args) 
    {
        Display display = new Display();
        Shell shell = new Shell(display);
        shell.setLayout(new GridLayout(1, false));

        new TestTab(shell, SWT.NONE);

        shell.open();
        while (!shell.isDisposed()) {
            if (!display.readAndDispatch())
                display.sleep();
        }
        display.dispose();
    }
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM