aspose file tools*
The moose likes Swing / AWT / SWT and the fly likes Custom Button 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 Button" Watch "Custom Button" New topic
Author

Custom Button

Dawid Skrzypczynski
Ranch Hand

Joined: Jun 08, 2011
Posts: 52
Hi,
I know that i had similar problem recently and i should to do it yourself but i tried many times and I was looking for solution without success.

I know that is possibility to add various graphics for different state: pressed etc. but i want to create button not only in the shape of a Rectangle or Square. I just want to more graphics control for my swing components.



Method paint is invoked but the Rectangle is not drawn. The Graphics is not displayed but if i change something for parameters component for example

this work

What is wrong ?
Maneesh Godbole
Saloon Keeper

Joined: Jul 26, 2007
Posts: 10523
    
    9


Check out the drawRect API to see what the parameters indicate.
Hint: The button cannot and will not paint outside its own "region"


[How to ask questions] [Donate a pint, save a life!] [Onff-turn it on!]
Dawid Skrzypczynski
Ranch Hand

Joined: Jun 08, 2011
Posts: 52
Sorry you have right but if i add size manually that won't work. example:


Darryl Burke
Bartender

Joined: May 03, 2008
Posts: 4658
    
    5

Coordinates are in user space. This means that the button's 4 corners are locate at [0, 0] [getWidth(), 0] [getWidth(), getHeight()] [0, getHeight()]

Take it from there.


luck, db
There are no new questions, but there may be new answers.
Dawid Skrzypczynski
Ranch Hand

Joined: Jun 08, 2011
Posts: 52
Coordinates
If i good understood this article and you my button has this coordinates:
x: 0
y: 0
c.getWidth()
c.getHeight()

but still it is the same. this is my corrected code:

Darryl Burke
Bartender

Joined: May 03, 2008
Posts: 4658
    
    5

You probably just can't see the drawn rectangle. Maybe you meant to fillRect(...) instead?

And when you get that sorted out, you'll still have a button sans text and icon, without focus or pressed decoration. Are you sure what you want can't be achieved by setting the values contained in Button.gradient in the UIManager?
Dawid Skrzypczynski
Ranch Hand

Joined: Jun 08, 2011
Posts: 52
I changed coord. and instead of draw i used fill and now works. I see that i could used UIManager to get the same effect.
I wonder if there is any possibility to add custom shape to button. For example circle. If i draw circle using MetalButtonUI he will be inside the square or rectangle. Maybe i paste image to be more specific.






btw. Darryl thanks you again
Darryl Burke
Bartender

Joined: May 03, 2008
Posts: 4658
    
    5

Dawid Skrzypczynski wrote:If i draw circle using MetalButtonUI he will be inside the square or rectangle.


Did you read what Maneesh said?
Hint: The button cannot and will not paint outside its own "region"
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: Custom Button