Java Swing ポップアップメニュー


ブラウザでダウンロード
AppPopup.java
sun の Examples を元に作成しました
アプリケーション固有のクラスとして、ポップアップメニュー
ばかりを記述するという仕様です。

ここでは、メインウインドウのポップアップメニューとして記述していますが、
メインウインドウは、AppWindow => BaseWindow => JFrame という 継承関係になっています

class AppPopupListener extends MouseAdapter は、共通で使用しますが、
コンテナやコントロール毎に追加用メソッドを記述していきます

↓例えばこんな感じ
public void addPopupJButton( JButton target )
package action;

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

// *****************************************************
// ポップアップメニューを実装する
// アプリケーション固有のクラス
// *****************************************************
public class AppPopup {

	// *****************************************************
	// コンストラクタ
	// *****************************************************
	public AppPopup() {

	}

	// *****************************************************
	// メインウインドウ用のポップアップメニューの実装
	// *****************************************************
	public void addPopupJFrame( AppWindow target ) {

		JPopupMenu popup = new JPopupMenu();
		JMenuItem menuItem;

		// *****************************************************
		// ポップアップメニュー1
		// *****************************************************
		menuItem = new JMenuItem("メニュー項目1");
		menuItem.setFont(new Font("MS Pゴシック", 0, 12));
		menuItem.addActionListener(new java.awt.event.ActionListener() {
			public void actionPerformed(java.awt.event.ActionEvent e) {

				System.out.println("メニュー1がクリックされました");

				try {

				}
				catch ( Exception ex ) {
					ex.printStackTrace();
				}

			}
		});
		popup.add(menuItem);

		// *****************************************************
		// セパレータと階層メニュー
		// *****************************************************
		popup.addSeparator();
		JMenu jm = new JMenu("階層メニュー");
		jm.setFont(new Font("MS Pゴシック", 0, 12));
			menuItem = new JMenuItem("階層内のメニュー");
			menuItem.setFont(new Font("MS Pゴシック", 0, 12));
			menuItem.addActionListener(new java.awt.event.ActionListener() {
				public void actionPerformed(java.awt.event.ActionEvent e) {

					System.out.println("階層内のメニューがクリックされました");

					try {

					}
					catch ( Exception ex ) {
						ex.printStackTrace();
					}

				}
			});
			jm.add(menuItem);
		popup.add(jm);

		// *****************************************************
		// ポップアップメニュー2
		// *****************************************************
		menuItem = new JMenuItem("メニュー項目2");
		menuItem.setFont(new Font("MS Pゴシック", 0, 12));
		menuItem.addActionListener(new java.awt.event.ActionListener() {
			public void actionPerformed(java.awt.event.ActionEvent e) {

				System.out.println("メニュー2がクリックされました");

				try {

				}
				catch ( Exception ex ) {
					ex.printStackTrace();
				}

			}
		});
		popup.add(menuItem);

		// *****************************************************
		// メインウインドウに実装
		// *****************************************************
		target.addMouseListener(new AppPopupListener(popup));


	}

	// *****************************************************
	// ポップアップを表示する為のクラス
	// *****************************************************
	class AppPopupListener extends MouseAdapter {

		JPopupMenu popup;

		AppPopupListener(JPopupMenu popupMenu) {
			this.popup = popupMenu;
		}

		public void mousePressed(MouseEvent e) {
			this.doPopup(e);
		}

		public void mouseReleased(MouseEvent e) {
			this.doPopup(e);
		}

		private void doPopup(MouseEvent e) {
			if (e.isPopupTrigger()) {
				this.popup.show(e.getComponent(),
					e.getX(), e.getY());
			}
		}
	}
}
AppWindow.java
コード実装の基本は Exlipse + Visual Editor です。
オリジナルをよりシンプルに変更してあります

他のコントロールにもポップアップメニューを追加したい場合は、
popup.addPopupJFrame( this ); の次に追加していくと良いでしょう。

↓例えばこんな感じ
popup.addPopupJButton( jButton );


ポップアップメニューのイベントの書き方は、Exlipse + Visual Editor の ボタンのクリックイベントと同じです。
package action;

import javax.swing.*;
import java.awt.*;
import action.*;
import java.io.*;

public class AppWindow extends BaseWindow {

	private JPanel jContentPane = null;
	private JButton jButton = null;

	public int mainWidth = 300;
	public int mainHeight = 200;
	public String titleString = "Swing サンプル作成用バッチビルドパッケージ";

	// *****************************************************
	// ボタン作成とクリックイベント
	// *****************************************************
	private JButton getJButton() {
		if (jButton == null) {
			jButton = new JButton();
			// メインウインドウに対して、100x30 のボタンを追加
			jButton.setBounds(
				new Rectangle(
					(mainWidth/2)-50, ((mainHeight-40)/2)-15,
					100, 30
			)
			);
			jButton.setText("実行");
			jButton.addActionListener(new java.awt.event.ActionListener() {
				public void actionPerformed(java.awt.event.ActionEvent e) {

					System.out.println("ボタンがクリックされました");

					try {
						// ボタン処理用クラスの呼び出し
						Object param[] = {AppWindow.this};
						Check.testProc(param);

					}
					catch ( Exception ex ) {
						ex.printStackTrace();
					}

				}
			});
		}
		return jButton;
	}

	// *****************************************************
	// コンストラクタ
	// *****************************************************
	public AppWindow() {
		super();
		initialize();
	}

	// *****************************************************
	// 初期処理
	// *****************************************************
	private void initialize() {
		// ウインドウサイズの決定
		this.setSize(mainWidth, mainHeight);

		// ウインドウ位置の変更
		centerWindow(-200);

		// パネルを適用
		this.setContentPane(getJContentPane());

		// タイトルセット
		this.setTitle(titleString);

		// カレントディレクトリの表示
		File cur = new File("");
		System.out.println(cur.getAbsolutePath());

		AppPopup popup = new AppPopup();
		popup.addPopupJFrame( this );

	}

	// *****************************************************
	// 画面( パネル作成 )
	// *****************************************************
	private JPanel getJContentPane() {
		if (jContentPane == null) {
			jContentPane = new JPanel();
			jContentPane.setLayout(null);
			jContentPane.add(getJButton(), null);
		}
		return jContentPane;
	}

}
BaseWindow.java
ウインドウの基本機能を実装するツールクラスです。
package action;

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

public class BaseWindow extends JFrame {

	//*************************************************
	// 単純確認メッセージボックス
	//*************************************************
	public void MsgOk(String Message) {

		JOptionPane.showMessageDialog(this, Message);

	}

	// *****************************************************
	// ウインドウの中央移動
	// *****************************************************
	public void centerWindow() {
		centerWindow( 0, 0 );
	}
	public void centerWindow( int offsetY ) {
		centerWindow( offsetY, 0 );
	}
	public void centerWindow(int offsetY,int offsetX) {

		Toolkit Tool;
		Dimension DesktopSize;
		Dimension WindowSize;
		
		Tool = this.getToolkit();
		DesktopSize = Tool.getScreenSize();
		WindowSize = this.getSize();
		this.setLocation(
			(DesktopSize.width-WindowSize.width)/2+offsetX,
			(DesktopSize.height-WindowSize.height)/2+offsetY
		);
		
	}

}