• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Tim Cooke
  • Ron McLeod
  • paul wheaton
  • Jeanne Boyarsky
Sheriffs:
  • Paul Clapham
  • Devaka Cooray
Saloon Keepers:
  • Tim Holloway
  • Roland Mueller
  • Himai Minh
Bartenders:

Custom Buttons

 
Ranch Hand
Posts: 15304
6
Mac OS X IntelliJ IDE Chrome
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Ranch Hand
Posts: 31
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,
I think you can create a rounded border. Then you set the border for the button.
Regards.
Luong
 
Gregg Bolinger
Ranch Hand
Posts: 15304
6
Mac OS X IntelliJ IDE Chrome
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks, but how do I create a rounded border?
 
Luong Nguyen
Ranch Hand
Posts: 31
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Ranch Hand
Posts: 15304
6
Mac OS X IntelliJ IDE Chrome
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 31
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Yes, you're right. Let's use:
JButton button = new JButton("your label");
button.setBorder(new OvalBorder());
Regards.
Luong.
 
Gregg Bolinger
Ranch Hand
Posts: 15304
6
Mac OS X IntelliJ IDE Chrome
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Ranch Hand
Posts: 148
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I'm not actually getting what u want, but try setting the colors to SystemColor.control & see the effect, is it what u want?
 
Ranch Hand
Posts: 342
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Gregg Bolinger
Ranch Hand
Posts: 15304
6
Mac OS X IntelliJ IDE Chrome
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 31
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 31
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I'm sorry. I confuse:
RoundButtonBorder doesn't extend JButton, RoundButton extends JButton.
 
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Ranch Hand
Posts: 92
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Did anyone ever figure out how to round the jbutton component edge? What class/method do I need to override?
 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic