Is there a way to add a JMenuItem (or similar button-type object) to a JMenuBar?
Adding a JMenuItem doesn't play well with the layout of a JMenuBar, and buttons look too button-like.
Should we be tweaking the button to look like a JMenuItem or tweaking the JMenuBar to display the JMenuItem correctly? Or something else altogether?
The following code implements camickr's solution, although I would have come up with the same thing after seeing the default way JMenuItem
s are rendered in a JMenuBar
. It looks reasonably authentic and responds to clicks, but not to the mnemonic.
I tried giving the JMenuItems accelerators (see code) and that works but that looks really weird.
public class TheDude19 extends JFrame {
private class Action1 extends AbstractAction {
private Action1() {
super("Action1");
putValue(MNEMONIC_KEY, (int) '1');
// putValue(ACCELERATOR_KEY, KeyStroke.getKeyStroke(KeyEvent.VK_A, KeyEvent.CTRL_MASK));
}
public void actionPerformed(ActionEvent arg0) {
System.out.println("Action 1!");
}
}
private class Action2 extends AbstractAction {
private Action2() {
super("Action2");
putValue(MNEMONIC_KEY, (int) '2');
}
public void actionPerformed(ActionEvent arg0) {
System.out.println("Action 2!");
}
}
private class NarrowMenuItem extends JMenuItem {
public NarrowMenuItem(Action a) {
super(a);
}
public Dimension getMaximumSize() {
return new Dimension(super.getPreferredSize().width, super.getMaximumSize().height);
}
}
public TheDude19() {
JMenuItem menu1 = new NarrowMenuItem(new Action1());
JMenuItem menu2 = new NarrowMenuItem(new Action2());
JMenuBar mb = new JMenuBar();
mb.add(menu1);
mb.add(menu2);
add(mb, BorderLayout.NORTH);
setSize(400, 300);
}
public static void main(String[] args) {
(new TheDude19()).setVisible(true);
}
}
JMenuItem doesn't play well with the layout of a JMenuBar
A menubar use a BoxLayout which will try to strech the component to its maximum size. Try using:
menuItem.setMaximumSize( menuItem.getPreferredSize() );
If you need more help post your SSCCE showing the problem.
Just tweak the JButton.
button= new JButton("MenuItem");
button.setOpaque(true);
button.setContentAreaFilled(false);
button.setBorderPainted(false);
button.setFocusable(false);
button.addActionListener(new buttonHandler());
menuBar.add(button);
setContentAreaFilled
makes it see-through,setBorderPainted
gets rid of the border,setFocusable
gets rid of the tiny border around the text.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With