Skip to Main Content

Java SE (Java Platform, Standard Edition)

Announcement

For appeals, questions and feedback about Oracle Forums, please email oracle-forums-moderators_us@oracle.com. Technical questions should be asked in the appropriate category. Thank you!

Changing rollover border on JButton

843805Nov 23 2005 — edited Aug 11 2010
Well, I am completely new in this forum so Hi. I hope you guys can help me out. I have tried to extend JButton to be able to change the background color without loosing the gradient look. I grabbed some code found in this forum (hardcoded som values)...
class JButtonG extends JButton
{
	private Color color1;
	private Color color2;
	private Color color3;

	public JButtonG()
	{

	}

	public JButtonG(String lab)
	{
		super.setText(lab);

		this.color1 = Color.WHITE;
		this.color2 = Color.RED;
		this.color3 = new Color(236, 236, 236);

		setContentAreaFilled(false);
	}

	protected void paintComponent(Graphics g)
	{
		final Graphics2D g2 = (Graphics2D) g;

		int w = getWidth();
		int h = getHeight();

		Color c = ((this.isEnabled())? color2 : color3);

		GradientPaint gradient =
			new GradientPaint(20, 0, color1, 20, h, c, false);

		g2.setPaint(gradient);
		g2.fillRect(0, 0, w, h);

		super.paintComponent(g);
	}
}
... which works beatifully, except the border on rollover. The above code will change the background on the button to red but when the mouse is over the button a border appear, which is blue. I guess the default color of the button.

How can I change the color of the extended JButton?

Thanks, O.
Comments
Locked Post
New comments cannot be posted to this locked post.
Post Details
Locked on Sep 8 2010
Added on Nov 23 2005
2 comments
1,477 views