SWTウィジェットを使用したラジオボタンの選択に基づいて2つのテキストボックスを表示したいとします。しかし、別のラジオボタンを選択するときは、前のテキストボックスを非表示にしてドロップダウンを表示する必要があります。私はsetVisibility(true)とfalseを使用して機能を実現することができます。ここでの主な問題は、テキストボックスが表示されていないとき(2番目のラジオボタンの選択時)、それらのスペースが消費され、ドロップダウンがその下にくることです。私はそれほど多くのスペースを無駄にしたくなく、レイアウトを重複させて、同時に使用することができないので、割り当てられた共通のスペースを消費したくありません。Eclipseのラジオボタンに基づいて行のレイアウト構造を変更する方法SWT
1
A
答えて
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();
}
}
関連する問題
- 1. 内容に基づいてSWTレイアウトのサイズを変更(拡大/縮小)する方法
- 2. ラジオボタンの回答に基づいてフレームを変更する
- 3. D3ラジオボタンに基づいて色とスケールを変更
- 4. セルの値に基づいて行の背景色を変更する方法
- 5. 値に基づいてラジオボタン名を取得する方法
- 6. ラジオボタンの選択に基づいてサブミット時のフォーム動作を変更する
- 7. 変更、テキスト入力フィールドに基づいており、ラジオボタン
- 8. 別のTextBox変更イベントに基づいてTextBox値を変更する方法
- 9. プロジェクト構成に基づいてcsproj OutputTypeを変更する
- 10. Python、Tkinter、buttonclickに基づいてGUIを変更する方法
- 11. MVC3 - ドロップダウン選択の更新に基づいてレイアウトを変更します
- 12. EclipseからAndroid Studioへの移行後のプロジェクトファイル構造の変更gradle構造
- 13. EXTJSのいくつかの基準に基づいて行の背景色を変更する方法
- 14. DataGridViewのラジオボタンに基づいてフィルタデータ
- 15. サブタイプに基づいて動的にC++の構造体をキャスト
- 16. コンボボックスの選択に基づいてHTMLレイアウトを変更します
- 17. datetimeに基づいて属性のデータ構造が変更されていますか?
- 18. 別のページのコンテンツに基づいてイメージリンクを変更する方法
- 19. データベースの結果に基づいてフォントの色を変更する方法
- 20. xamlのErrorContentに基づいてValidation.ErrorTemplateを動的に変更する方法
- 21. 行のリストに基づいてテーブル内の行の順序を変更する方法id?
- 22. デバイスまたはホストに基づいてレイアウトを変更してください
- 23. WSDLファイルに基づいてSOAPヘッダーネームスペースを構築する方法
- 24. Androidのレイアウトに基づいてアクションバーを非表示にする方法
- 25. numpyで配列に基づいて行列を構築する方法は?
- 26. ケース式の条件に基づいて行を更新する方法
- 27. ハイチャートの値に基づいて動的な色を変更する方法
- 28. 以前のドロップダウンに基づいてドロップダウン名を変更する方法
- 29. テキストボックスの値に基づいてクラス名を変更する方法
- 30. selectlyputに基づいてグラフのy変数を変更する
ありがとうございますPhineas。私はこのアプローチを試してみるつもりです。あなたはどんな懸念の場合にもお知らせします:) –