The Java Developers Almanac 1.4


Order this book from Amazon.

   
Home > List of Packages > javax.swing  [141 examples] > Menus  [8 examples]

e812. Forcing a Popup Menu to Be a Heavyweight Component

By default, Swing popup menus used by JMenu and JPopupMenu are lightweight. If heavyweight components are used in the same frame, the popup menus may appear behind the heavyweight components.

This example demonstrates how to force a JPopupMenu to use heavyweight components:

    JPopupMenu popupMenu = new JPopupMenu();
    
    // Retrieve current setting
    boolean lwPopup = popupMenu.isLightWeightPopupEnabled(); // true
    
    // Force the popup menu to use heavyweight components
    popupMenu.setLightWeightPopupEnabled(false);
    // To use the popup menu, see e810 Creating a Popup Menu

This example demonstrates how to force the popup menu of a JMenu to be heavyweight:
    // Create a menu with a menu item
    JMenu menu = new JMenu("Menu Label");
    menu.add(new JMenuItem("Item Label"));
    
    // Retrieve current setting
    lwPopup = menu.getPopupMenu().isLightWeightPopupEnabled(); // true
    
    // Force the menu's popup menu to be heavyweight
    menu.getPopupMenu().setLightWeightPopupEnabled(false);
    // To use the menu, see e808 Creating a JMenuBar, JMenu, and JMenuItem Component

This example configures all popup menus to be heavyweight:
    // Retrieve current setting
    lwPopup = JPopupMenu.getDefaultLightWeightPopupEnabled(); // true
    
    // Globally use heavyweight components for all popup menus
    JPopupMenu.setDefaultLightWeightPopupEnabled(false);

 Related Examples
e808. Creating a JMenuBar, JMenu, and JMenuItem Component
e809. Separating Menu Items in a Menu
e810. Creating a Popup Menu
e811. Creating a Popup Menu with Nested Menus
e813. Getting the Currently Selected Menu or Menu Item
e814. Creating a Menu Item That Listens for Changes to Its Selection Status
e815. Listening for Changes to the Currently Selected Menu or Menu Item

See also: Actions    JButton    JCheckBox    JComboBox    JDesktop and JInternalFrame    JFrame, JWindow, JDialog    JLabel    JList    JProgressBar    JRadioButton    JScrollPane    JSlider    JSpinner    JSplitPane    JTabbedPane    JToolBar    Keystrokes and Input Maps    Layout    Look and Feel    Progress Monitor    The Screen    Tool Tips    UI Default Values   


© 2002 Addison-Wesley.