aspose file tools*
The moose likes JavaFX and the fly likes JavaFX Button: You'd think this would be simple.... Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » JavaFX
Bookmark "JavaFX Button: You Watch "JavaFX Button: You New topic
Author

JavaFX Button: You'd think this would be simple....

Gregg Bolinger
GenRocket Founder
Ranch Hand

Joined: Jul 11, 2001
Posts: 15299
    
    6

I'm porting an iphone game over to java for desktop distribution. I'm in the process of investigating different techs to create it and one of those is JavaFX. I want the app to look as similar to the iphone version as possible and I'm trying to apply an image to all my menu buttons. My first attempt was to have a custom group that I would end up adding a mouse listener to that looks something like this...



This is overly verbose for a button. So I started looking at the Button control. I added an image to it however you can still see the Button underneath because of insets/margins and I can't seem to find out how to remove them. I started looking into "skinning" but that seemed way overly complex for what I am trying to do. I also started looking into creating a custom node or extending Button but again, can't figure out how to apply an image to it. Anyone know a simple way to apply an Image to a Button control and not have any button margins/insets?


GenRocket - Experts at Building Test Data
Michael Dunn
Ranch Hand

Joined: Jun 09, 2003
Posts: 4632
"Wild Wild West" guess here.

in the book threads/discussions last week, it was mentioned that javafx had a wrapper class for swing components.

perhaps getting the button to behave itself as a swing component, then using the wrapper class might work.

Jim Clarke
author
Ranch Hand

Joined: Jun 15, 2009
Posts: 49
First, you might want to look at the javafx.scene.control.Button class, however if you want to make the same view that you saw on the iPhone, something like this might work for you. You could also replace Text in the example with javafx.scene.control.Label. Label will confine the text to a maximum width to stay within the overall bounds of the button.



Gregg Bolinger
GenRocket Founder
Ranch Hand

Joined: Jul 11, 2001
Posts: 15299
    
    6

Jim, did you read my post? I looked at the Button control. I just can't figure out how to give it an image and remove the margins. Thanks for the code example, but my example code is no worse and gives me the result I want. I just assumed there was an easier (less code) way of doing this.
Jim Clarke
author
Ranch Hand

Joined: Jun 15, 2009
Posts: 49
The Button control has a skin based on what is called the Caspian theme. To remove the boundaries you could write your own skin, but that would probably be more work that what you are already doing.
Mohamed Sanaulla
Saloon Keeper

Joined: Sep 08, 2007
Posts: 3071
    
  33

Jim, did you read my post? I looked at the Button control. I just can't figure out how to give it an image and remove the margins. Thanks for the code example, but my example code is no worse and gives me the result I want. I just assumed there was an easier (less code) way of doing this.


Can make use of "graphic" property of Button Control.



Mohamed Sanaulla | My Blog
Gregg Bolinger
GenRocket Founder
Ranch Hand

Joined: Jul 11, 2001
Posts: 15299
    
    6

mohammed sanaullah wrote:
Jim, did you read my post? I looked at the Button control. I just can't figure out how to give it an image and remove the margins. Thanks for the code example, but my example code is no worse and gives me the result I want. I just assumed there was an easier (less code) way of doing this.


Can make use of "graphic" property of Button Control.




Gregg Bolinger wrote:I added an image to it however you can still see the Button underneath because of insets/margins and I can't seem to find out how to remove them

Mohamed Sanaulla
Saloon Keeper

Joined: Sep 08, 2007
Posts: 3071
    
  33

Gregg Bolinger wrote:
mohammed sanaullah wrote:
Jim, did you read my post? I looked at the Button control. I just can't figure out how to give it an image and remove the margins. Thanks for the code example, but my example code is no worse and gives me the result I want. I just assumed there was an easier (less code) way of doing this.


Can make use of "graphic" property of Button Control.




Gregg Bolinger wrote:I added an image to it however you can still see the Button underneath because of insets/margins and I can't seem to find out how to remove them



Any one got a solution for this?
Jim Clarke
author
Ranch Hand

Joined: Jun 15, 2009
Posts: 49
That was my point, you can use Graphic, but the underlying border will still be shown.

The 2 options are: use CustomNode as I had shown, or use Button but write your own Skin.

Each Control has a
skin
variable and a Skin implements
. Each skin
in turn has a
behavior
variable,
javafx.scene.control.Behavior
.

So you could write you own skin.
Then, create the Button as:



Both options would be about the same amount of work, but the skin option
is a better design option.

Gregg Bolinger
GenRocket Founder
Ranch Hand

Joined: Jul 11, 2001
Posts: 15299
    
    6

Thanks Jim.
Mohamed Sanaulla
Saloon Keeper

Joined: Sep 08, 2007
Posts: 3071
    
  33

Thanks a lot. That was resourceful.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: JavaFX Button: You'd think this would be simple....