Changing Text Fonts Using FXML - Java JavaFX

Java examples for JavaFX:Text

Description

Changing Text Fonts Using FXML

Demo Code

import javafx.application.Application;
import javafx.fxml.FXMLLoader;
import javafx.scene.Scene;
import javafx.stage.Stage;

public class Main extends Application {
    //ww  w . j  av a  2  s . c  o m
    @Override
    public void start(Stage stage) throws Exception {
        stage.setScene((Scene) FXMLLoader.load(getClass().getResource("textfonts.fxml")));
        stage.show();
    }

    public static void main(String[] args) {
        launch(args);
    }
}

FXML source

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

<?import java.net.*?> 
<?import javafx.geometry.*?> 
<?import javafx.scene.*?> 
<?import javafx.scene.control.*?> 
<?import javafx.scene.layout.*?> 
<?import javafx.scene.text.*?> 

<Scene width="200" height="75" fill="white" xmlns:fx="http://javafx.com/fxml"> 
    <stylesheets> 
        <URL value="@textfonts.css"/> 
    </stylesheets> 
    <Pane fx:id="pane"> 

         <TextFlow styleClass="mainmessage"> 
            <Text styleClass="span1">Hello </Text> 
            <Text text=" "/> 
            <Text styleClass="span2, large">Java</Text> 
            <Text styleClass="span3, slant">FX</Text> 
            <Text text=" "/> 
            <Text styleClass="cool">8</Text> 

        </TextFlow> 
    </Pane> 
</Scene> 

CSS File

.mainmessage { 
    -fx-font-family: "Helvetica"; 
    -fx-font-size: 30px; 
} 

.span1 { 
    -fx-color: "red"; 
} 

.span2 { 
    -fx-font-family: "Serif"; 
    -fx-font-size: 30px; 
    -fx-color: "red"; 
} 

.span3 { 
    -fx-font-family: "Serif"; 
    -fx-font-size: 30px; 
    -fx-fill: "orange"; 
    -fx-font-style: italic; 
} 

.cool { 
    -fx-effect: dropshadow(gaussian, gray, 8, 0.5, 8, 8); 
} 

Related Tutorials