I am using FontAwesomeFX for many icons throughout my application. I would like to use them as icons for my Stage as well.
Since FontAwesomeIconView extends GlyphIcon which extends Text, I can not use it as an Image directly.
Is there a way to create a usable Image from a Text object?
I have tried to use snapshot, but I am not familiar with that method and end up with the standard "empty" icon on my stage.
Here is the MCVE I have so far:
import de.jensd.fx.glyphs.fontawesome.FontAwesomeIcon;
import de.jensd.fx.glyphs.fontawesome.FontAwesomeIconView;
import javafx.application.Application;
import javafx.geometry.Insets;
import javafx.geometry.Pos;
import javafx.scene.Scene;
import javafx.scene.SnapshotParameters;
import javafx.scene.image.WritableImage;
import javafx.scene.layout.VBox;
import javafx.stage.Stage;
public class FontAwesomeIconExample extends Application {
public static void main(String[] args) {
launch(args);
}
@Override
public void start(Stage primaryStage) {
// Simple Interface
VBox root = new VBox(10);
root.setAlignment(Pos.CENTER);
root.setPadding(new Insets(10));
// Convert the FontAwesomeIconView node to an Image
FontAwesomeIconView iconView = new FontAwesomeIconView(FontAwesomeIcon.ID_CARD_ALT);
WritableImage icon = iconView.snapshot(new SnapshotParameters(), null);
primaryStage.getIcons().add(icon);
// Show the stage
primaryStage.setWidth(300);
primaryStage.setHeight(100);
primaryStage.setScene(new Scene(root));
primaryStage.setTitle("Sample");
primaryStage.show();
}
}
And the result:
Is it possible to use a node snapshot as a stage icon? Or is there a better method of converting the FontAwesomeIconView for this purpose?
EDIT:
I have tried both Sedrick's and JKostikiadis's methods and both give similar results for me.
Sedrick's:
// Convert the FontAwesomeIconView node to an Image
FontAwesomeIconView iconView = new FontAwesomeIconView(FontAwesomeIcon.AMAZON);
WritableImage icon = iconView.snapshot(new SnapshotParameters(), null);
java.awt.image.BufferedImage bImage = SwingFXUtils.fromFXImage(icon, null);
ByteArrayOutputStream s = new ByteArrayOutputStream();
try {
javax.imageio.ImageIO.write(bImage, "png", s);
} catch (IOException e) {
e.printStackTrace();
}
JKostikiadis's:
FontAwesomeIconView iconView = new FontAwesomeIconView(FontAwesomeIcon.AMAZON);
WritableImage writableImg = iconView.snapshot(null, null);
Image img = SwingFXUtils.toFXImage(SwingFXUtils.fromFXImage(writableImg, null), null);
primaryStage.getIcons().add(img);
I believe they both accomplish essentially the same thing (with the sizing of the Image resulting in different displayed icons).
What I do not understand now, is why this is the icon it is producing for me and not them...
I am running Windows 7 and JDK 1.8.0_161.





