在javafx中填充fxml文件中的类别轴

时间:2017-01-18 13:35:57

标签: javafx-8 fxml

我在scatter chart文件中有一个fxml,我想在其类别轴上添加一些值。我想在fxml文件中执行此操作。如何在javafx中将这些值添加到category axis

<ScatterChart fx:id="scatterChart" title="title 1" titleSide="BOTTOM" BorderPane.alignment="CENTER">
    <xAxis  fx:id="xaxis">
        <NumberAxis lowerBound="-1.0" minorTickCount="0" side="BOTTOM" upperBound="1.0" />
    </xAxis>
    <yAxis>
        <CategoryAxis side="LEFT" />
    </yAxis  fx:id="yaxis">
</ScatterChart>

1 个答案:

答案 0 :(得分:0)

CategoryAxis使用ObservableList<String>方法<categories>,因此您需要在<CategoryAxis>元素中使用ObservableList<String>元素。创建和填充fx:factory需要对setCategories()进行一些挖掘,但基本上您需要fx:value来创建列表,String来创建<?xml version="1.0" encoding="UTF-8"?> <?import javafx.scene.layout.BorderPane?> <?import javafx.scene.chart.ScatterChart?> <?import javafx.scene.chart.NumberAxis?> <?import javafx.scene.chart.CategoryAxis?> <?import javafx.collections.FXCollections?> <?import java.lang.String?> <BorderPane xmlns:fx="http://javafx.com/fxml/1"> <center> <ScatterChart fx:id="scatterChart" title="title 1" titleSide="BOTTOM" BorderPane.alignment="CENTER"> <xAxis > <NumberAxis fx:id="xaxis" lowerBound="-1.0" minorTickCount="0" side="BOTTOM" upperBound="1.0" /> </xAxis> <yAxis> <CategoryAxis fx:id="yaxis" side="LEFT" > <categories> <FXCollections fx:factory="observableArrayList"> <String fx:value="One"/> <String fx:value="Two"/> <String fx:value="Three" /> <String fx:value="Four" /> <String fx:value="Five" /> </FXCollections> </categories> </CategoryAxis> </yAxis > </ScatterChart> </center> </BorderPane> 个实例:

import java.io.IOException;

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

public class CategoryAxisExample extends Application {

    @Override
    public void start(Stage primaryStage) throws IOException {
        primaryStage.setScene(new Scene(FXMLLoader.load(getClass().getResource("ScatterChart.fxml")), 600, 600));
        primaryStage.show();
    }

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

测试类:

max-height

截图:

FXML documentation