Skip to main content
gamedev.net
🔒 Locked

[java] Customizing Buttons

Started by Dovyman 1 reply 800+ views
Original Post
Dovyman
Dovyman
I wanted to make a button with rounded edges or that I could basically do anything with graphically, so I made a new class like the one below:
public class ColorButton extends JButton {

		public ColorButton(String text) {
			super(text);

			// We must be non-opaque since we won't fill all pixels.

			// This will also stop the UI from filling our background.

			setOpaque(false);

			 setBorder(BorderFactory.createEmptyBorder(3, 5, 3, 5));
		}

		protected void paintComponent(Graphics g) {
			int width = getWidth();
			int height = getHeight();

			g.setColor(Color.black);
			g.fillRect(0,0, width, height);
			// Paint a rounded rectangle in the background.

			g.setColor(Color.orange);
	        g.fillRoundRect(0, 0, width, height, height, height);

			// Now call the superclass behavior to paint the foreground.

			super.paintComponent(g);
		}
	}
 
This works well until you click on the button, at which point it must be calling a different painting method or something, because it goes back to its default pressed look, until you let go. Any ideas? or are there other better ways to achieve this? [edited by - dovyman on April 26, 2004 9:09:01 PM]
CaptainJester
CaptainJester
The only way to "easily" change the appearance of the Swing GUI is to create your own look and feel. Easily is in quotes because it is a relative term. To create a look and feel requires subclassing an existing one or creating an entirly new one. While it is not particularly difficult, there are a lot of classes involved. The easiest way to experiment is to subclass an existing one, like Metal, and play around with changing some values. To start you should also look at the source for Basic and Metal to see what kinds of things that they do. You can also check out The Swing Connection for a little more information. Unfortunatly you really have to dig to get any info at all and there is not a lot of it.



First make it work, then make it fast. --Brian Kernighan

The problems of this world cannot possibly be solved by skeptics or cynics whose horizons are limited by the obvious realities. We need men and women who can dream of things that never were. - John Fitzgerald Kennedy(35th US President)

Do not interrupt your enemy when he is making a mistake. - Napolean Bonaparte
"None of us learn in a vacuum; we all stand on the shoulders of giants such as Wirth and Knuth and thousands of others. Lend your shoulders to building the future!" - Michael Abrash[JavaGaming.org][The Java Tutorial][Slick][LWJGL][

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.