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
What about a Confirm dialog return value with this?
ReplyDeleteYou can use the JOptionPane#getInputValue() method.
ReplyDeletefinal 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);
}
});
Thank you.
ReplyDeleteThis leads to erroneous behavior when a JCombobox is present in the dialog...(the Combobox's list popup isn't shown).
ReplyDeleteThat's why JOptionpane#showInternalXXXX uses PopupFactory.forceHeavyWeightPopupKey, Boolean.TRUE);
Hi Sjors, Thanks for reporting.
ReplyDeleteNote:
- 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);