サプウィンドウを開いて、そこから親コンポーネントのプロパティを求める
戻る
多分もう少しスマートな方法があるはずです。
::::::::::::::
MyFrame.java
::::::::::::::
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.io.*;
/**
$Id: subwindow.html,v 1.1 2009/06/22 16:12:28 kishi Exp kishi $
*/
public class MyFrame extends JFrame {
public MyFrame() {
super( "サプウィンドウを開いて、そこから親コンポーネントのプロパティを求める" );
JButton button = new JButton( "サブウィンドウを開く" );
button.setForeground( Color.white );
button.setBackground( new Color( 0x9933cc ) );
this.add( button );
// アクションの設定
MyFrameAction action = new MyFrameAction();
action.setParentFrame( this );
button.setActionCommand( "BUTTON_CLICKED" );
button.addActionListener( action );
}
public static void main( String[] args ) {
MyFrame frame = new MyFrame();
/* 終了処理を追加 */
frame.addWindowListener( new WindowAdapter() {
public void windowClosing( WindowEvent e ) {
System.exit( 0 );
}
}
);
/** サイズと位置を指定 */
frame.setBounds( 0, 0, 600, 300 );
frame.setVisible( true );
// frame.pack();
}
}
::::::::::::::
MyFrameAction.java
::::::::::::::
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.io.*;
/**
$Id: subwindow.html,v 1.1 2009/06/22 16:12:28 kishi Exp kishi $
*/
public class MyFrameAction extends AbstractAction {
private JFrame parentFrame = null;
public MyFrameAction() {
super();
}
public void setParentFrame( JFrame parentFrame ) {
// めんどくさいので直接引き渡す
this.parentFrame = parentFrame;
}
public void actionPerformed( ActionEvent event ) {
String eventName = event.getActionCommand();
// 実行されたコマンドをダンプします
System.out.println( eventName );
// イベント発生源の情報をダンプする
System.out.println();
System.out.println( event.getSource() );
// イベント発生源のクラスを表示する
System.out.println();
Object source = event.getSource();
System.out.println( source.getClass().getName() );
// 親オブジェクトを辿っていく
System.out.println();
JComponent component = ( JComponent ) event.getSource();
Container parent = component.getParent();
int i = 0;
while ( parent != null ) {
System.out.println( ++i + ": " + parent );
parent = parent.getParent();
}
if ( "BUTTON_CLICKED".equals( eventName ) ) {
// System.exit( 0 );
// 必ずサブウィンドウは1個しかオープンしないようにシングルトンにしておく
// 親フレームの参照を引き渡す
SubSingletonFrame subwin = SubSingletonFrame.getInstance( parentFrame );
}
}
}
::::::::::::::
SubSingletonFrame.java
::::::::::::::
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.io.*;
/**
$Id: subwindow.html,v 1.1 2009/06/22 16:12:28 kishi Exp kishi $
*/
public class SubSingletonFrame extends JFrame {
private static SubSingletonFrame uniqueInstance = null;
private SubSingletonFrame() {
super();
}
public static SubSingletonFrame getInstance( JFrame parentFrame ) {
if ( uniqueInstance == null ) {
uniqueInstance = new SubSingletonFrame();
/** 親フレームのタイトルをそのまま表示 */
uniqueInstance.setTitle( parentFrame.getTitle() );
/** サイズと位置を指定 */
uniqueInstance.setBounds( 400, 300, 400, 200 );
uniqueInstance.setVisible( true );
uniqueInstance.addWindowListener( new WindowAdapter() {
public void windowClosing( WindowEvent e ) {
// インスタンスも消滅させる
uniqueInstance = null;
}
}
);
} else {
System.err.println( "既にサブウィンドウがオープンしています!" );
}
return uniqueInstance;
}
}
戻る