How to customize the JMenu or any component 10-11-2013, 04:16 PM
#1
Hello [username],
I want to know how to customize a component in java. I know that I need to extend it and then paint in in the way I want.
I want to have a JMenu whose hackground is a different color and the font text is different color.
I have already done something like this with JMenuBar :-
and JMenu
you can use the above code and see the simple effect.
I want to know how to customize them further like changing the action listeners property or change the colour of foreground onMouseHover or setting the gradients and setting some kind of animations. I want to know if I want to create my own components and different shapes and events then how can this be done. I haven't worked with customizing components much so I don't have good idea.
One way to do the customization is this but I doubt whether this is efficient or not :-
EDIT : I want to set the background of the tool tip to black. The edited code above does show the tool-tip with black ground but a think border is shown as well, how to get rid of that. Also is there a way to make it dynamic like here I have written from before hand "Click to open", what if I want to show the text on the component in the tooltip.
To do this I think I need to inherit the JTooltip and then paint it in my way, but I haven't tried it yet.
I want to know how to customize a component in java. I know that I need to extend it and then paint in in the way I want.
I want to have a JMenu whose hackground is a different color and the font text is different color.
I have already done something like this with JMenuBar :-
Code:
package com.psychocoder.ide;
import java.awt.Color;
import java.awt.ComponentOrientation;
import java.awt.Graphics;
import java.awt.Graphics2D;
import javax.swing.JMenuBar;
public class MenuBar extends JMenuBar{
private static final long serialVersionUID = -1251333663922218277L;
public MenuBar(){
setComponentOrientation(ComponentOrientation.LEFT_TO_RIGHT);
}
@Override
protected void paintComponent(Graphics g){
super.paintComponent(g);
Graphics2D g2d = (Graphics2D) g;
g2d.setColor(new Color(146, 0, 0));
g2d.fillRect(0, 0, getWidth() - 1, getHeight() - 1);
}
}
and JMenu
Code:
package com.psychocoder.ide;
import java.awt.Color;
import java.awt.Graphics;
import java.awt.Graphics2D;
import javax.swing.JMenu;
public final class Menu extends JMenu{
private static final long serialVersionUID = -5687538740176278239L;
public Menu(String name){
super(name);
}
@Override
protected void paintComponent(Graphics g){
super.paintComponent(g);
Graphics2D g2d = (Graphics2D)g;
g2d.setBackground(new Color(52,52,52));
setForeground(Color.WHITE);
setIconTextGap(8);
setToolTipText("<html>" +
"<body bgcolor=black>" +
"<font color=#ffffff>Click to Open</font>" +
"</body>"+
"</html>");
}
}
you can use the above code and see the simple effect.
I want to know how to customize them further like changing the action listeners property or change the colour of foreground onMouseHover or setting the gradients and setting some kind of animations. I want to know if I want to create my own components and different shapes and events then how can this be done. I haven't worked with customizing components much so I don't have good idea.
One way to do the customization is this but I doubt whether this is efficient or not :-
Code:
UIManager.getLookAndFeelDefaults().put("Menu.foreground", Color.WHITE);
EDIT : I want to set the background of the tool tip to black. The edited code above does show the tool-tip with black ground but a think border is shown as well, how to get rid of that. Also is there a way to make it dynamic like here I have written from before hand "Click to open", what if I want to show the text on the component in the tooltip.
To do this I think I need to inherit the JTooltip and then paint it in my way, but I haven't tried it yet.
![[Image: OilyCostlyEwe.gif]](http://fat.gfycat.com/OilyCostlyEwe.gif)