I add a menu to a toolbar like this :
JMenuBar menu = new JMenuBar();
JMenu actions = new JMenu("Aktionen");
Icon menuIcon = ImageUtilities.loadImageIcon("pathToIcon", true);
actions.setIcon(menuIcon);
// Add
JMenuItem addItem = new JMenuItem("Add");
Icon addIcon = ImageUtilities.loadImageIcon("pathToIcon", true);
addItem.setIcon(addIcon);
addItem.setToolTipText("Add new Item");
addItem.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
AddItemAction someAction = new AddItemAction();
someAction.actionPerformed(new ActionEvent(this, ActionEvent.ACTION_PERFORMED, null) {
// Foo
});
}
});
menu.add(actions);
actions.add(addItem);
toolbar.addSeparator();
toolbar.add(menu);
Basically, it works fine. But, it never displays the tooltip ("Add new Item"). Any hints ?
EDIT: Just in case someone with the same problem stumbles upon this: it was the L&F, as I should have suspected from the beginning. It has a property for displaying tooltips of JMenuItems ; and it defaults to false.
The sscce below works correctly. If you still have problems, please edit your question to include an example that exhibits the problem you describe.
Addendum: I added the menu
to a JToolBar
, and it still works, either docked or free-floating.
import java.awt.EventQueue;
import javax.swing.JFrame;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.JToolBar;
/** @see http://stackoverflow.com/a/14630345/230513 */
public class Test {
private void display() {
JFrame f = new JFrame("Test");
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JMenuBar menu = new JMenuBar();
JMenu actions = new JMenu("Aktionen");
JMenuItem addItem = new JMenuItem("Add");
addItem.setToolTipText("Add new Item");
menu.add(actions);
actions.add(addItem);
JToolBar toolbar = new JToolBar("Tools");
toolbar.add(menu);
f.add(toolbar);
f.pack();
f.setLocationRelativeTo(null);
f.setVisible(true);
}
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
@Override
public void run() {
new Test().display();
}
});
}
}