ボタン(Button)

ボタン(Button)

SWTのorg.eclipse.swt.widgets.Buttonクラスは、SwingのJButton、JCheckBox、 JRadioButton、JToggleButtonに対応します。スタイルを変更することで、以下のような様々な種類のボタンを作成できます。


通常のボタン



 

Button normalButton = new Button(parent, SWT.NONE);
normalButton.setText("NormalButton");
normalButton.setBounds(x, y, width, height);

フラットボタン



 

Button flatButton = new Button(parent, SWT.FLAT);
flatButton.setText("FlatButton");
flatButton.setBounds(x, y, width, height);

チェックボタン(チェックボックス)



 

Button checkButton = new Button(parent, SWT.CHECK);
checkButton.setText("CheckButton");
checkButton.setBounds(x, y, width, height);

ラジオボタン(グループなし)

   グループの指定をせずにラジオボタンを作成すると、同一のコンポジットに属するボタンに対して自動的に排他制御がかかります。画面内のラジオボタンに細かく排他制御を設定したい場合は、下記のラジオボタン(グループあり)を参照して下さい。




 

Button radioButton1 = new Button(parent, SWT.RADIO);
radioButton1.setText("100");
radioButton1.setBounds(x, y, width, height);

y += alignY;
Button radioButton2 = new Button(parent, SWT.RADIO);
radioButton2.setText("200");
radioButton2.setBounds(x, y, width, height);

ラジオボタン(グループあり)



 

⁄⁄ グループの生成
Group group = new Group(parent, SWT.NONE);
group.setBounds(10, 10, 120, 80);

// 親をグループに作成したグループを指定
Button radioButton3 = new Button(group, SWT.RADIO);
radioButton3.setText("300");
radioButton3.setBounds(10, 10, 100, 20);

Button radioButton4 = new Button(group, SWT.RADIO);
radioButton4.setText("400");
radioButton4.setBounds(10, 40 , 100, 20);

トグルボタン



 

Button toggleButton = new Button(parent, SWT.TOGGLE);
toggleButton.setText("ToggleButton");
toggleButton.setBounds(x, y, width, height);

テキスト(Text)

SWTのorg.eclipse.swt.widgets.Textクラスは、SwingのJTextField、JTextAreaに対応します。スタイルを変更することで、以下のような様々な種類のテキストを作成できます。

入力テキスト


 

Text inputText = new Text(parent, SWT.BORDER | SWT.NONE);
inputText.setBounds(x, y, width, height);

パスワードテキスト



 

Text passwordText = new Text(parent, SWT.BORDER | SWT.PASSWORD);
passwordText.setBounds(x, y, width, height);

スクロールテキスト



 

Text scrollableText = new Text(parent, SWT.BORDER | SWT.V_SCROLL | SWT.H_SCROLL);
scrollableText.setBounds(x, y, width, height * 4);

入力値のチェックや日本語入力の制御などもリスナーを使用して行います。たとえば数値だけ入力 できるテキストを作成する場合は、以下のようにorg.eclipse.swt.events.FocusAdapterクラスを継承したクラスを作成 し、addFocusListener(new TextFocusAdapter());でリスナーとして追加します。
 

⁄**
 * 数値のチェックを行うアダプタクラス
 *⁄
class TextFocusAdapter extends FocusAdapter {

    ⁄**
     * フォーカスが当たった場合の処理
     *⁄
    public void focusGained(FocusEvent e) {
        ⁄⁄ 数値のみの入力なので、IMEをOFFにする
        Display.getCurrent().getActiveShell().setImeInputMode(SWT.NONE);
    }

    ⁄**
     * フォーカスが外れた場合の処理
     *⁄
    public void focusLost(FocusEvent e) {
        Text source = (Text)e.getSource();
        String text = source.getText();

        if (text.length() != 0) {
            ⁄⁄ 数値以外が含まれた場合
            if (!RegexCheck.checkRegex("[0-9]*", text)) {
                source.setText("数値で入力してください");
            }
        }
     }

}

人気記事トップ10

人気記事ランキングをもっと見る

企画広告も役立つ情報バッチリ! Sponsored

サーバやOSの保守期限、VMware環境の見直しといった節目に考える、Oracle AI Databaseの次期基盤。オンプレ継続・AWS・OCIそれぞれの特性と留意点を整理するWebセミナー 11月19日 16:00 AIエージェントの“見えないリスク”に備えてアイデンティティを「関係」で統制する 11月17日 6:25 AI生成コードをどう安全に使うか――決定論的検証とAutoFixで築く品質保証ワークフロー 11月16日 6:30 デジタル主権時代、データとモデルの持ち方は企業が主体的に定める 11月13日 6:25 ClickHouse Cloudが実現した広告運用SaaS「ATOM」のデータ基盤革新、パフォーマンスを向上しながら、運用負荷とコストを削減 11月11日 6:20 AIで開発が加速する時代、品質保証はどう変わる? Tricentisが示すAIエージェント活用の実像 11月9日 6:45 悪用までの猶予が「1分」に迫るフロンティアAI時代に開発と本番を貫く脆弱性管理が必要 10月29日 8:30 欧州CRA対応とSBOM運用。AI生成コードのOSS混入問題の対策とは ――スニペット単位で検出する「FossID」「Insignary Clarity」によるSBOM運用 9月16日 6:40 PostgreSQLからTiDBへ、稼働中SaaSで進める段階的DB移行の実践と再設計への展望 9月7日 6:30 機能安全開発におけるGoogleTestの活用を支援!「Parasoft C/C++test CT」で実現する効率化&AI連携 4月14日 6:30