Google Tag Manager

2008/10/20

Modal Internal Frame

Code

//menuItem.setMnemonic(KeyEvent.VK_3);
//Creating Modal Internal Frames -- Approach 1 and Approach 2
//http://java.sun.com/developer/JDCTechTips/2001/tt1220.html
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) {
    JOptionPane optionPane = new JOptionPane();
    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) {
        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);
    glass.setVisible(true);
    modal.setVisible(true);
    try{
      modal.setSelected(true);
    }catch(java.beans.PropertyVetoException ex) {}
  }
}

References

5 comments:

  1. What about a Confirm dialog return value with this?

    ReplyDelete
  2. You can use the JOptionPane#getInputValue() method.

    final JOptionPane optionPane = new JOptionPane();
    optionPane.setMessage("Hello, World");
    optionPane.setMessageType(JOptionPane.QUESTION_MESSAGE);
    //optionPane.putClientProperty(
    // PopupFactory.forceHeavyWeightPopupKey, Boolean.TRUE);
    optionPane.setWantsInput(true);
    JInternalFrame modal = optionPane.createInternalFrame(desktop, "modal3");
    removeSystemMenuListener(modal);
    modal.addInternalFrameListener(new InternalFrameAdapter() {
    public void internalFrameClosed(InternalFrameEvent e) {
    Object value = optionPane.getInputValue();
    if(value == JOptionPane.UNINITIALIZED_VALUE) value = null;
    System.out.println(value);
    glass.setVisible(false);
    frame.setGlassPane(orgGlassPane);
    }
    });

    ReplyDelete
  3. This leads to erroneous behavior when a JCombobox is present in the dialog...(the Combobox's list popup isn't shown).

    That's why JOptionpane#showInternalXXXX uses PopupFactory.forceHeavyWeightPopupKey, Boolean.TRUE);

    ReplyDelete
  4. Hi Sjors, Thanks for reporting.
    Note:
    - Alt-1, Alt-2 simply use JOptionPane.showInternalXXXDialog(...), but JInternalFrame System Menu cause unexpected behavior(mouse).
    - To use JComboBox in this case (Alt-3 JOptionPane), may need to use refrection... following code:
    JOptionPane optionPane = new JOptionPane();
    JInternalFrame modal = optionPane.createInternalFrame(desktop, "modal3");
    //GlassPane + JComboBox Test:
    JComboBox combo = new JComboBox(new String[] {"Banana", "Apple", "Pear", "Grape"});
    combo.setEditable(true);
    try{
    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);
    }catch(Exception ex) {
    ex.printStackTrace();
    }
    optionPane.setMessage(combo);
    optionPane.setMessageType(JOptionPane.QUESTION_MESSAGE);

    ReplyDelete