20 * 21 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 22 * or visit www.oracle.com if you need additional information or have any 23 * questions. 24 */ 25 package javax.swing; 26 27 import java.awt.Component; 28 import java.awt.Graphics; 29 import java.awt.Insets; 30 import java.awt.event.*; 31 import java.beans.JavaBean; 32 import java.beans.BeanProperty; 33 import java.beans.Transient; 34 import java.util.Vector; 35 36 import java.io.Serializable; 37 import java.io.ObjectOutputStream; 38 import java.io.ObjectInputStream; 39 import java.io.IOException; 40 41 import javax.swing.plaf.*; 42 import javax.accessibility.*; 43 44 /** 45 * An implementation of a menu bar. You add <code>JMenu</code> objects to the 46 * menu bar to construct a menu. When the user selects a <code>JMenu</code> 47 * object, its associated <code>JPopupMenu</code> is displayed, allowing the 48 * user to select one of the <code>JMenuItems</code> on it. 49 * <p> 50 * For information and examples of using menu bars see 51 * <a 52 href="http://docs.oracle.com/javase/tutorial/uiswing/components/menu.html">How to Use Menus</a>, 53 * a section in <em>The Java Tutorial.</em> 54 * <p> 55 * <strong>Warning:</strong> Swing is not thread safe. For more 56 * information see <a 57 * href="package-summary.html#threading">Swing's Threading 58 * Policy</a>. 59 * <p> 60 * <strong>Warning:</strong> 61 * Serialized objects of this class will not be compatible with 62 * future Swing releases. The current serialization support is 63 * appropriate for short term storage or RMI between applications running 89 { 90 /** 91 * @see #getUIClassID 92 * @see #readObject 93 */ 94 private static final String uiClassID = "MenuBarUI"; 95 96 /* 97 * Model for the selected subcontrol. 98 */ 99 private transient SingleSelectionModel selectionModel; 100 101 private boolean paintBorder = true; 102 private Insets margin = null; 103 104 /* diagnostic aids -- should be false for production builds. */ 105 private static final boolean TRACE = false; // trace creates and disposes 106 private static final boolean VERBOSE = false; // show reuse hits/misses 107 private static final boolean DEBUG = false; // show bad params, misc. 108 109 /** 110 * Creates a new menu bar. 111 */ 112 public JMenuBar() { 113 super(); 114 setFocusTraversalKeysEnabled(false); 115 setSelectionModel(new DefaultSingleSelectionModel()); 116 updateUI(); 117 } 118 119 /** 120 * Returns the menubar's current UI. 121 * 122 * @return a {@code MenuBarUI} which is the menubar's current L&F object 123 * @see #setUI 124 */ 125 public MenuBarUI getUI() { 126 return (MenuBarUI)ui; 127 } 128 129 /** 130 * Sets the L&F object that renders this component. 131 * 132 * @param ui the new MenuBarUI L&F object 133 * @see UIDefaults#getUI 134 */ 135 @BeanProperty(hidden = true, visualUpdate = true, description 136 = "The UI object that implements the Component's LookAndFeel.") 137 public void setUI(MenuBarUI ui) { 138 super.setUI(ui); 139 } 140 141 /** 142 * Resets the UI property with a value from the current look and feel. 143 * 144 * @see JComponent#updateUI 145 */ 146 public void updateUI() { 147 setUI((MenuBarUI)UIManager.getUI(this)); 148 } 149 150 151 /** 152 * Returns the name of the L&F class that renders this component. 153 * 154 * @return the string "MenuBarUI" 155 * @see JComponent#getUIClassID 156 * @see UIDefaults#getUI 157 */ 158 @BeanProperty(bound = false) 159 public String getUIClassID() { 160 return uiClassID; 161 } 162 163 164 /** 165 * Returns the model object that handles single selections. 166 * | 20 * 21 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 22 * or visit www.oracle.com if you need additional information or have any 23 * questions. 24 */ 25 package javax.swing; 26 27 import java.awt.Component; 28 import java.awt.Graphics; 29 import java.awt.Insets; 30 import java.awt.event.*; 31 import java.beans.JavaBean; 32 import java.beans.BeanProperty; 33 import java.beans.Transient; 34 import java.util.Vector; 35 36 import java.io.Serializable; 37 import java.io.ObjectOutputStream; 38 import java.io.ObjectInputStream; 39 import java.io.IOException; 40 import java.lang.reflect.Method; 41 42 import javax.swing.plaf.*; 43 import javax.accessibility.*; 44 45 import java.security.AccessController; 46 import java.security.PrivilegedAction; 47 import sun.awt.OSInfo; 48 49 /** 50 * An implementation of a menu bar. You add <code>JMenu</code> objects to the 51 * menu bar to construct a menu. When the user selects a <code>JMenu</code> 52 * object, its associated <code>JPopupMenu</code> is displayed, allowing the 53 * user to select one of the <code>JMenuItems</code> on it. 54 * <p> 55 * For information and examples of using menu bars see 56 * <a 57 href="http://docs.oracle.com/javase/tutorial/uiswing/components/menu.html">How to Use Menus</a>, 58 * a section in <em>The Java Tutorial.</em> 59 * <p> 60 * <strong>Warning:</strong> Swing is not thread safe. For more 61 * information see <a 62 * href="package-summary.html#threading">Swing's Threading 63 * Policy</a>. 64 * <p> 65 * <strong>Warning:</strong> 66 * Serialized objects of this class will not be compatible with 67 * future Swing releases. The current serialization support is 68 * appropriate for short term storage or RMI between applications running 94 { 95 /** 96 * @see #getUIClassID 97 * @see #readObject 98 */ 99 private static final String uiClassID = "MenuBarUI"; 100 101 /* 102 * Model for the selected subcontrol. 103 */ 104 private transient SingleSelectionModel selectionModel; 105 106 private boolean paintBorder = true; 107 private Insets margin = null; 108 109 /* diagnostic aids -- should be false for production builds. */ 110 private static final boolean TRACE = false; // trace creates and disposes 111 private static final boolean VERBOSE = false; // show reuse hits/misses 112 private static final boolean DEBUG = false; // show bad params, misc. 113 114 private static boolean disableGlobalMenuBar = true; 115 116 117 static { 118 AccessController 119 .doPrivileged((PrivilegedAction<Void>) () -> { 120 if (OSInfo.getOSType() == OSInfo.OSType.MACOSX 121 && !Boolean.getBoolean("jdk.swing.disableForcedGlobalMenuBar")) { 122 System.loadLibrary("osxui"); 123 disableGlobalMenuBar = false; 124 } 125 return null; 126 }); 127 } 128 129 130 private static boolean getScreenMenuBarProperty() { 131 try { 132 Class<?> cls = Class.forName("com.apple.laf.AquaMenuBarUI"); 133 Method m = cls.getDeclaredMethod("getScreenMenuBarProperty"); 134 m.setAccessible(true); 135 136 return ((boolean) m.invoke(null)); 137 } catch (Exception ignored) {} 138 139 return false; 140 } 141 142 143 144 /** 145 * Creates a new menu bar. 146 */ 147 public JMenuBar() { 148 super(); 149 setFocusTraversalKeysEnabled(false); 150 setSelectionModel(new DefaultSingleSelectionModel()); 151 updateUI(); 152 } 153 154 /** 155 * Returns the menubar's current UI. 156 * 157 * @return a {@code MenuBarUI} which is the menubar's current L&F object 158 * @see #setUI 159 */ 160 public MenuBarUI getUI() { 161 return (MenuBarUI)ui; 162 } 163 164 /** 165 * Sets the L&F object that renders this component. 166 * 167 * @param ui the new MenuBarUI L&F object 168 * @see UIDefaults#getUI 169 */ 170 @BeanProperty(hidden = true, visualUpdate = true, description 171 = "The UI object that implements the Component's LookAndFeel.") 172 public void setUI(MenuBarUI ui) { 173 super.setUI(ui); 174 } 175 176 /** 177 * Resets the UI property with a value from the current look and feel. 178 * 179 * @see JComponent#updateUI 180 */ 181 public void updateUI() { 182 if (!disableGlobalMenuBar) { 183 if (getScreenMenuBarProperty()) { 184 UIManager.put("MenuBarUI", "com.apple.laf.AquaMenuBarUI"); 185 } else { 186 UIManager.put("MenuBarUI", null); 187 } 188 } 189 setUI((MenuBarUI)UIManager.getUI(this)); 190 } 191 192 193 /** 194 * Returns the name of the L&F class that renders this component. 195 * 196 * @return the string "MenuBarUI" 197 * @see JComponent#getUIClassID 198 * @see UIDefaults#getUI 199 */ 200 @BeanProperty(bound = false) 201 public String getUIClassID() { 202 return uiClassID; 203 } 204 205 206 /** 207 * Returns the model object that handles single selections. 208 * |