scenebuilder javafx线图 [英] scenebuilder javafx linechart

查看:260
本文介绍了scenebuilder javafx线图的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以这是交易,我正在尝试编写一个在线图上显示实时数据的GUI。
到目前为止一切都那么好,我可以让线图工作但不能进入GUI。

So here's the deal, i'm trying to code a GUI that shows live data on a linechart. So far so good, i can get the linechart working but not into the GUI.

使用scenebuilder,我用一个线图对象做了一个视图,按顺序将它链接到我生成的图表。但由于某些原因,这似乎不适用于我的mainApp中的此代码。

Using scenebuilder, i made a view with a linechart object, in order to link it to my generated chart. But for some reason this does not seem to work with this code in my mainApp.

public void showSes() {
    try {
        // Load the fxml file and set into the center of the main layout
        FXMLLoader loader = new FXMLLoader();
        loader.setLocation(MainApp.class.getResource("view/Session.fxml"));

        AnchorPane Session = (AnchorPane) loader.load();
        rootLayout.setCenter(Session);

        SessionController controller = loader.getController();
        controller.setMainApp(this);
        controller.initGraph();

    } catch (IOException e) {
        // Exception gets thrown if the fxml file could not be loaded
        e.printStackTrace();
    }
}

这只是显示带有空线图的视图。
我知道图表应该知道,因为我可以用它来创建一个场景,并在GUI中显示,但我在scenebuilder中制作的视图还有一些我想要显示的其他字段...

This simply shows the view with an empty linechart. I know the chart should know however, because i can use it to create a scene, and show that into the GUI, but the view i made in scenebuilder also has some other fields i want to show...

有没有人有想法?

FXML

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

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

<AnchorPane prefHeight="900.0" prefWidth="1280.0" xmlns="http://javafx.com/javafx/8" xmlns:fx="http://javafx.com/fxml/1" fx:controller="gui.view.SessionController">
   <children>
      <Pane prefHeight="900.0" prefWidth="1280.0" AnchorPane.bottomAnchor="0.0" AnchorPane.leftAnchor="0.0" AnchorPane.rightAnchor="0.0" AnchorPane.topAnchor="0.0">
         <children>
            <LineChart fx:id="linechart" layoutX="29.0" layoutY="194.0" prefHeight="416.0" prefWidth="1222.0" title="Temperature of session">
              <xAxis>
                <CategoryAxis label="Time (s)" fx:id="xAxis" />
              </xAxis>
              <yAxis>
                <NumberAxis fx:id="yAxis" label="Temperature (°C)" side="LEFT" upperBound="160.0" />
              </yAxis>
            </LineChart>
            <GridPane layoutX="254.0" layoutY="87.0" prefHeight="150.0" prefWidth="771.0">
              <columnConstraints>
                <ColumnConstraints hgrow="SOMETIMES" maxWidth="274.0" minWidth="10.0" prefWidth="274.0" />
                  <ColumnConstraints hgrow="SOMETIMES" maxWidth="273.0" minWidth="10.0" prefWidth="273.0" />
                <ColumnConstraints hgrow="SOMETIMES" maxWidth="273.0" minWidth="10.0" prefWidth="273.0" />
              </columnConstraints>
              <rowConstraints>
                <RowConstraints minHeight="10.0" prefHeight="30.0" vgrow="SOMETIMES" />
              </rowConstraints>
               <children>
                  <Label prefHeight="53.0" prefWidth="153.0" text="Temperature fluid:" GridPane.halignment="CENTER" GridPane.valignment="TOP">
                     <font>
                        <Font size="16.0" />
                     </font>
                  </Label>
                  <Label prefHeight="52.0" prefWidth="181.0" text="Temperature vapor:" GridPane.columnIndex="2" GridPane.halignment="CENTER" GridPane.valignment="TOP">
                     <font>
                        <Font size="16.0" />
                     </font>
                  </Label>
                  <TextField fx:id="fluidT" editable="false" />
                  <TextField fx:id="gasT" editable="false" GridPane.columnIndex="2" />
               </children>
            </GridPane>
            <Label layoutX="474.0" layoutY="14.0" text="TempTracker">
               <font>
                  <Font size="50.0" />
               </font>
            </Label>
            <TextArea editable="false" layoutX="190.0" layoutY="638.0" prefHeight="160.0" prefWidth="900.0" promptText="&#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;------Warning log------- " wrapText="true" />
            <Button layoutX="540.0" layoutY="808.0" mnemonicParsing="false" onAction="#handleStop" prefHeight="65.0" prefWidth="200.0" text="STOP">
               <font>
                  <Font size="22.0" />
               </font>
            </Button>
         </children></Pane>
   </children>
</AnchorPane>

CONTROLLER

CONTROLLER

    private static final int MAX_DATA_POINTS = 50;
    private String xSeriesData = "";
    private XYChart.Series series1;
    private XYChart.Series series2;
    private ExecutorService executor;
    private BlockingQueue<Number> dataQ1 = new ArrayBlockingQueue<Number>(1024);
    private BlockingQueue<Number> dataQ2 = new ArrayBlockingQueue<Number>(1024);

    @FXML
    private CategoryAxis xAxis = new CategoryAxis();
    @FXML
    final NumberAxis yAxis = new NumberAxis();
    @FXML
    final LineChart<String, Number> linechart = new LineChart<String, Number>(xAxis, yAxis);


public void initGraph(){
    xAxis.setAutoRanging(false);

    xAxis.setTickLabelsVisible(false);
    xAxis.setTickMarkVisible(false);

    NumberAxis yAxis = new NumberAxis();
    yAxis.setAutoRanging(true);

    //Graph
    final LineChart<String, Number> lc = new LineChart<String, Number>(xAxis, yAxis){
            @Override 
            protected void dataItemAdded(Series<String, Number> series, int itemIndex, Data<String, Number> item){}
            };
    lc.setAnimated(false);
    lc.setId("liveLineChart");
    lc.setTitle("Animated Line Chart");

    //Graph Series
    series1 = new XYChart.Series<Number, Number>();
    series2 = new XYChart.Series<Number, Number>();
    linechart.getData().addAll(series1, series2);

    series1.setName("T1");
    series2.setName("T2");

    fluidT.setText("0000");
    gasT.setText("0000");

    prepareTimeline();

    Runnable con = new Consumer(this);
    Thread c = new Thread(con);
    c.start();

}


推荐答案

不要为@FXML注入成员创建新对象

切勿结合使用 new @FXML ,即永不写:

Never use new in conjunction with @FXML, i.e., never write:

@FXML 
private CategoryAxis xAxis = new CategoryAxis();

相反,只需写:

@FXML 
private CategoryAxis xAxis;

FXMLLoader 将自动生成,即为FXML文件中的每个元素创建一个新对象,并将其引用到您的控制器中,在该控制器中提供 @FXML 注释。因此,如果重置 @FXML 成员对您在控制器中创建的新对象的引用,则该对象与加载器创建的对象之间不会存在关联。

The FXMLLoader will automatically generate, i.e., create, a new object for each element in the FXML file and inject a reference to that into your controller where you provide an @FXML annotation. So if you reset the @FXML member reference to a new object you create in the controller there will be no association between that object and objects created by the loader.

此外,不要在initGraph()函数中创建另一个新的LineChart。您已经有一个由FXMLLoader创建的LineChart,只需参考。对于NumberAxis和使用@FXML注入的其他元素也是如此。

Additionally, don't create another new LineChart within your initGraph() function. You already have a LineChart created by the FXMLLoader, just reference that. Same for NumberAxis and the other elements you are using @FXML injection with.

如果您使用 @FXML 注释也使用< fx:id>

If you use an @FXML annotation also use <fx:id>

您有:

@FXML
private CategoryAxis xAxis;

所以在你的fxml中,你应该定义:

So in your fxml, you should define:

<xAxis fx:id="xAxis">

否则,FXMLLoader将无法为您在FXML中定义的轴注入引用。

Otherwise, the FXMLLoader will not be able to inject a reference to the axis you defined in your FXML.

除了

您的代码中可能还有其他错误(例如,并发和线程)。所以上面可能不是你的所有错误。通常,在创建mcve时,尝试消除与手头问题无关的任何事情(例如FXML的线程代码和非线图部分),但包括某人可用于复制和粘贴代码的所有内容。编译并运行它来复制你的问题。

You may have other errors in your code (e.g., around concurrency and threading). So the above might not be all of your errors. In general, when creating an mcve, try to eliminate anything which is not relevant to the question at hand (e.g. the threading code and non-linechart portions of the FXML), but include everything that somebody could use to copy and paste your code to compile and run it to replicate your issue.

注意: Ensemble 示例应用程序包含一个示例程序,它根据音频频谱输入数据实时更新图形。

Note: The Ensemble sample application contains a sample program which updates a graph in real-time based upon audio spectrum input data.

这篇关于scenebuilder javafx线图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆