Wednesday, June 6, 2012

Set font of text using FXML vs JavaFX

Example to set font using FXML:

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

<?import java.lang.*?>
<?import java.util.*?>
<?import javafx.scene.*?>
<?import javafx.scene.control.*?>
<?import javafx.scene.layout.*?>
<?import javafx.scene.text.*?>

<AnchorPane id="AnchorPane" prefHeight="200" prefWidth="320" xmlns:fx="http://javafx.com/fxml" fx:controller="javafxml_text.Sample">
    <children>

        <Text layoutX="10" layoutY="30" stroke="BLACK" text="Set Font in FXML">
            <font>
                <Font name="Arial Black" size="24.0" />
            </font>
        </Text>
        
    </children>
</AnchorPane>


Set font in FXML


Example to set font using JavaFX:

package javafx_text;

import javafx.application.Application;
import javafx.scene.Scene;
import javafx.scene.layout.StackPane;
import javafx.scene.text.Font;
import javafx.scene.text.Text;
import javafx.stage.Stage;

/**
 *
 * @web http://java-buddy.blogspot.com/
 */
public class JavaFX_Text extends Application {

    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) {
        launch(args);
    }
    
    @Override
    public void start(Stage primaryStage) {

        Text text = new Text();
        text.setText("Text Font in JavaFX");
        text.setFont(Font.font("Arial Black", 24.0));

        StackPane root = new StackPane();
        root.getChildren().add(text);
        
        Scene scene = new Scene(root, 300, 250);
        
        primaryStage.setTitle("java-buddy.blogspot.com");
        primaryStage.setScene(scene);
        primaryStage.show();
    }
}


Set font in JavaFX


1 comment: