private JMenuItem createColoredSeparator(Color color){ JMenuItem separator = new JMenuItem(); separator.setPreferredSize(new Dimension(5,5)); separator.setBackground(color); separator.setEnabled(false); //make the item unclickable so menu doesn't close return separator; }
JMenuItem item = new JMenuItem(); item.setPreferredSize(new Dimension(200, item.getPreferredSize().height));
JMenu menu = new JMenu("Menu"); JMenuItem item = new JMenuItem("Add"); item.add(new JButton("SOMETHING A BIT WORDY")); item.setLayout(new FlowLayout()); // set FlowLayout for item item.setPreferredSize(new Dimension(200, 100)); // hard code implementation :-( menu.add(item); // ... JMenuBar menuBar = new JMenuBar(); menuBar.add(menu); JFrame frame = new JFrame(); frame.setJMenuBar(menuBar); // ... frame.pack(); frame.setVisible(true);
private JMenuItem createColoredSeparator(Color color){ JMenuItem separator = new JMenuItem(); separator.setPreferredSize(new Dimension(50,50)); //Increased to highlight whats happening //Imitate the default raised border Border raisedBorder = BorderFactory.createBevelBorder(BevelBorder.RAISED, UIManager.getColor("MenuItem.background"),UIManager.getColor("MenuItem.background").darker()); //Merge the borders with the raised border applied outside of the border concealing part of the background separator.setBorder(new CompoundBorder( raisedBorder, BorderFactory.createLineBorder(UIManager.getColor("MenuItem.background"), 8))); separator.setBackground(color); separator.setEnabled(false); //make the item un-clickable so menu doesn't close return separator; }
private JMenuItem newItem(String text, String iconPath) { JMenuItem myMenuItem; if (iconPath == null || iconPath.isEmpty()) { myMenuItem = new JMenuItem(text); myMenuItem.setPreferredSize(new Dimension(myMenuItem.getFontMetrics(myMenuItem.getFont()).stringWidth(text), 20)); } else { ImageIcon icon = new ImageIcon(iconPath); myMenuItem = new JMenuItem(text, icon); myMenuItem.setPreferredSize(new Dimension(icon.getIconWidth(), icon.getIconHeight())); } return myMenuItem; }
private JMenuItem newItem(String text, String iconPath) { JMenuItem myMenuItem; ImageIcon icon= null; int iconPixels = 0; if (iconPath == null || iconPath.isEmpty()) { myMenuItem = new JMenuItem(text); } else { icon = new ImageIcon(iconPath); myMenuItem = new JMenuItem(text, icon); iconPixels = icon.getIconWidth(); } FontMetrics fontM = myMenuItem.getFontMetrics(new Font("Default", Font.PLAIN, 12)); int stringPixels = fontM.stringWidth(text); int newWidth = stringPixels + iconPixels; newWidth = newWidth < Menu.MINWIDTH ? Menu.MINWIDTH : newWidth; newWidth = newWidth > Menu.MAXWIDTH ? Menu.MAXWIDTH : newWidth; myMenuItem.setPreferredSize(new Dimension(newWidth, 20)); System.out.println(text + " - " + newWidth); return myMenuItem; }
item.add(label); Dimension size = new Dimension((int) Math.ceil(item.getPreferredSize().getWidth() + label.getPreferredSize().getWidth()) + 10, 20); item.setPreferredSize(size);
Dimension size = new Dimension(); size.setSize(w, h+8); item.setPreferredSize(size);
newFileMenuItem.setPreferredSize(new java.awt.Dimension(28,16)); newFileMenuItem.setBounds(new java.awt.Rectangle(5,5,28,16)); jMenu3.add(newFileMenuItem); cutMenuItem.setPreferredSize(new java.awt.Dimension(27,16)); cutMenuItem.setBounds(new java.awt.Rectangle(5,5,27,16)); jMenu4.add(cutMenuItem); helpMenuItem.setPreferredSize(new java.awt.Dimension(31,16)); helpMenuItem.setBounds(new java.awt.Rectangle(5,5,31,16)); jMenu5.add(helpMenuItem);