Swing-jpopupmenu-control

提供:Dev Guides
移動先:案内検索

SWING-JPopupMenuクラス

前書き

ポップアップメニューは、コンポーネント内の指定された位置に動的にポップアップできるメニューを表します。

クラス宣言

以下は javax.swing.JPopupMenu クラスの宣言です-

public class JPopupMenu
   extends JComponent
      implements Accessible, MenuElement

クラスコンストラクター

Sr.No. Constructor & Description
1

JPopupMenu()

「呼び出し側」なしでJPopupMenuを構築します。

2

JPopupMenu(String label)

指定されたタイトルでJPopupMenuを構築します。

クラスメソッド

Swing JPopupMenu Controlクラスのメソッドのリストを以下に示します。

Sr.No. Method & Description
1

JMenuItem add(Action a)

指定されたActionオブジェクトをディスパッチするメニューの最後に新しいメニュー項目を追加します。

2

JMenuItem add(JMenuItem menuItem)

指定されたメニュー項目をこのメニューの最後に追加します。

3

JMenuItem add(String s)

指定されたテキストを使用して新しいメニュー項目を作成し、このメニューの末尾に追加します。

4

void addMenuKeyListener(MenuKeyListener l)

MenuKeyListenerをポップアップメニューに追加します。

5

void addPopupMenuListener(PopupMenuListener l)

PopupMenuリスナーを追加します。

6

void addSeparator()

メニューの最後に新しいセパレーターを追加します。

7

protected PropertyChangeListener createActionChangeListener(JMenuItem b)

アクションの変更が発生するとコントロールを更新する、適切に構成されたPropertyChangeListenerを返します。

8

protected JMenuItem createActionComponent(Action a)

JPopupMenuに追加されたアクションのJMenuItemを作成するファクトリメソッド。

9

protected void firePopupMenuCanceled()

このポップアップメニューがキャンセルされたことをPopupMenuListenersに通知します。

10

protected void firePopupMenuWillBecomeInvisible()

このポップアップメニューが非表示になることをPopupMenuListenersに通知します。

11

protected void firePopupMenuWillBecomeVisible()

このポップアップメニューが表示されることをPopupMenuListenerに通知します。

12

AccessibleContext getAccessibleContext()

このJPopupMenuに関連付けられているAccessibleContextを取得します。

13

Component getComponent()

このJPopupMenuコンポーネントを返します。

14

Component getComponentAtIndex(int i)

廃止予定です。 Container.getComponent(int)に置き換えられました

15

int getComponentIndex(Component c)

指定されたコンポーネントのインデックスを返します。

16

static boolean getDefaultLightWeightPopupEnabled()

defaultLightWeightPopupEnabledプロパティを取得します。デフォルトではtrueです。

17

Component getInvoker()

このポップアップメニューの「呼び出し側」であるコンポーネントを返します。

18

String getLabel()

ポップアップメニューのラベルを返します。

19

Insets getMargin()

ポップアップメニューの境界線とそのコンテナの間にあるマージンをピクセル単位で返します。

20

MenuKeyListener[]getMenuKeyListeners()

addMenuKeyListener()でこのJPopupMenuに追加されたすべてのMenuKeyListenersの配列を返します。

21

PopupMenuListener[] getPopupMenuListeners()

addPopupMenuListener()でこのJMenuItemに追加されたすべてのPopupMenuListenerの配列を返します。

22

SingleSelectionModel getSelectionModel()

単一の選択を処理するモデルオブジェクトを返します。

23

MenuElement[] getSubElements()

このメニューコンポーネントのサブメニューを含むMenuElementsの配列を返します。

24

PopupMenuUI getUI()

このコンポーネントをレンダリングするLook&Feel(L&F)オブジェクトを返します。

25

String getUIClassID()

このコンポーネントをレンダリングするL&Fクラスの名前を返します。

26

void insert(Action a, int index)

指定されたActionオブジェクトのメニュー項目を特定の位置に挿入します。

27

void insert(Component component, int index)

指定されたコンポーネントをメニューの指定された位置に挿入します。

28

boolean isBorderPainted()

境界線をペイントする必要があるかどうかを確認します。

29

boolean isLightWeightPopupEnabled()

lightWeightPopupEnabledプロパティを取得します。

30

boolean isPopupTrigger(MouseEvent e)

JPopupMenuの現在インストールされているUIによってMouseEventがポップアップトリガーと見なされる場合、trueを返します。

31

boolean isVisible()

ポップアップメニューが表示されている(現在表示されている)場合にtrueを返します。

32

void menuSelectionChanged(boolean isIncluded)

このメニューをアクティブまたは非アクティブにするためにメニューバーの選択が変更されたときにメッセージが送信されます。

33

void pack()

内容を表示するために必要な最小限のスペースを使用するようにコンテナをレイアウトします。

34

protected void paintBorder(Graphics g)

borderPaintedプロパティがtrueの場合、ポップアップメニューの境界線をペイントします。

35

protected String paramString()

このJPopupMenuの文字列表現を返します。

36

protected void processFocusEvent(FocusEvent evt)

登録されたFocusListenerオブジェクトにディスパッチすることにより、このコンポーネントで発生するフォーカスイベントを処理します。

37

protected void processKeyEvent(KeyEvent evt)

ニーモニックやアクセラレータなどのキーストロークイベントを処理します。

38

void processKeyEvent(KeyEvent e, MenuElement[] path, MenuSelectionManager manager)

MenuSelectionManagerから転送されたキーイベントを処理し、必要に応じて、MenuSelectionManagerのAPIを使用してメニュー選択を変更します。

39

void processMouseEvent(MouseEvent event, MenuElement[] path, MenuSelectionManager manager)

このメソッドは、MenuElementインターフェイスに準拠するために必要ですが、実装されていません。

40

void remove(int pos)

このポップアップメニューから指定されたインデックスのコンポーネントを削除します。

41

void removeMenuKeyListener(MenuKeyListener l)

ポップアップメニューからMenuKeyListenerを削除します。

42

void removePopupMenuListener(PopupMenuListener l)

PopupMenuリスナーを削除します。

43

void setBorderPainted(boolean b)

境界線をペイントするかどうかを設定します。

44

static void setDefaultLightWeightPopupEnabled(boolean aFlag)

lightWeightPopupEnabledプロパティのデフォルト値を設定します。

45

void setInvoker(Component invoker)

このポップアップメニューの呼び出し側に、ポップアップメニューメニューが表示されるコンポーネントを設定します。

46

void setLabel(String label)

ポップアップメニューのラベルを設定します。

47

void setLightWeightPopupEnabled(boolean aFlag)

lightWeightPopupEnabledプロパティの値を設定します。デフォルトではtrueです。

48

void setLocation(int x, int y)

x、y座標を使用して、ポップアップメニューの左上隅の位置を設定します。

49

void setPopupSize(Dimension d)

Dimensionオブジェクトを使用して、ポップアップウィンドウのサイズを設定します。

50

void setPopupSize(int width, int height)

ポップアップウィンドウのサイズを指定された幅と高さに設定します。

51

void setSelected(Component sel)

現在選択されているコンポーネントを設定します。これにより、選択モデルが変更されます。

52

void setSelectionModel(SingleSelectionModel model)

単一の選択を処理するようにモデルオブジェクトを設定します。

53

void setUI(PopupMenuUI ui)

このコンポーネントをレンダリングするL&Fオブジェクトを設定します。

54

void setVisible(boolean b)

ポップアップメニューの可視性を設定します。

55

void show(Component invoker, int x, int y)

コンポーネント呼び出し元の座標空間の位置x、yにポップアップメニューを表示します。

56

void updateUI()

UIプロパティを現在のルックアンドフィールの値にリセットします。

継承されるメソッド

このクラスは、次のクラスからメソッドを継承します-

  • javax.swing.JComponent
  • java.awt.Container
  • java.awt.Component
  • java.lang.Object

JPopupMenuの例

たとえば、 D:/> SWING> com> finddevguides> gui> の任意のエディターを使用して、次のJavaプログラムを作成します。

SwingMenuDemo.java

import java.awt.*;
import java.awt.event.*;

import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JMenuItem;
import javax.swing.JPanel;
import javax.swing.JPopupMenu;

public class SwingJpopupMenu{
   private JFrame mainFrame;
   private JLabel headerLabel;
   private JLabel statusLabel;
   private JPanel controlPanel;

   public SwingJpopupMenu(){
      prepareGUI();
   }
   public static void main(String[] args){
      SwingJpopupMenu  swingMenuDemo = new SwingJpopupMenu();
      swingMenuDemo.showPopupMenuDemo();
   }
   private void prepareGUI(){
      mainFrame = new JFrame("Java SWING Examples");
      mainFrame.setSize(400,400);
      mainFrame.setLayout(new GridLayout(3, 1));

      headerLabel = new JLabel("",JLabel.CENTER );
      statusLabel = new JLabel("",JLabel.CENTER);
      statusLabel.setSize(350,100);

      mainFrame.addWindowListener(new WindowAdapter() {
         public void windowClosing(WindowEvent windowEvent){
            System.exit(0);
         }
      });
      controlPanel = new JPanel();
      controlPanel.setLayout(new FlowLayout());

      mainFrame.add(headerLabel);
      mainFrame.add(controlPanel);
      mainFrame.add(statusLabel);
      mainFrame.setVisible(true);
   }
   private void showPopupMenuDemo(){
      final JPopupMenu editMenu = new JPopupMenu("Edit");

      JMenuItem cutMenuItem = new JMenuItem("Cut");
      cutMenuItem.setActionCommand("Cut");

      JMenuItem copyMenuItem = new JMenuItem("Copy");
      copyMenuItem.setActionCommand("Copy");

      JMenuItem pasteMenuItem = new JMenuItem("Paste");
      pasteMenuItem.setActionCommand("Paste");

      MenuItemListener menuItemListener = new MenuItemListener();

      cutMenuItem.addActionListener(menuItemListener);
      copyMenuItem.addActionListener(menuItemListener);
      pasteMenuItem.addActionListener(menuItemListener);

      editMenu.add(cutMenuItem);
      editMenu.add(copyMenuItem);
      editMenu.add(pasteMenuItem);

      mainFrame.addMouseListener(new MouseAdapter() {
         public void mouseClicked(MouseEvent e) {
            editMenu.show(mainFrame, e.getX(), e.getY());
         }
      });
      mainFrame.add(editMenu);
      mainFrame.setVisible(true);
   }
   class MenuItemListener implements ActionListener {
      public void actionPerformed(ActionEvent e) {
         statusLabel.setText(e.getActionCommand() + " MenuItem clicked.");
      }
   }
}

コマンドプロンプトを使用してプログラムをコンパイルします。 D:/> SWING に移動して、次のコマンドを入力します。

D:\SWING>javac com\finddevguides\gui\SwingMenuDemo.java

エラーが発生しない場合、コンパイルが成功したことを意味します。 次のコマンドを使用してプログラムを実行します。

D:\SWING>java com.finddevguides.gui.SwingMenuDemo

次の出力を確認します。 (画面の中央をクリックします。)

SWING JPopupMenu