JInternalFrame As Modal

狂风中的少年 提交于 2019-12-07 18:07:28

How about using the JOptionPane.showInternalMessageDialog(...):

  • I am running JDK 1.7.0_21 on Windows 7 x64:
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.event.*;

public class ModalInternalFrameTest {
  private final JDesktopPane desktop = new JDesktopPane();
  private final String[] items = new String[] {
    "bananas", "pizza", "hot dogs", "ravioli"
  };
  private final Action openAction = new AbstractAction("open") {
    @Override public void actionPerformed(ActionEvent e) {
      JComboBox<String> combo = new JComboBox<String>(items);
      combo.setEditable(true);
      JOptionPane.showInternalMessageDialog(desktop, combo);
      System.out.println(combo.getSelectedItem());
    }
  };
  public JComponent makeUI(JFrame frame) {
    frame.setJMenuBar(createMenuBar());

    JButton button = new JButton(openAction);
    button.setMnemonic(KeyEvent.VK_S);
    JInternalFrame internal = new JInternalFrame("Button");
    internal.getContentPane().add(button);
    internal.setBounds(20, 20, 100, 100);
    desktop.add(internal);
    internal.setVisible(true);

    JButton b = new JButton(new AbstractAction("beep") {
      @Override public void actionPerformed(ActionEvent e) {
        Toolkit.getDefaultToolkit().beep();
      }
    });
    b.setMnemonic(KeyEvent.VK_B);

    JPanel p = new JPanel(new BorderLayout());
    p.add(b, BorderLayout.SOUTH);
    p.add(desktop);
    return p;
  }
  private JMenuBar createMenuBar() {
    JMenuBar menuBar = new JMenuBar();
    JMenu menu = new JMenu("Frame");
    menu.setMnemonic(KeyEvent.VK_F);
    menuBar.add(menu);

    JMenuItem menuItem = new JMenuItem(openAction);
    menuItem.setMnemonic(KeyEvent.VK_1);
    menuItem.setAccelerator(
      KeyStroke.getKeyStroke(KeyEvent.VK_1, ActionEvent.ALT_MASK));
    menu.add(menuItem);

    return menuBar;
  }
  public static void main(String[] args) {
    EventQueue.invokeLater(new Runnable() {
      @Override public void run() {
        createAndShowGUI();
      }
    });
  }
  public static void createAndShowGUI() {
    JFrame f = new JFrame();
    f.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
    f.getContentPane().add(new ModalInternalFrameTest().makeUI(f));
    f.setSize(640, 480);
    f.setLocationRelativeTo(null);
    f.setVisible(true);
  }
}

There are three types of popups:

  • light weight
  • medium weight
  • heavy weight

The only one that works in a modal state is the heavy weight popup. The "offical" way to change the popup's weight is through the setLightWeightPopupEnabled(boolean aFlag) method. If you set it false the popup will be medium weight when it's inside the application frame and heavy weight when exceeds the frame bounds.

To force heavy weight, you have to use a client property called javax.swing.ClientPropertyKey.PopupFactory_FORCE_HEAVYWEIGHT_POPUP. With this property you can force popups (or every popup in a container) to be heavy weight. But the only way to access it is through reflection because it's private.

Here is an example code:

try {
  Class<?> enumElement = Class.forName("javax.swing.ClientPropertyKey");
  Object[] constants = enumElement.getEnumConstants();
  putClientProperty(constants[3], Boolean.TRUE);
}
catch(ClassNotFoundException ex) {}

If you put this inside your modalInternalFrame's contructor, then every popup placed on it will be heavy weight.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!