This week's book giveaway is in the Flex forum.
We're giving away four copies of Flex 4 in Action and have Tariq Ahmed, Dan Orlando, John C. Bland II & Joel Hooks on-line!
See this thread for details.
[Logo] JavaRanch » JavaRanch Saloon
  Search | FAQ | Recent Topics | Hot Topics
Register / Login


Win a copy of Flex 4 in Action this week in the Flex forum!
Reply Bookmark it! Watch this topic JavaRanch » Forums » Java » Java FX
 
RSS feed
 
New topic
Author

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

Gregg Bolinger
Sheriff

Joined: Jul 11, 2001
Messages: 13664

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?

"Fookin Prawns"
Michael Dunn
Rancher

Joined: Jun 09, 2003
Messages: 3090

"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
Messages: 45

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
Sheriff

Joined: Jul 11, 2001
Messages: 13664

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.

"Fookin Prawns"
Jim Clarke
author
Ranch Hand

Joined: Jun 15, 2009
Messages: 45

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.
mohammed sanaullah
Ranch Hand

Joined: Sep 08, 2007
Messages: 39

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
Final Year, B.Tech
National Institute of Technology Karnataka, India
Gregg Bolinger
Sheriff

Joined: Jul 11, 2001
Messages: 13664

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


"Fookin Prawns"
mohammed sanaullah
Ranch Hand

Joined: Sep 08, 2007
Messages: 39

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?

Mohamed Sanaulla
Final Year, B.Tech
National Institute of Technology Karnataka, India
Jim Clarke
author
Ranch Hand

Joined: Jun 15, 2009
Messages: 45

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
javafx.scene.control.Skin
. 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.

This message was edited 1 time. Last update was at by Jim Clarke

Gregg Bolinger
Sheriff

Joined: Jul 11, 2001
Messages: 13664

Thanks Jim.

"Fookin Prawns"
mohammed sanaullah
Ranch Hand

Joined: Sep 08, 2007
Messages: 39

Thanks a lot. That was resourceful.

Mohamed Sanaulla
Final Year, B.Tech
National Institute of Technology Karnataka, India
 
 
 
Reply Bookmark it! Watch this topic JavaRanch » Forums » Java » Java FX
 
RSS feed
 
New topic
IntelliJ open source