2017-01-31 40 views
0

我有一個組合,在創建視圖時創建五個文本框。有一個按鈕,我想用組合框替換第四個文本框。問題是我無法從開始重新加載頁面,因爲我想顯示文本框的當前值。如何處置一個小部件並在SWT中創建另一個小部件來代替它?

這裏是一個片段。

public class DisposeDemo { 
    private static void addControls(Shell shell) { 
    shell.setLayout(new GridLayout()); 
    Text textOne=new Text(shell, SWT.BORDER); 
    Text textTwo=new Text(shell, SWT.BORDER); 
    Text textThree=new Text(shell, SWT.BORDER); 
    Text textFour=new Text(shell, SWT.BORDER); 
    Text textFive=new Text(shell, SWT.BORDER); 

    Button button = new Button(shell, SWT.PUSH); 
    button.setText("Replace text with Combo"); 

    button.addSelectionListener(new SelectionAdapter() { 
     @Override public void widgetSelected(SelectionEvent event) { 

     //What code should go here to Replace the textFour with a combo 

     } 
    }); 
    shell.pack(); 
    } 

    public static void main(String[] args) { 
    Display display = new Display(); 
    Shell shell = new Shell(display); 
    addControls(shell); 
    shell.open(); 
    while (!shell.isDisposed()) { 
     if (!display.readAndDispatch()) { 
     display.sleep(); 
     } 
    } 
    display.dispose(); 
    } 
} 

回答

0

而不是試圖取代控制(這是棘手)創建所有控件的開始,但使不可見的其中之一,從佈局中排除。隨後可以切換可見性。

當創建使用:

Text textFour = new Text(shell, SWT.BORDER); 
GridData data = new GridData(SWT.LEFT, SWT.CENTER, false, false); 
textFour.setLayoutData(textFour); 

Combo comboFour = new Combo(shell, ... style ...); 
comboFour.setVisible(false); // Not visible 
data = new GridData(SWT.LEFT, SWT.CENTER, false, false); 
data.exclude = true; // Exclude from layout 
comboFour.setLayoutData(data); 

要切換到具有組合可見:

textFour.setVisible(false); 
GridData data = (GridData)textFour.getLayoutData(); 
data.exclude = true; 

comboFour.setVisible(true); 
data = (GridData)comboFour.getLayoutData(); 
data.exclude = false; 

shell.layout(true); // Tell shell to redo the layout 
+0

實際上實時我有排列的具有複合的row.Each 5層複合材料的一種形式帶有一些小部件,如單個文本框/文本框/組合等。因此,只需單擊一個按鈕,我希望再次加載一個複合材料的內容,使其他複合材料靜態化。是否有任何解決此問題的方法? –

+0

對不起,我不明白。問一個新的問題更詳細的解釋。 –

+0

Sure.I將發佈一個新問題。 –

相關問題