java - Styling a JavaFX 2 button using FXML only - How to add an image to a button? -


i want change styling of button, of threads here , articles on internet show how using java code, don't see real solution, there way example set button text , image inside using fxml (no css) ?

solution using fxml

as tarrsalah points out, css recommended way of doing this, though can in fxml if prefer:

<?import java.lang.*?> <?import java.util.*?> <?import javafx.scene.control.*?> <?import javafx.scene.image.*?> <?import javafx.scene.layout.*?> <?import javafx.scene.paint.*?> <?import javafx.scene.text.*?>  <anchorpane id="anchorpane" maxheight="-infinity" maxwidth="-infinity" minheight="-infinity" minwidth="-infinity" prefheight="400.0" prefwidth="600.0" xmlns:fx="http://javafx.com/fxml">   <children>     <button layoutx="104.0" layouty="81.0" mnemonicparsing="false" text="love&#10;potion">       <font>         <font size="30.0" />       </font>       <graphic>         <imageview fitheight="150.0" fitwidth="200.0" pickonbounds="true" preserveratio="true">           <image>             <image url="http://icons.iconarchive.com/icons/mirella-gabriele/fantasy-mediaeval/128/poison-red-icon.png" />           </image>         </imageview>       </graphic>     </button>   </children> </anchorpane> 

to above in scenebuilder, drag imageview on top of button , automatically set graphic button. select imageview , type url of image imageview's image field in scenebuilder properties pane.

open above fxml in scenebuilder see image below.

lovepotion


alternate css attributes

alternate css -fx-background* attributes.

  • -fx-graphic
  • -fx-padding
  • -fx-content-display
  • -fx-graphic-text-gap

these different, not better trying do. preference thing use. find these settings easier use -fx-background* settings. more restrictive, syntax , options easier me understand , meanings map javadoc api labeled.

a detailed description of attributes in css reference guide.

here's example style setting graphic embedded in fxml, though better separate style information out separate css stylesheet in tarrsalah's sample.

<button layoutx="138.0" layouty="226.0" mnemonicparsing="false" style="-fx-graphic: url('http://icons.iconarchive.com/icons/mirella-gabriele/fantasy-mediaeval/128/poison-red-icon.png')" text="love&#10;potion">   <font>     <font size="20.0" />   </font> </button> 

related solutions adding images buttons using java code


Comments

Popular posts from this blog

linux - Does gcc have any options to add version info in ELF binary file? -

android - send complex objects as post php java -

charts - What graph/dashboard product is facebook using in Dashboard: PUE & WUE -