Joao Spirit
Joao Spirit

Reputation: 133

Set icons to JFXButton

How to add an icon or a fontawesome icon to a JFXButton from the controller class?

public void initialize() {
    JFXButton hamburger = new JFXButton();
} 

Upvotes: 0

Views: 1192

Answers (2)

smithnblack
smithnblack

Reputation: 487

Extending @Mayur Patel answer, use JFXButton constructor with text and graphic, see documentation JFXButton:

public JFXButton(String text, Node graphic) {
    super(text, graphic);
    initialize();
}

So in your case, it should look like this:

Image image = new Image(getClass().getResourceAsStream("icon.png"));
JFXButton hamburger = new JFXButton("Try me",image);

And with fontawesome icons (remember to import it first in your scene builder, if you are working with scene builder):

 <JFXButton fx:id="delete_btn" text="Try me">
   <graphic>
     <FontAwesomeIconView fill="WHITE" glyphName="play" size="16.0"/>
   </graphic>
 </JFXButton>

Upvotes: 2

Mayur Patel
Mayur Patel

Reputation: 339

Try this, it might work -

JFXButton hamburger = new JFXButton();
Image image = new Image(getClass().getResourceAsStream("icon.png"));
hamburger.setGraphic(new ImageView(image));

Upvotes: 2

Related Questions