Lakshitha Kanchana
Lakshitha Kanchana

Reputation: 1235

How to fix changing fontawesomefx icons to a rectangle when the font size is specified in JavaFX?

I'm using fontawesomefx-8.9 and I have set their font size to 15px using

.glyph-icon {
  -fx-font-size: 15px;
}

Then I have created some FontAwesomeIconViews and embed them in some Buttons(JavaFX).

The issue I had to face is changing fontAwesome icons to a rectangle when hovering mouse over the button.

enter image description here

FXML File is below(FXMLDocument,fxml)

<?xml version="1.0" encoding="UTF-8"?>

<?import de.jensd.fx.glyphs.fontawesome.FontAwesomeIconView?>
<?import javafx.scene.control.Button?>
<?import javafx.scene.control.Label?>
<?import javafx.scene.layout.AnchorPane?>

<AnchorPane id="AnchorPane" prefHeight="200" prefWidth="320" stylesheets="@styles.css" xmlns:fx="http://javafx.com/fxml/1" xmlns="http://javafx.com/javafx/8.0.111" fx:controller="test_fontawesome.FXMLDocumentController">
    <children>
        <Button fx:id="button" contentDisplay="TOP" layoutX="44.0" layoutY="41.0" onAction="#handleButtonAction" text="Click Me!" AnchorPane.leftAnchor="10.0" AnchorPane.topAnchor="40.0">
         <graphic>
            <FontAwesomeIconView fill="BLUE" glyphName="CUT" />
         </graphic></Button>
        <Label fx:id="label" layoutX="126" layoutY="120" minHeight="16" minWidth="69" />
      <Button fx:id="button1" contentDisplay="TOP" layoutX="183.0" layoutY="41.0" onAction="#handleButtonAction" text="Click Me!" AnchorPane.rightAnchor="10.0" AnchorPane.topAnchor="40.0">
         <graphic>
            <FontAwesomeIconView fill="BLUE" glyphName="PLUS" />
         </graphic>
      </Button>
    </children>
</AnchorPane>

CSS is file below(styles.css)

.root {
    -fx-font-size: 12px ;
}

Just preview it on scene builder, the issue will appear!

If you want the source code, get it from here.

Upvotes: 2

Views: 4848

Answers (3)

ivanoklid
ivanoklid

Reputation: 91

If you change the font-size, you must set the font-family for icon.

.glyph-icon {
  -fx-font-size: 15px;
  -fx-font-family: "FontAwesome"; /* or Material Design Icons*/
}

This fix my issue! ;)

Upvotes: 0

user68883
user68883

Reputation: 838

i had the same issue sometime back .. i remember adding the following line to my css file fixed the issue.

.glyph-icon{ -fx-font-family:"Material Design Icons"; }

Looks like your are using FontAwesome icon , may be try with the appropriate font family for that.

  .glyph-icon{ -fx-font-family:"FontAwesome"; }

Upvotes: 9

Jens Deters
Jens Deters

Reputation: 21

You should consider to switch to the latest version of FontAwesomeFX. Yesterday I released 8.14: http://www.jensd.de/wordpress/?p=2579 8.14 comes with a fix of these resizing issues!

Upvotes: 2

Related Questions