11 February 2013

Java Swing メインフレーム(JFrame)とダイアログ(JDialog)の実装

Java Swingツールキットを用いてメインフレーム(JFrame)とダイアログ(JDialog)の間でのデータの受け渡しと、モーダルダイアログの制御サンプル

■ 開発環境
・Eclipse 3.7.2
・Oracle Java 1.7
■ 実行環境
・Windows / Linux(Ubuntu)

20130211-swing-jdialog.jpg

■ メインフレームのクラス

extends JFrameを指定してJFrameクラスから派生させることで、コンストラクタで自動的にJFrameウインドウを作成する。

implements ActionListenerを実装することで、ボタン押下などのイベントをハンドリングできるようになる。ボタンを作成後btn_open_dlg.addActionListener(this);を実行して割り込みにフックする。actionPerformed関数の中で、どのエレメントから発生したイベントなのかを判定し、それぞれに合わせた処理を行う。

AppMain.class

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class AppMain extends JFrame implements ActionListener {
private static final long serialVersionUID = 1L;

// actionPerformed内で指定のボタンが押されたかどうか判定に使う
private JButton btn_ok;
private JButton btn_open_dlg;
private JTextField txtbox_02;

public static void main(String[] args) {
// コンストラクタ(JFrameウインドウの表示)
// スレッドを起動して直ちに戻る
new AppMain();
System.out.println("main entry function, exit");
}

// アクション(ボタンが押された場合)の割り込み
public void actionPerformed(ActionEvent e) {
if(e.getSource() == btn_ok){
this.setVisible(false);
this.dispose();
return;
}
else if(e.getSource() == btn_open_dlg){
dlgTestDlg dlg = new dlgTestDlg();
txtbox_02.setText(dlg.txt_02);
return;
}
}

// コンストラクタ(JFrameウインドウを表示する)
public AppMain() {
// ダイアログがモーダルを指定しても、このウインドウが使えるように設定
// this.setModalExclusionType(Dialog.ModalExclusionType.APPLICATION_EXCLUDE);
// 閉じるボタンの動作
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
// フレームのサイズ
this.setSize(450, 300);
// フレームのタイトル
this.setTitle("AppMain - Mainframe Window");

JLabel label_01 = new JLabel("モーダルダイアログで文字列入力とデータ受け渡し");
this.add(label_01, BorderLayout.NORTH);

JPanel panel_02 = new JPanel();
JLabel label_02 = new JLabel("ダイアログボックスで入力された文字列");
panel_02.add(label_02, BorderLayout.CENTER);
txtbox_02 = new JTextField("", 40);
panel_02.add(txtbox_02, BorderLayout.CENTER);
btn_open_dlg = new JButton("ダイアログを開く");
panel_02.add(btn_open_dlg, BorderLayout.CENTER);
btn_open_dlg.addActionListener(this);
this.add(panel_02, BorderLayout.CENTER);

btn_ok = new JButton("閉じる");
this.add(btn_ok, BorderLayout.SOUTH);
btn_ok.addActionListener(this);

// フレーム表示
this.setVisible(true);
}
}

■ ダイアログのクラス

ダイアログウインドウ自体の発するイベント、たとえば閉じるボタンを押した時などの割り込みを利用するために、implements WindowListenerを実装した上で、ダイアログウインドウ作成時にthis.addWindowListener(this);を実行して割り込みにフックする。今回の割り込みは複数の割り込み関数で処理され、下のソースコードでは緑で着色されたところ。

モーダルダイアログとするためにはthis.setModal(true);を指定する。

AppMain.class

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;

public class dlgTestDlg extends JDialog implements ActionListener,WindowListener {
private static final long serialVersionUID = 2L;

private JButton btn_ok;
private JButton btn_cancel;
private JTextField txtbox_02;
public String txt_02;

@Override
public void actionPerformed(ActionEvent e) {
// TODO Auto-generated method stub
if(e.getSource() == btn_ok){
txt_02 = txtbox_02.getText();
this.setVisible(false);
this.dispose();
return;
}
else if(e.getSource() == btn_cancel){
this.setVisible(false);
this.dispose();
return;
}
else {
System.out.println("other event");
}
}

public void windowOpened(WindowEvent e){
// ウインドウが新たに作られて開かれた時
System.out.println("windowOpened");
}
public void windowClosed(WindowEvent e){
// ウインドウ リソースが破棄された時
System.out.println("windowClosed");
}
public void windowClosing(WindowEvent e) {
// ウインドウ タイトルバーの「閉じる」アイコンを押した場合
// this.dispose();
System.out.println("windowClosing");
}
public void windowIconified(WindowEvent e){
System.out.println("windowIconified");
}
public void windowDeiconified(WindowEvent e){
System.out.println("windowDeiconified");
}
public void windowActivated(WindowEvent e){
// ウインドウが前面に出た場合
System.out.println("windowActivated");
}
public void windowDeactivated(WindowEvent e){
// ウインドウが後面に隠れた場合
System.out.println("windowDeactivated");
}


public dlgTestDlg() {
this.setModal(true);

this.setDefaultCloseOperation(JDialog.DO_NOTHING_ON_CLOSE);
// ウインドウが閉じられたら、リソースを自動的に解放する
// this.setDefaultCloseOperation(JDialog.DISPOSE_ON_CLOSE);
// フレームのサイズ
this.setSize(450, 300);
// フレームのタイトル
this.setTitle("dlgTestDlg - Dialog Window");

JLabel label_01 = new JLabel("入力ダイアログ");
this.add(label_01, BorderLayout.NORTH);

JPanel panel_02 = new JPanel();
JLabel label_02 = new JLabel("何か入力してください");
panel_02.add(label_02, BorderLayout.NORTH);
txtbox_02 = new JTextField("未入力", 40);
panel_02.add(txtbox_02, BorderLayout.SOUTH);
this.add(panel_02, BorderLayout.CENTER);

JPanel panel_03 = new JPanel();
btn_cancel = new JButton("キャンセル");
panel_03.add(btn_cancel, BorderLayout.EAST);
btn_cancel.addActionListener(this);
btn_ok = new JButton("OK");
panel_03.add(btn_ok, BorderLayout.WEST);
btn_ok.addActionListener(this);
this.add(panel_03, BorderLayout.SOUTH);

this.addWindowListener(this);
this.setVisible(true);
}
}