2012-03-31 7 views
1

SWTウィジェットを使用したラジオボタンの選択に基づいて2つのテキストボックスを表示したいとします。しかし、別のラジオボタンを選択するときは、前のテキストボックスを非表示にしてドロップダウンを表示する必要があります。私はsetVisibility(true)とfalseを使用して機能を実現することができます。ここでの主な問題は、テキストボックスが表示されていないとき(2番目のラジオボタンの選択時)、それらのスペースが消費され、ドロップダウンがその下にくることです。私はそれほど多くのスペースを無駄にしたくなく、レイアウトを重複させて、同時に使用することができないので、割り当てられた共通のスペースを消費したくありません。Eclipseのラジオボタンに基づいて行のレイアウト構造を変更する方法SWT

答えて

0

私は、好奇心のためにそれを達成する方法について考えていました。だから私はあなたのためだけに99行のコードでSSCCEを作成しました。奇妙な振る舞いを避けるための基本的な考え方は、Compositeをサブクラス化してそこに出現するウィジェットを配置することです。したがって、3つの方法が必要です:最初のウィジェットのセットを作成するメソッド、2つ目のウィジェットのセットを作成するメソッド、およびすべてを消去するメソッドです。ウィジェットを操作した後、変更を有効にするにはlayoutを呼び出します。

package test; 

import org.eclipse.swt.SWT; 
import org.eclipse.swt.widgets.*; 
import org.eclipse.swt.layout.*; 
import org.eclipse.swt.events.*; 

public class Foo { 
    Shell shell; 
    Button button1, button2; 
    MyComposite composite; 

    public Foo() { 
     Display display = new Display(); 
     shell = new Shell(display); 
     shell.setLayout(new GridLayout(1, false)); 

     button1 = new Button(shell, SWT.RADIO); 
     button1.setText("Button 1"); 
     button1.setSelection(true); 
     button1.addSelectionListener(new ButtonListener()); 

     button2 = new Button(shell, SWT.RADIO); 
     button2.setText("Button 2"); 

     composite = new MyComposite(); 
     composite.createTexts(); 

     shell.open(); 
     shell.pack(); 

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

    /** 
    * A custom composite for displaying either two Combos or two Texts 
    */ 
    class MyComposite extends Composite { 
     /** Combos */ 
     Combo combo1, combo2; 

     /** Texts */ 
     Text text1, text2; 

     MyComposite() { 
      super(shell, SWT.NONE); 
      setLayout(new GridLayout(2, true)); 
     } 

     /** create the combos */ 
     void createCombos() { 
      combo1 = new Combo(this, SWT.DROP_DOWN); 
      combo1.setText("foo"); 
      combo2 = new Combo(this, SWT.DROP_DOWN); 
      combo2.setText("bar"); 
     } 

     /** create the texts */ 
     void createTexts() { 
      text1 = new Text(this, SWT.SINGLE | SWT.BORDER); 
      text1.setText("foo"); 
      text2 = new Text(this, SWT.SINGLE | SWT.BORDER); 
      text2.setText("bar"); 
     } 

     /** dispose all children */ 
     void disposeAll() { 
      for(Widget w : getChildren()) { 
       w.dispose(); 
      } 
     } 
    } 

    class ButtonListener extends SelectionAdapter { 
     @Override 
     public void widgetSelected(SelectionEvent e) { 
      // erase all 
      composite.disposeAll(); 

      // re-add widgets according to radio button state 
      if(button1.getSelection()) { 
       composite.createTexts(); 
      } 
      else { 
       composite.createCombos(); 
      } 

      // re-do layout and fit the shell 
      shell.layout(); 
      shell.pack(); 
     } 
    } 

    public static void main(String[] args) { 
     new Foo(); 
    } 
} 
+0

ありがとうございますPhineas。私はこのアプローチを試してみるつもりです。あなたはどんな懸念の場合にもお知らせします:) –

関連する問題