DFansler
Posts: 13
Joined: Sun Aug 31, 2014 12:14 pm
Location: North Carolina, USA
Contact: Website

JavaFX problems

Wed Nov 19, 2014 7:22 pm

Greetings - I am building a sports car form scratch and want to use my Pi as the main display for fuel, speed, rpm, etc. Another microprocessor will be collecting and formatting the data and passing it to the Pi via RS232. The Pi will display speed, rpm, fuel, etc.

i am new to Java, but have been programming microprocessors since 1976.

When I posted a query in the Oracle form requesting help with binding, a gentleman wrote a goo chunk of the program that accepts data from a telnet session and displays it.

My only issue was that I am using Scene Builder with an AnchorPane, Image and labels to display the data. His example used an fxml file with a HBox and labels inside of that. So I changed the fxml file, but cannot get data to populate in the window. His complete code is at:
https://gist.github.com/james-d/95457d1ac1a90e063337

His fxml file looks like :

Code: Select all

<?xml version="1.0" encoding="UTF-8"?>
	
<?import javafx.scene.layout.HBox?>
<?import javafx.scene.control.Label?>
<?import javafx.geometry.Insets?>
	
<HBox xmlns:fx="http://javafx.com/fxml/1" spacing="5" 
	alignment="CENTER" fx:controller="streamingdatafx.DisplayController">
	
	<padding>
		<Insets top="10" left="10" right="10" bottom="10"/>
	</padding>
	
	<Label text="Speed:"/>
	<Label fx:id="speedLabel"/>
</HBox>
My fxml file looks lihe

Code: Select all

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

<?import javafx.scene.text.*?>
<?import javafx.scene.image.*?>
<?import java.lang.*?>
<?import javafx.geometry.*?>
<?import javafx.scene.control.*?>
<?import javafx.scene.layout.*?>
<?import javafx.scene.layout.HBox?>
<?import javafx.scene.control.Label?>
<?import javafx.geometry.Insets?>

<AnchorPane maxHeight="-Infinity" maxWidth="-Infinity" minHeight="-Infinity" minWidth="-Infinity" prefHeight="480.0" prefWidth="800.0" xmlns="http://javafx.com/javafx/8" xmlns:fx="http://javafx.com/fxml/1">
   <children>
      <ImageView fitHeight="480.0" fitWidth="800.0" pickOnBounds="true" preserveRatio="true">
         <image>
            <Image url="@../../../../Documents/NetBeansProjects/Test/src/Main_Display.png" />
         </image>
      </ImageView>
      <HBox prefHeight="100.0" prefWidth="200.0">
         <children>
 	<Label text="Speed:" />
         </children>
      </HBox>
	<Label id="speedLabel" layoutX="114.0" layoutY="146.0" prefHeight="70.0" prefWidth="191.0" textFill="#e8e3e3">
         <font>
            <Font size="48.0" />
         </font>
      </Label>
   </children>
</AnchorPane>
and a segment of the main.java :

Code: Select all

public class Main extends Application {
    
    private DataStreamParser dataStream ;
    
	@Override
	public void start(Stage primaryStage) {
		try {
		    FXMLLoader loader = new FXMLLoader(getClass().getResource("Display.fxml"));
			AnchorPane root = loader.load();
			DisplayController controller = loader.getController();
			
			dataStream = new DataStreamParser();
			dataStream.speedProperty().addListener((obs, oldSpeed, newSpeed) -> {
			    // update controller on FX Application Thread:
			    Platform.runLater(() -> controller.setSpeed(newSpeed.doubleValue()));
			});
			
			Scene scene = new Scene(root,400,400);
			primaryStage.setScene(scene);
			primaryStage.show();
		}
Where it has HBox, used to be, I have substituted AnchorPane.
The form will run and display speed, but the program throws an exception at at the line
Platform.runLater(() -> controller.setSpeed(newSpeed.doubleValue()));

Any suggestions would be welcome.
Kind regards,
David

Return to “Java”