i want the menu on the menu bar to change its background color on mouse over .. like in most applications.
i want this effect -> Sample Picture
what i tried so far ...
public class Menu extends JMenuBar implements ActionListener {
private JMenuItem fileItem_close;
private final MouseListener mouseAction = new MouseAdapter() { //i use this to apply the mouse event
@Override
public void mouseEntered(MouseEvent e) {
JMenu item = (JMenu)e.getSource(); //is this implementation correct ?
item.setOpaque(true);
};
@Override
public void mouseExited(MouseEvent e) {
JMenu item = (JMenu)e.getSource();
item.setOpaque(false);
};
};
public Menu() {
initFileMenu();
}
private void initFileMenu() {
JMenu fileMenu = new JMenu("File");
fileMenu.setMnemonic('F');
fileMenu.setRolloverEnabled(true);
fileItem_close = new JMenuItem("Close");
fileItem_close.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_F4, KeyEvent.ALT_MASK)); //exit on pressing (Alt+F4)
fileItem_close.addActionListener(this);
fileMenu.add(fileItem_close);
fileMenu.setRolloverEnabled(true);
fileMenu.addMouseListener(mouseAction);
fileMenu.setBackground(new Color(0x0066FF)); //The background is not visible as JMenu is not opaque by default.
add(fileMenu);
}
@Override
public void actionPerformed(ActionEvent e) {
JMenuItem source = (JMenuItem)e.getSource();
if(source == fileItem_close)
System.exit(0);
}
}
The code above is not working, whenever i hover over that menu title nothing happen.
P.S: i'm not a GUI expert.
EDIT : i'm using Nimbus LaF
You can call setSelected() for a hover effect. And yes, your implementation of e.getSource() was correct. So change it to these lines:
@Override
public void mouseEntered(MouseEvent e) {
JMenu item = (JMenu) e.getSource(); // is this implementation
// correct ?
item.setSelected(true);
};
@Override
public void mouseExited(MouseEvent e) {
JMenu item = (JMenu) e.getSource();
item.setSelected(false);
};
If you also want the menuitems to pop up on mouseEntered(), call item.doClick() in your mouseEntered method instead of setting it selected.
Edit:
For customization:
UIManager.put("Menu.selectionBackground", Color.BLUE);
UIManager.put("Menu.selectionForeground", Color.WHITE);
UIManager.put("Menu.background", Color.WHITE);
UIManager.put("Menu.foreground", Color.BLACK);
UIManager.put("Menu.opaque", false);
You can change these settings to any color you want, and it's more comfortable than creating an own class that extends JMenu.
If you also want to do this with other components (JMenuItems, for example), have a look at this. You can find all UIManager color key values there.
Edit 2:
For Nimbus LAF, create a new class:
class FillPainter implements Painter<JComponent> {
private final Color color;
FillPainter(Color c) {
color = c;
}
@Override
public void paint(Graphics2D g, JComponent object, int width, int height) {
g.setColor(color);
g.fillRect(0, 0, width, height);
}
}
The above is required for the painting of the background. Now, do this:
for (LookAndFeelInfo info : UIManager.getInstalledLookAndFeels()) {
if ("Nimbus".equals(info.getName())) {
try {
UIManager.setLookAndFeel(info.getClassName());
UIManager.getLookAndFeelDefaults().put("MenuBar:Menu[Selected].backgroundPainter",
new FillPainter(Color.BLUE));
UIManager.getLookAndFeelDefaults().put("MenuBar:Menu[Selected].textForeground", Color.WHITE);
break;
} catch (ClassNotFoundException | InstantiationException | IllegalAccessException
| UnsupportedLookAndFeelException e) {
e.printStackTrace();
}
break;
}
}
For all other Nimbus LAF color key values, check this
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