Answer the question
In order to leave comments, you need to log in
How to display your custom fonts from folder in combobox?
For the first time I get acquainted with combobox. According to the tutorial, I made the display of string values in a combobox.
I'm writing a JavaFX program.
I need my custom fonts, which are in the program folder, to be displayed in the combobox, and then when one of these fonts is selected, the font is applied to the text in the Label
How to do this?
FontController.java:
package card;
import javafx.collections.FXCollections;
import javafx.collections.ObservableList;
import javafx.event.ActionEvent;
import javafx.fxml.FXML;
import javafx.fxml.Initializable;
import javafx.scene.control.ComboBox;
import javafx.scene.control.Label;
import java.net.URL;
import java.util.ResourceBundle;
public class FontController implements Initializable {
@FXML private ComboBox<String> fontSelector;
@FXML private Label fontLabel;
ObservableList<String> list = FXCollections.observableArrayList("Apple", "Banana", "Peach", "Juice");
@Override
public void initialize(URL location, ResourceBundle resources) {
fontSelector.setItems(list);
}
public void changeLabel(ActionEvent event) {
fontLabel.setText(fontSelector.getValue());
}
}
<?xml version="1.0" encoding="UTF-8"?>
<?import javafx.scene.control.ComboBox?>
<?import javafx.scene.control.Label?>
<?import javafx.scene.layout.AnchorPane?>
<?import javafx.scene.text.Font?>
<AnchorPane prefHeight="300.0" prefWidth="500.0" style="-fx-background-color: green;" xmlns="http://javafx.com/javafx/10.0.1" xmlns:fx="http://javafx.com/fxml/1" fx:controller="card.FontController">
<children>
<ComboBox fx:id="fontSelector" layoutX="14.0" layoutY="54.0" onAction="#changeLabel" prefWidth="150.0" promptText="Select Font" />
<Label fx:id="fontLabel" layoutX="155.0" layoutY="180.0" prefHeight="67.0" prefWidth="190.0" text="Selected Font" textFill="WHITE">
<font>
<Font size="23.0" />
</font>
</Label>
</children>
</AnchorPane>
Answer the question
In order to leave comments, you need to log in
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question