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
Luong Nguyen
Ranch Hand
Joined: May 06, 2001
Posts: 31
posted
0
Hi, I think you can create a rounded border. Then you set the border for the button. Regards. Luong
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.
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
posted
0
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
posted
0
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.
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
posted
0
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.
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
posted
0
Did anyone ever figure out how to round the jbutton component edge? What class/method do I need to override?
I agree. Here's the link: http://ej-technologies/jprofiler - if it wasn't for jprofiler, we would need to
run our stuff on 16 servers instead of 3.