SQLの窓

2018年06月20日


WindowsBuilder の JFaceで アプリケーションウインドウとダイアログを使用した基本処理

WindowsBuilder のインストールとプロジェクトの作成

▼ 以下を参照して下さい
Pleiades All in One で、Windows アプリを作成する手順( WindowBuilder + Swing デザイナー or SWT デザイナー[JFace] )

ApplicationWindow を中央に表示
	@Override
	protected Point getInitialLocation(Point initialSize) {

		GraphicsEnvironment env = GraphicsEnvironment.getLocalGraphicsEnvironment();

		// デスクトップのサイズ
		Point windowSize = new Point(
				env.getMaximumWindowBounds().width,
				env.getMaximumWindowBounds().height);
		Point position = new Point(0,0);

		// デスクトップの中心に表示します
		return new Point(
			position.x + ((windowSize.x - initialSize.x) / 2),
			position.y + ((windowSize.y - initialSize.y) / 2)
		);
	}

ダイアログの呼び出し

呼び出し先から、こちら側のコントロールにアクセスする為、識別する為に名前を setData でセットしています
		Button btnNewButton = new Button(container, SWT.NONE);
		btnNewButton.addSelectionListener(new SelectionAdapter() {
			@Override
			public void widgetSelected(SelectionEvent e) {
				Dialog1 dialog = new Dialog1(getShell());
				int result = dialog.open();
				System.out.println(result);
			}
		});
		btnNewButton.setBounds(10, 10, 75, 25);
		btnNewButton.setText("New Button");
		btnNewButton.setData("name","Button1");



ダイアログを親ウインドウの中央に
	@Override
	protected Point getInitialLocation(Point initialSize) {

		Point windowSize = this.getShell().getParent().getSize();
		Point position = this.getShell().getParent().getLocation();

		return new Point(
			position.x + ((windowSize.x - initialSize.x) / 2),
			position.y + ((windowSize.y - initialSize.y) / 2)
		);
	}



ダイアログから親ウインドウにアクセス

対症療法なので正確では無いですが、parentShell.getChildren() の内容にもう少しチェックを追加すれば使用に問題は無いと思います。コンストラクタにウインドウのインスタンスを渡せば簡単ですが、汎用性はこちらのほうが勝ると思います。
	@Override
	protected void createButtonsForButtonBar(Composite parent) {
		Button button = createButton(parent, IDialogConstants.OK_ID, IDialogConstants.OK_LABEL, true);
		button.addSelectionListener(new SelectionAdapter() {
			@Override
			public void widgetSelected(SelectionEvent e) {

				Shell parentShell = Dialog1.this.getParentShell();
				Control[] controls = parentShell.getChildren();
				Composite con = (Composite)controls[1];

				controls = con.getChildren();

				for( Control control: controls ) {
					Object name = control.getData("name");
					if ( name != null) {
						if ( name.toString().equals("Button1") ) {
							Button btn = (Button) controls[0];
							btn.setText("ボタン");
							break;
						}
					}
				}

				Dialog1.this.setReturnCode(Dialog.OK);
				Dialog1.this.close();

			}
		});
		Button button_1 = createButton(parent, IDialogConstants.CANCEL_ID, IDialogConstants.CANCEL_LABEL, false);
		button_1.addSelectionListener(new SelectionAdapter() {
			@Override
			public void widgetSelected(SelectionEvent e) {
				Dialog1.this.setReturnCode(Dialog.CANCEL);
				Dialog1.this.close();
			}
		});
	}





posted by lightbox at 2018-06-20 20:02 | Pleiades | このブログの読者になる | 更新情報をチェックする
container 終わり



フリーフォントで簡単ロゴ作成
フリーフォントでボタン素材作成
フリーフォントで吹き出し画像作成
フリーフォントではんこ画像作成
ほぼ自由に利用できるフリーフォント
フリーフォントの書体見本とサンプル
画像を大きく見る為のウインドウを開くボタンの作成

CSS ドロップシャドウの参考デモ
イラストAC
ぱくたそ
写真素材 足成
フリーフォント一覧
utf8 文字ツール
右サイド 終わり
base 終わり