TITLE:JInternalFrameをModalにする

JInternalFrameをModalにする

Posted by terai at 2007-10-15
  • category: swing folder: ModalInternalFrame title: JInternalFrameをModalにする tags: [JInternalFrame, GlassPane, Mnemonic, JDesktopPane, JToolTip, JLayeredPane] author: aterai pubdate: 2007-10-15T13:16:07+09:00 description: JInternalFrameをModalにして、他のJInternalFrameなどを操作できないようにブロックします。 image: https://lh6.googleusercontent.com/_9Z4BYR88imo/TQTP9wW-lJI/AAAAAAAAAe0/xQ9vJrX3MuQ/s800/ModalInternalFrame.png hreflang:
       href: https://java-swing-tips.blogspot.com/2008/10/modal-internal-frame.html
       lang: en

概要

JInternalFrameModalにして、他のJInternalFrameなどを操作できないようにブロックします。

概要

JInternalFrameをModalにして、他のJInternalFrameなどを操作できないようにブロックします。
  • &jnlp;
  • &jar;
  • &zip;

#screenshot

サンプルコード

#spanend
#spandel
//menuItem.setMnemonic(KeyEvent.VK_1);
#spanend
#spanadd
* サンプルコード [#sourcecode]
#spanend
#spanadd
#code(link){{
#spanend
#spanadd
// menuItem.setMnemonic(KeyEvent.VK_1);
#spanend
class ModalInternalFrameAction1 extends AbstractAction {
  public ModalInternalFrameAction1(String label) {
    super(label);
  }
  public void actionPerformed(ActionEvent e) {
#spanadd

#spanend
  @Override public void actionPerformed(ActionEvent e) {
    setJMenuEnabled(false);
    JOptionPane.showInternalMessageDialog(
      desktop, "information", "modal1", JOptionPane.INFORMATION_MESSAGE);
    setJMenuEnabled(true);
  }
}
#spandel
#spanend
#spandel
//menuItem.setMnemonic(KeyEvent.VK_2);
#spanend
#spanadd

#spanend
#spanadd
// menuItem.setMnemonic(KeyEvent.VK_2);
#spanend
class ModalInternalFrameAction2 extends AbstractAction {
  private final JPanel glass = new MyGlassPane();
  public ModalInternalFrameAction2(String label) {
    super(label);
    Rectangle screen = frame.getGraphicsConfiguration().getBounds();
    glass.setBorder(BorderFactory.createEmptyBorder());
    glass.setLocation(0,0);
    glass.setLocation(0, 0);
    glass.setSize(screen.width, screen.height);
    glass.setOpaque(false);
    glass.setVisible(false);
    desktop.add(glass, JLayeredPane.MODAL_LAYER);
  }
  public void actionPerformed(ActionEvent e) {
#spanadd

#spanend
  @Override public void actionPerformed(ActionEvent e) {
    setJMenuEnabled(false);
    glass.setVisible(true);
    JOptionPane.showInternalMessageDialog(
      desktop, "information", "modal2", JOptionPane.INFORMATION_MESSAGE);
    glass.setVisible(false);
    setJMenuEnabled(true);
  }
}
#spandel
#spanend
#spandel
//menuItem.setMnemonic(KeyEvent.VK_3);
#spanend
#spandel
//Creating Modal Internal Frames -- Approach 1 and Approach 2
#spanend
#spandel
//http://java.sun.com/developer/JDCTechTips/2001/tt1220.html
#spanend
#spanadd

#spanend
#spanadd
// menuItem.setMnemonic(KeyEvent.VK_3);
#spanend
#spanadd
// Creating Modal Internal Frames -- Approach 1 and Approach 2
#spanend
#spanadd
// http://web.archive.org/web/20090803142839/http://java.sun.com/developer/JDCTechTips/2001/tt1220.html
#spanend
class ModalInternalFrameAction3 extends AbstractAction {
  private final Component orgGlassPane;
  private final JPanel glass = new PrintGlassPane();
  public ModalInternalFrameAction3(String label) {
    super(label);
    orgGlassPane = frame.getGlassPane();
    glass.setVisible(false);
  }
  public void actionPerformed(ActionEvent e) {
#spanadd

#spanend
  @Override public void actionPerformed(ActionEvent e) {
    JOptionPane optionPane = new JOptionPane();
    JInternalFrame modal = optionPane.createInternalFrame(desktop, "modal3");
    optionPane.setMessage("Hello, World");
    optionPane.setMessageType(JOptionPane.INFORMATION_MESSAGE);
    JInternalFrame modal = optionPane.createInternalFrame(desktop, "modal3");
    removeSystemMenuListener(modal);
    modal.addInternalFrameListener(new InternalFrameAdapter() {
      public void internalFrameClosed(InternalFrameEvent e) {
      @Override public void internalFrameClosed(InternalFrameEvent e) {
        glass.removeAll();
        glass.setVisible(false);
        frame.setGlassPane(orgGlassPane);
      }
    });
    glass.add(modal);
    Rectangle screen = desktop.getBounds();
    modal.setLocation(screen.x + screen.width/2  - modal.getSize().width/2,
                      screen.y + screen.height/2 - modal.getSize().height/2);
    frame.setGlassPane(glass);
    modal.pack();
    getRootPane().setGlassPane(glass);
    glass.setVisible(true);
    modal.setVisible(true);
    try{
      modal.setSelected(true);
    }catch(java.beans.PropertyVetoException ex) {}
  }
}

解説

  • Alt+1: JOptionPane.showInternalMessageDialogメソッドを使用して、簡単なメッセージを表示するModalなDialogをJDeskTopPane内に表示します。
    • Mnemonicがうまく無効に出来ないため、Alt+Bでボタンを押すことが出来てしまいます(Mnemonicを設定したコンポーネントはsetEnabled(false)とする必要がある)。
    • MnemonicをJMenuに設定しているとsetEnabled(false)としても、Altキーに反応してしまう(Windows L&Fだけ?)ので、ダイアログを表示している間は、JMenuBarをダミーと入れ替えています。
    • このダイアログを閉じない限り、アプリケーションをAlt+F4などで閉じることは出来ません。
    • ダイアログのシステムメニュー(左上のアイコンをクリックすると表示される)がマウスで操作できません。

解説

  • Alt+1: JOptionPane.showInternalMessageDialogメソッドを使用して簡単なメッセージを表示するModalDialogJDesktopPane内に表示
    • JButtonのマウスクリックは無効になるがMnemonicが無効にならない
      • Alt+Bでボタンを押すことが出来てしまう
      • Mnemonicを設定したコンポーネントはsetEnabled(false)とする必要がある
    • MnemonicJMenuに設定しているとsetEnabled(false)としてもAltキーに反応する
    • このInternalMessageDialogを閉じない限りアプリケーションをAlt+F4などで閉じることは出来ない
    • InternalMessageDialogのシステムメニュー(左上のアイコンをクリックすると表示される)がマウスで操作不可能
    • JToolTipは正常
      • showInternalMessageDialog(...)メソッド内で、pane.putClientProperty(PopupFactory_FORCE_HEAVYWEIGHT_POPUP, Boolean.TRUE)(JDK 1.6.0の場合のKeyPopupFactory.forceHeavyWeightPopupKey) されているため、JComboBoxなどのドロップダウンメニューも正常
  • Alt+2: Alt+1と同様ですが、もう一枚タイトルバーなどを削除した半透明なJInternalFrameを下の 半透明なGlassPaneをJLayeredPane.MODAL_LAYERに追加して表示しています。
    • JDeskTop内にマスクが掛かります。
  • Alt+2: Alt+1と同様にJOptionPane.showInternalMessageDialogメソッドを使用し、かつ半透明なGlassPaneJLayeredPane.MODAL_LAYERに追加
    • 動作、制限などはAlt+2InternalMessageDialogと同じ
    • JDesktopPane内にマスクが掛かる
  • Alt+3: JFrameに半透明なGlassPaneを追加し、そこにJInternalFrameを追加することでModalにしています。
    • JFrame内全体(JMenuBarなども含む)にマスクが掛かります。
    • ダイアログのシステムメニューが自身のレイヤーより奥に表示されるため、アイコン(JLabel)をクリックしても反応しないようにリスナーを取り除いています。
    • このダイアログを開いていても、アプリケーションをAlt+F4などで閉じることが出来てしまいます。
  • Alt+3: JFrameに半透明なGlassPaneを追加しそこにJInternalFrameを追加することでModalに設定
    • JFrame内全体(JMenuBarなども含む)にマスクが掛かる
    • InternalMessageDialogのシステムメニューが自身のレイヤーより奥に表示されるため、アイコン(JLabel)をクリックしても反応しないようにリスナーを除去
    • JComboBoxInternalMessageDialogに追加するとそのドロップダウンメニューが裏に表示される
    • このInternalMessageDialogを開いていてもアプリケーションをAlt+F4などで閉じることが出来てしまう

Alexander Potochkin's Blog: Disabling Swing Containers, the final solution?を参考に*1して、GlassPane を以下のように修正すると、上記のサンプルのAlt+3*2は、Mnemonic もうまくブロックできるようです。
  • JFrameのメニューバーのMnemonicもブロックできる
  • JFrameのシステムメニューはブロックできない
  • モーダルにしたJInternalFrameのシステムメニューは表示されない
    • ただし表示されないだけで、クリックしてからカーソル移動やダブルクリックなどが動いてしまう
  • モーダルにしたJInternalFrameの右上の閉じるボタンのJToolTip がJDesktopPane内で表示される場合、空白になる JDK 6 ではJToolTipは表示されない
  • Alt+3の方法で、InternalOptionDialogJComboBoxを追加する場合、ドロップダウンメニューを正しく表示させるにはリフレクションを使ってClientPropertyを設定するしかない?
    • JInternalFrame#putClientProperty(PopupFactory_FORCE_HEAVYWEIGHT_POPUP, Boolean.TRUE)とすればシステムメニューも正常に表示されるが、JOptionPane.showInternalXXXDialogではなぜかJOptionPaneに設定するようになっている(JInternalFrameは使い回ししているから?)
#spanadd
JInternalFrame modal = optionPane.createInternalFrame(desktop, "modal3");
#spanend
#spanadd
JComboBox combo = new JComboBox(new String[] {"Banana", "Apple", "Pear", "Grape"});
#spanend
#spanadd
combo.setEditable(true);
#spanend
#spanadd
try {
#spanend
  Field field;
  if (System.getProperty("java.version").startsWith("1.6.0")) {
    Class clazz = Class.forName("javax.swing.PopupFactory");
    field = clazz.getDeclaredField("forceHeavyWeightPopupKey");
  } else { // 1.7.0, 1.8.0
    Class clazz = Class.forName("javax.swing.ClientPropertyKey");
    field = clazz.getDeclaredField("PopupFactory_FORCE_HEAVYWEIGHT_POPUP");
  }
  field.setAccessible(true);
  modal.putClientProperty(field.get(null), Boolean.TRUE);
#spanadd
} catch (Exception ex) {
#spanend
  ex.printStackTrace();
#spanadd
}
#spanend
#spanadd
optionPane.setMessage(combo);
#spanend
#spanadd
optionPane.setMessageType(JOptionPane.QUESTION_MESSAGE);
#spanend
#spanadd
  • -
  • Alexander Potochkin's Blog: Disabling Swing Containers, the final solution?を参考に(paintではなくprintが使用されている)してGlassPaneを以下のように修正すると、上記のサンプルのAlt+3(Alt+2の場合描画が乱れる)はMnemonicもうまくブロックできるようになる
  • JFrameのメニューバーのMnemonicもブロックできる
    • JRootPaneから取得したLayeredPaneが非表示なので、その子コンポーネント(JMenuBarContentPaneなど)のキーイベントがすべて無効になる
    • JRootPane (Java Platform SE 8)の図にあるように、GlassPaneJRootPaneの最上位の子コンポーネントなのでLayeredPaneを画像として表示している
  • JFrameのシステムメニューはブロックできない
  • モーダルにしたJInternalFrameのシステムメニューは表示されない
    • ただし表示されないだけで、クリックしてからカーソル移動やダブルクリックなどが動いてしまう
  • モーダルにしたJInternalFrameの右上の閉じるボタンのJToolTipJDesktopPane内で表示される場合、空のJToolTipが背面に表示される
    • UIManager.put("InternalFrame.titleButtonToolTipsOn", Boolean.FALSE);で非表示になる
#spanend
class PrintGlassPane extends JPanel {
  TexturePaint texture = TextureFactory.createCheckerTexture(4);
  public PrintGlassPane() {
    super((LayoutManager)null);
    super((LayoutManager) null);
    setOpaque(false);
  }
  @Override
  public void setVisible(boolean isVisible) {
#spanadd

#spanend
  @Override public void setVisible(boolean isVisible) {
    boolean oldVisible = isVisible();
    super.setVisible(isVisible);
    JRootPane rootPane = SwingUtilities.getRootPane(this);
    if(rootPane!=null && isVisible()!=oldVisible) {
    if (rootPane != null && isVisible() != oldVisible) {
      rootPane.getLayeredPane().setVisible(!isVisible);
    }
  }
  @Override
  public void paintComponent(Graphics g) {
#spanadd

#spanend
  @Override protected void paintComponent(Graphics g) {
    JRootPane rootPane = SwingUtilities.getRootPane(this);
    if(rootPane!=null) {
      //http://weblogs.java.net/blog/alexfromsun/archive/2008/01/
    if (rootPane != null) {
      // http://weblogs.java.net/blog/alexfromsun/archive/2008/01/
      // it is important to call print() instead of paint() here
      // because print() doesn't affect the frame's double buffer
      rootPane.getLayeredPane().print(g);
    }
    Graphics2D g2 = (Graphics2D) g;
    g2.setPaint(texture);
    g2.fillRect(0,0,getWidth(),getHeight());
    g2.fillRect(0, 0, getWidth(), getHeight());
  }
}

参考リンク

コメント

  • JInternalFrameを半透明にすると、同様にGlassPaneがUbuntu(GNOME)などで半透明にならない場合があります。 -- terai
    • (Alt+2)で開いた場合、JInternalFrameにGlassPaneを乗せるのではなく、直接JDeskTopPaneのJLayeredPane.MODAL_LAYERに追加するように変更しました。 -- terai
  • メモ: Alexander Potochkin's Blog: Disabling Swing Containers, the final solution?のサンプルでは、Mnemonic もちゃんとブロックできているようなので、「あとで調べる & 参考にする」こと。 -- terai
  • Mnemonic を数字(1-3)に変更 -- terai

参考リンク

コメント