File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Swing / AWT / SWT and the fly likes Custom Buttons Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Swing / AWT / SWT
Bookmark "Custom Buttons" Watch "Custom Buttons" New topic
Author

Custom Buttons

Gregg Bolinger
GenRocket Founder
Ranch Hand

Joined: Jul 11, 2001
Posts: 15299
    
    6

I would like to give my JButtons a rounded edge. Would I need to manipulate the UIManager or can I do that by extending JButton and doing it there.
Any pointers would be a great help.
Thanks

------------------
Happy Coding,
Gregg Bolinger


GenRocket - Experts at Building Test Data
Luong Nguyen
Ranch Hand

Joined: May 06, 2001
Posts: 31
Hi,
I think you can create a rounded border. Then you set the border for the button.
Regards.
Luong
Gregg Bolinger
GenRocket Founder
Ranch Hand

Joined: Jul 11, 2001
Posts: 15299
    
    6

Thanks, but how do I create a rounded border?
Luong Nguyen
Ranch Hand

Joined: May 06, 2001
Posts: 31
It's the code:
public class OvalBorder implements Border {
protected int m_w = 6;
protected int m_h = 6;
protected Color m_topColor = Color.white;
protected Color m_bottomColor = Color.gray;
public OvalBorder() {
}
public OvalBorder(int w, int h) {
m_w = w;
m_h = h;
}
public OvalBorder(int w, int h, Color topColor, Color bottomColor) {
m_w = w;
m_h = h;
m_topColor = topColor;
m_bottomColor = bottomColor;
}
public Insets getBorderInsets(Component c) {
return new Insets(m_h, m_w, m_h, m_w);
}
public boolean isBorderOpaque() {
return true;
}
public void paintBorder(Component c, Graphics g, int x, int y, int w, int h) {
w--;
h--;
g.setColor(m_topColor);
g.drawLine(x, y + h - m_h, x, y + m_h);
g.drawArc(x, y, 2 * m_w, 2 * m_h, 180, -90);
g.drawLine(x + m_w, y, x + w - m_w, y);
g.drawArc(x + w - 2 * m_w, y, 2 * m_w, 2 * m_h, 90, -90);
g.setColor(m_bottomColor);
g.drawLine(x + w, y + m_h, x + w, y + h - m_h);
g.drawArc(x + w - 2 * m_w, y + h - 2 * m_h, 2 * m_w, 2 * m_h, 0, -90);
g.drawLine(x + m_w, y + h, x + w - m_w, y + h);
g.drawArc(x, y + h - 2 * m_h, 2 * m_w, 2 * m_h, -90, -90);
}
}
Regards.
Luong.
Gregg Bolinger
GenRocket Founder
Ranch Hand

Joined: Jul 11, 2001
Posts: 15299
    
    6

Thanks!! That is great. Just one more question, and sorry for being to dumb, but do I now just say
myButton.setBorder(OvalBorder());
Again, thanks.

------------------
Happy Coding,
Gregg Bolinger
Luong Nguyen
Ranch Hand

Joined: May 06, 2001
Posts: 31
Yes, you're right. Let's use:
JButton button = new JButton("your label");
button.setBorder(new OvalBorder());
Regards.
Luong.
Gregg Bolinger
GenRocket Founder
Ranch Hand

Joined: Jul 11, 2001
Posts: 15299
    
    6

Thanks Luong, that really looks nice. There is still just one thing though.
The border is rounded, but you can still see the corners of the component that the border is placed on.
Know any fix for that? I guess I would have to change the way the component is drawn huh.
------------------
Happy Coding,
Gregg Bolinger
Vinod Venugopal
Ranch Hand

Joined: Dec 06, 2000
Posts: 148
I'm not actually getting what u want, but try setting the colors to SystemColor.control & see the effect, is it what u want?


- Vinod<br />-------<br />SCJP2
Ben Wood
Ranch Hand

Joined: Aug 14, 2001
Posts: 342
I never knew you could do this sort of thing. I'm impressed!
Following on from this: In theory, can you tell me is it a similar process to over-ride the paint() method of JComponent in your own class that implements JComponent, and thus draw your own completely new component in the same way that paintBorder() was used to create the completely new rounded border?
regards,
Ben.


SCJP 1.4, www.gsi3d.org.uk
Gregg Bolinger
GenRocket Founder
Ranch Hand

Joined: Jul 11, 2001
Posts: 15299
    
    6

What I am saying is, making the border rounded is nice, but the JComponent that you place the border on is still a rectangle, so where the rounded edges of the border are, you can see the squared corners of the JButton. I need to actually round off the corners of the JButton, not just the border.

------------------
Happy Coding,
Gregg Bolinger
Luong Nguyen
Ranch Hand

Joined: May 06, 2001
Posts: 31
Hi Gregg,
I think you have known about the following classes: JComponent, ComponentUI, JButton, BasicButtonUI. The JButton extends JComponent and BasicButtonUI extends ComponentUI. In the Swing, each button has a default border that is BasicBorders.ButtonBorder (BasicBorders is in package javax.swing.plaf.basic). And further, we have known to render a component, the paintComponent method is used. If you observe this method, you will find out the method use ComponentUI.update method for painting. Therefore JButton will use buttonUI object that extends BasicButtonUI to render the button.
I think you should create 3 classes: RoundButtonBorder (extends JButton), RoundButtonUI, RoundButton. You should observe the code of BasicBorders.ButtonBorder, WindowButtonUI (in the package com.sun.window.plaf.) and the ComponentUI.update method.
Regards.
Luong.
Luong Nguyen
Ranch Hand

Joined: May 06, 2001
Posts: 31
I'm sorry. I confuse:
RoundButtonBorder doesn't extend JButton, RoundButton extends JButton.
syed fazal faheem
Greenhorn

Joined: Jul 13, 2001
Posts: 12
Hi,
There is quite a lot of stuff in here. Thanks guys for all the info. But i still have a problem.
As a quick intro, i am developing look and feel implementation for my swing application. I am having a set of internal frames in my application.
When i try to set the button border in UIDefaults, even the minimize, maximize and close buttons of internal frames are getting affected. Can any one help me out with this.
This is the code i used to set the UIDefaults in a class derived from javax.swing.plaf.basic.BasicLookAndFeel
protected void initComponentDefaults(UIDefaults table) {
super.initComponentDefaults(table);
Object[] defaults = {
"Button.font", new FontUIResource("Arial", Font.BOLD, 12 ),
"Button.border",new BorderUIResource(new OvalBorder()),
"Button.margin", new InsetsUIResource(8, 8, 8, 8)
};
table.putDefaults( defaults );
}
OvalBorder class implements javax.swing.border.Border interface

Waiting for a solution,
fazal.
Cathy Song
Ranch Hand

Joined: Jul 01, 2003
Posts: 92
Did anyone ever figure out how to round the jbutton component edge? What class/method do I need to override?
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
 
subject: Custom Buttons