Real-time LineChart JavaFx - javafx

I have a problem with my GUI. I am trying to create a Gui which displays some analog values stored by the Arduino microcontroller. I am able to store all data I want in an array. Now I am using SceneBuilder and JavaFx to plot the arrays of this analog signal and I see the real signal but the graph is autoscaling and scattering . I would like to have more something like the following:
Advanced Stock Line Chart example in JAVAFX
JavaFX Example
Here is my code. The function updateGraph1(...) is called everytime that I receive a value which can update the graph.
package de.zft.degreen.view;
import java.io.IOException;
import java.net.URL;
import java.util.ResourceBundle;
import java.util.Set;
import javax.swing.event.ChangeEvent;
import javax.swing.event.ChangeListener;
import javafx.application.Application;
import javafx.application.Platform;
import javafx.collections.FXCollections;
import javafx.collections.ObservableList;
import javafx.fxml.FXML;
import javafx.fxml.Initializable;
import javafx.fxml.FXMLLoader;
import javafx.scene.Node;
import javafx.scene.Scene;
import javafx.scene.chart.LineChart;
import javafx.scene.chart.NumberAxis;
import javafx.scene.chart.ValueAxis;
import javafx.scene.chart.XYChart;
import javafx.scene.layout.BorderPane;
import javafx.scene.layout.Pane;
import javafx.stage.Stage;
public class Gui extends Application implements Runnable, Initializable {
#FXML
private static LineChart<Number,Number> graph1;
public static XYChart.Series<Number, Number> series;
private ChangeListener changeListener;
private static Scene scene;
public Gui() {
series = new XYChart.Series<>();
series.setName("bye");
}
public ChangeListener getChangeListener() {
return changeListener;
}
public void setChangeListener(ChangeListener changeListener) {
this.changeListener = changeListener;
}
#Override
public void start(Stage primaryStage) throws Exception {
System.out.println("Start start");
primaryStage.setTitle("DEGREEN GUI");
System.out.println("Before");
Pane myPane = FXMLLoader.load(getClass().getResource("Gui2.fxml"));
System.out.println("After");
System.out.println("Step1");
scene = new Scene(myPane);
System.out.println("Step2");
primaryStage.setScene(scene);
System.out.println("Step3");
primaryStage.show();
System.out.println("End start");
}
private void init(Stage primaryStage){
System.out.println("Start Init");
}
#FXML
public void updateGraph1(Long long1, int v){
System.out.println("Start updateGraph1"+series.getName());
final int q = v;
final long long2 = long1;
Platform.runLater(new Runnable() {
#Override
public void run() {
add2Series(long2,q,100);
}
});
System.out.println("End updateGraph1");
}
#Override
public void run() {
System.out.println("Gui Initialized");
launch();
System.out.println("Gui Ended");
}
#FXML
public void initialize(URL arg0, ResourceBundle arg1) {
System.out.println("Start Initialize"+series.getName());
series = new XYChart.Series<Number,Number>();
series.setName("Voltage DE1");
graph1.getData().add(series);
graph1.setAnimated(false);
System.out.println("End Initialize"+series.getName());
}
#FXML
protected static void add2Series(Number x, Number y, int displayedSize) {
int seriesSize = series.getData().size();
if (seriesSize >= displayedSize) {
series.getData().remove(0);
}
series.getData().add(new XYChart.Data<Number,Number>(x,y));
}
}
Someone Can help me please?

Take a look at this sample . The visible categories are managed in runtime so that only last ten added are visible.

Related

Editable ComboBox cleared when i pressed space or arrow buttons - JavaFX

I am making a Editable ComboBox which show any values contain the input and it works fine.
But the problem is in the input field, whenever i typed Space or arrow buttons the input field keep reseting.
I tried setonKeyPressed and setonKeyTyped too but it isn't solve the problem.
import javafx.application.Application;
import javafx.collections.FXCollections;
import javafx.collections.ObservableList;
import javafx.event.EventHandler;
import javafx.scene.Scene;
import javafx.scene.control.ComboBox;
import javafx.scene.input.KeyEvent;
import javafx.scene.layout.HBox;
import javafx.stage.Stage;
public class Main extends Application {
#Override
public void start(Stage primaryStage) throws Exception{
ObservableList<String> fruit = FXCollections.observableArrayList();
fruit.add("apple");
fruit.add("orange");
fruit.add("banana");
ComboBox myComboBox = new ComboBox();
myComboBox.setEditable(true);
myComboBox.setOnKeyReleased(new EventHandler<KeyEvent>() {
#Override
public void handle(KeyEvent keyEvent) {
myComboBox.getItems().clear();
myComboBox.show();
String input = myComboBox.getEditor().getText();
for ( int i = 0 ; i < fruit.size(); i++) {
if(fruit.get(i).contains(input)) { //Check if the list contains the input
myComboBox.getItems().add(fruit.get(i));
}
}
}
});
HBox hbox = new HBox(myComboBox);
primaryStage.setScene(new Scene(hbox, 300, 275));
primaryStage.show();
}
public static void main(String[] args) {
launch(args);
}
}

Error of UDP server in javaFx

I made the source code of UDP server. the task of code shows figure when it received data via UDP communication. I used thread to receive data from other PC via UDP communication.
I use udp test tool from the other PC to check the code.
But, when startup my jar of source code in first, then the udp test tool can't bind the port .
what is the problem of my source code?
(In case that I set different values as the bind's port and send port in other PC's udp test tool,then my source code shows successfully the figure.)
package javafxapplication15;
import javafx.application.Application;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.scene.Group;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.ButtonBuilder;
import javafx.scene.layout.FlowPane;
import javafx.scene.layout.FlowPaneBuilder;
import javafx.scene.layout.HBox;
import javafx.scene.layout.HBoxBuilder;
import javafx.scene.layout.StackPane;
import javafx.scene.layout.VBox;
import javafx.scene.layout.VBoxBuilder;
import javafx.stage.Stage;
import javafx.stage.StageBuilder;
import java.io.*;
import java.net.*;
import java.util.logging.Level;
import java.util.logging.Logger;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import javafx.animation.KeyFrame;
import javafx.animation.Timeline;
import javafx.application.Platform;
import javafx.collections.FXCollections;
import javafx.collections.ObservableList;
import javafx.concurrent.ScheduledService;
import javafx.concurrent.Service;
import javafx.concurrent.Task;
import javafx.geometry.Pos;
import javafx.scene.chart.LineChart;
import javafx.scene.chart.NumberAxis;
import javafx.scene.chart.XYChart;
import javafx.scene.control.ComboBox;
import javafx.scene.control.Label;
import javafx.scene.control.LabelBuilder;
import javafx.scene.control.ProgressIndicator;
import javafx.scene.control.TextField;
import javafx.scene.control.TextFieldBuilder;
import javafx.scene.layout.GridPane;
import javafx.scene.text.Font;
import javafx.scene.text.FontPosture;
import javafx.scene.text.TextAlignment;
import javafx.util.Duration;
import sun.misc.HexDumpEncoder;
public class JavaFXApplication15 extends Application {
public DatagramSocket receivesocket;
public DatagramPacket receivepacket;
public DatagramSocket sendsocket;
public DatagramPacket sendpacket;
public InetSocketAddress remoteAdress;
public Label label;
public Label label_IP;
public Label label_IP_in;
public TextField tx_IP;
public Label label_SENDPORT;
public Label label_SENDPORT_in;
public Label label_RECEIVEPORT;
public Label label_RECEIVEPORT_in;
public Label label_status;
public TextField tx_SENDPORT;
public TextField tx_RECEIVEPORT;
public Button bt_co;
public Button bt1;
public Button bt2;
public Button bt_play ;
public ObservableList<String> play_num=null;
public ComboBox<String> comboBox;
private String message;
private XYChart.Series series;
private Timeline timer;
private ProgressIndicator indicator;
private static String s = null;
private static String IP = "192.168.101.30";
private static Integer RECEIVEPORT = 8084;
public double time_counter=0.0;
public double torque_receive_value=0.0;
public byte[] torque_Hex;
private String text;
private byte[] buf;
#Override
public void start(Stage stage) throws Exception{
/* timer */
timer = new Timeline(new KeyFrame(Duration.millis(1000), new EventHandler<ActionEvent>(){
#Override
public void handle(ActionEvent event) {
time_counter = time_counter+1;
}
}));
timer.setCycleCount(Timeline.INDEFINITE);
timer.play();
/* Figure*/
stage.setTitle("Line Chart Sample");
//defining the axes
final NumberAxis xAxis = new NumberAxis();
final NumberAxis yAxis = new NumberAxis();
xAxis.setLabel("Time [s]");
yAxis.setLabel("Force [N]");
//creating the chart
final LineChart<Number,Number> lineChart =
new LineChart<Number,Number>(xAxis,yAxis);
lineChart.setTitle("Receive Data");
//defining a series
series = new XYChart.Series();
series.setName("Torque");
series.getData().add(new XYChart.Data(0.0,0.0));
lineChart.getData().add(series);
VBox root = VBoxBuilder.create().spacing(25).children(lineChart).build();
Scene scene = new Scene(root);
recieve_UDP();
stage = StageBuilder.create().width(640).height(640).scene(scene).title("").build();
stage.show();
}
private void recieve_UDP() throws SocketException, IOException {
ScheduledService<Boolean> ss = new ScheduledService<Boolean>()
{
#Override
protected Task<Boolean> createTask()
{
Task<Boolean> task = new Task<Boolean>()
{
#Override
protected Boolean call() throws Exception
{
receivesocket = null;
byte[] receiveBuffer = new byte[1024];
receivepacket = new DatagramPacket(receiveBuffer, receiveBuffer.length);
receivesocket = new DatagramSocket(RECEIVEPORT);
receivesocket.receive(receivepacket);
message = new String(receivepacket.getData(),0, receivepacket.getLength());
torque_Hex = receivepacket.getData();
System.out.println(message);
if(message != "Status:Wait"){
Platform.runLater( () ->label_status.setText("Status:Done"));
/* show Figure */
Platform.runLater( () -> series.getData().add(new XYChart.Data(time_counter,Double.parseDouble( message))));
}
receivesocket.close();
return true;
};
};
return task;
}
};
ss.start();
}
public static void main(String[] args) {
launch(args);
}
}

JavaFX Stage not fully loaded

I have this whole mess of Controls and Transitions in the Scene, and whenever an error or message is made, I call FormBuilder.say(String x).
The sample included doesn't error as much, because it's empty. But when all the stuff are active, the pop up from FormBuilder.say(String x) comes out as a mess.
If I wait long enough, attempt to interact, or if I minimize then restore the Stage, it loads properly. But many times, it's just blank.
My laptop runs Ubuntu 15.10, with approx 2GB RAM. No Video Card.
Included is a screen cap of the messy picture.
Included is the code that's affected during the bug. I've striped out some of the less suspected parts. (EDITED)
package solo;
import javafx.animation.Animation;
import javafx.animation.TranslateTransition;
import javafx.application.Application;
import javafx.beans.value.ChangeListener;
import javafx.beans.value.ObservableValue;
import javafx.geometry.Pos;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.layout.Pane;
import javafx.scene.layout.VBox;
import javafx.scene.paint.Color;
import javafx.scene.shape.Rectangle;
import javafx.scene.text.Text;
import javafx.stage.Stage;
import javafx.stage.StageStyle;
import javafx.stage.Window;
import javafx.util.Duration;
public class FormBuilder extends Application{
Stage ps;
public FormBuilder(){
}
public void begin(String[] args){
launch(args);
}
public void start(Stage primaryStage) {
ps = primaryStage;
ps.setTitle("E-VIL v0.0");
ps.setScene(new Scene(scene(), 800, 600));
ps.show();
}
public void say(String things){
Stage stage = new Stage(StageStyle.UTILITY);
stage.initOwner(ps);
Text text = new Text(things);
Button btn = new Button("Okay");
btn.setOnAction(e -> {stage.close();});
VBox pane = new VBox(text,btn);
pane.setAlignment(Pos.TOP_CENTER);
text.setLayoutY(12);
pane.autosize();
stage.setScene(new Scene(pane));
stage.sizeToScene();
stage.setOnShown(e -> {stage.requestFocus();});
stage.showAndWait();
}
public Pane scene(){
Pane pn = new Pane();
Button btn = new Button("Say Something");
btn.setOnAction(e->{FormBuilder.this.say("Something");});
btn.setLayoutX(0);
btn.setLayoutY(10);
pn.getChildren().add(btn);
pn.sceneProperty().addListener(new ChangeListener<Scene>(){
#Override
public void changed(ObservableValue<? extends Scene> arg0, Scene arg1, Scene arg2) {
System.out.println("new Scene");
if(arg2 != null)
arg2.windowProperty().addListener(new ChangeListener<Window>(){
#Override
public void changed(ObservableValue<? extends Window> observable, Window oldValue,
Window newValue) {
System.out.println("new window");
if(newValue != null){
Rectangle rect = new Rectangle(0,30,300,300);
rect.setFill(Color.RED);
pn.getChildren().add(rect);
Rectangle box = new Rectangle(30,60,50,50);
TranslateTransition tt = new TranslateTransition(Duration.seconds(1), box);
tt.setByX(160);
tt.setByY(160);
tt.setAutoReverse(true);
tt.setCycleCount(Animation.INDEFINITE);
pn.getChildren().add(box);
tt.play();
}
}
});
}
});
return pn;
}
}
package solo;
public class Main {
public static void main(String args[]){
FormBuilder fb = new FormBuilder();
fb.begin(args);
}
}

JavaFX application won't compile in NetBeans IDE 8.1

I am trying to run this application in which I used event-handling and it won't compile and won't show anything. I am currently using NetBeans 8.1. What is that I did wrong. Also it is not showing any errors.
import javafx.application.Application;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.geometry.Pos;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.layout.HBox;
import javafx.scene.layout.StackPane;
import javafx.stage.Stage;
public class event extends Application {
public void start(Stage primaryStage)
{
HBox pane = new HBox();
pane.setAlignment(Pos.CENTER);
Button btOk = new Button("OK");
Button btCancel = new Button("Cancel");
OkHandlerClass handler1 = new OkHandlerClass();
btOk.setOnAction(handler1);
CancelHandlerClass handler2 = new CancelHandlerClass();
pane.getChildren().addAll(btOk, btCancel);
Scene scene = new Scene(pane);
primaryStage.setTitle("Handle the fucking event");
primaryStage.setScene(scene);
primaryStage.show();
}
class OkHandlerClass implements EventHandler<ActionEvent>{
public void handle(ActionEvent e) {
System.out.println("OK button clicked");
}
}
class CancelHandlerClass implements EventHandler<ActionEvent>{
public void handle(ActionEvent event) {
System.out.println("Cancel button clicked");
}
}
public static void main(String[] args) {
launch(args);
}
}

Get list of all JavaFX Tasks

Is there a way to get all running Tasks and Services in JavaFX application?
I would like to display the running Tasks in a List.
brian's answer is the way to go. If you have multiple places you are creating Tasks, this might get a bit tricky to manage. The following shows a way to encapsulate everything into an implementation of Executor. Note that I'm still doing exactly as brian suggests: adding Tasks to a List and removing them when they're complete; I just manage it all in an Executor so that the code to modify the list is all in one place.
MonitoringExecutor.java:
import java.util.concurrent.Executor;
import javafx.beans.value.ChangeListener;
import javafx.beans.value.ObservableValue;
import javafx.collections.FXCollections;
import javafx.collections.ObservableList;
import javafx.concurrent.Worker;
import javafx.concurrent.Worker.State;
/**
* Wraps an Executor and exposes an ObservableList of Workers which have been
* executed but have not completed. (Workers are considered completed if they
* exit a RUNNING state; i.e. they are in a SUCCEEDED, FAILED, or CANCELLED state.)
*
*/
public class MonitoringExecutor implements Executor {
private final Executor exec ;
private final ObservableList<Worker<?>> taskList ;
public MonitoringExecutor(Executor exec) {
this.exec = exec;
this.taskList = FXCollections.observableArrayList();
}
#Override
public void execute(Runnable command) {
if (command instanceof Worker) {
final Worker<?> task = (Worker<?>) command ;
task.stateProperty().addListener(new ChangeListener<State>() {
#Override
public void changed(ObservableValue<? extends State> obs,
State oldState, State newState) {
if (oldState == State.RUNNING) {
taskList.remove(task);
}
}
});
taskList.add(task);
}
exec.execute(command);
}
public ObservableList<Worker<?>> getWorkerList() {
return taskList;
}
}
And here's an example of using it:
import java.util.Arrays;
import java.util.Random;
import java.util.concurrent.Executors;
import java.util.concurrent.ThreadFactory;
import javafx.application.Application;
import javafx.beans.binding.Bindings;
import javafx.beans.property.IntegerProperty;
import javafx.beans.property.SimpleIntegerProperty;
import javafx.concurrent.Task;
import javafx.concurrent.Worker;
import javafx.concurrent.Worker.State;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.geometry.Insets;
import javafx.geometry.Pos;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.ProgressBar;
import javafx.scene.control.TableCell;
import javafx.scene.control.TableColumn;
import javafx.scene.control.TableView;
import javafx.scene.control.cell.PropertyValueFactory;
import javafx.scene.layout.BorderPane;
import javafx.scene.layout.HBox;
import javafx.stage.Stage;
import javafx.util.Callback;
public class TaskMonitor extends Application {
#Override
public void start(Stage primaryStage) {
final IntegerProperty tasksCreated = new SimpleIntegerProperty(0);
final ThreadFactory threadFactory = new ThreadFactory() {
#Override
public Thread newThread(Runnable r) {
Thread t = new Thread(r);
t.setDaemon(true);
return t;
}
};
final MonitoringExecutor exec = new MonitoringExecutor(Executors.newFixedThreadPool(5, threadFactory));
final TableView<Worker<?>> taskTable = createTable();
taskTable.setItems(exec.getWorkerList());
final Button newTaskButton = new Button();
newTaskButton.textProperty().bind(Bindings.format("Create task %d", tasksCreated.add(1)));
newTaskButton.setOnAction(new EventHandler<ActionEvent>() {
#Override
public void handle(ActionEvent event) {
tasksCreated.set(tasksCreated.get()+1);
exec.execute(new CountingTask("Task "+tasksCreated.get()));
}
});
final BorderPane root = new BorderPane();
root.setCenter(taskTable);
final HBox controls = new HBox();
controls.setPadding(new Insets(10));
controls.setAlignment(Pos.CENTER);
controls.getChildren().add(newTaskButton);
root.setBottom(controls);
final Scene scene = new Scene(root, 600, 400);
primaryStage.setScene(scene);
primaryStage.show();
}
private TableView<Worker<?>> createTable() {
final TableView<Worker<?>> taskTable = new TableView<>();
final TableColumn<Worker<?>, String> titleCol = new TableColumn<>("Title");
titleCol.setCellValueFactory(new PropertyValueFactory<Worker<?>, String>("title"));
final TableColumn<Worker<?>, Double> progressCol = new TableColumn<>("Progress");
progressCol.setCellValueFactory(new PropertyValueFactory<Worker<?>, Double>("progress"));
progressCol.setCellFactory(new Callback<TableColumn<Worker<?>, Double>, TableCell<Worker<?>, Double>>() {
#Override
public TableCell<Worker<?>, Double> call(TableColumn<Worker<?>, Double> col) {
return new ProgressTabelCell();
}
});
final TableColumn<Worker<?>, State> stateCol = new TableColumn<>("State");
stateCol.setCellValueFactory(new PropertyValueFactory<Worker<?>, State>("state"));
final TableColumn<Worker<?>, String> messageCol = new TableColumn<>("Message");
messageCol.setCellValueFactory(new PropertyValueFactory<Worker<?>, String>("message"));
messageCol.setPrefWidth(200);
taskTable.getColumns().addAll(Arrays.asList(titleCol, progressCol, stateCol, messageCol));
return taskTable;
}
private static class CountingTask extends Task<Void> {
private CountingTask(String title) {
updateTitle(title);
}
#Override
protected Void call() throws Exception {
final int n = new Random().nextInt(100)+100;
for (int i=0; i<n; i++) {
updateProgress(i, n);
updateMessage(String.format("Count is %d (of %d)", i, n));
Thread.sleep(100);
}
return null;
}
}
private static class ProgressTabelCell extends TableCell<Worker<?>, Double> {
final ProgressBar progressBar = new ProgressBar();
#Override
public void updateItem(Double value, boolean empty) {
if (empty || value == null) {
setGraphic(null);
} else {
setGraphic(progressBar);
progressBar.setProgress(value);
}
}
}
public static void main(String[] args) {
launch(args);
}
}
Just add and remove them from a list as needed. Then you can show them in a list. Here's some pieces of code.
ObservableList<String> runningTasks;
runningTasks.add(task.getTitle());
new Thread(task).start();
task.setOnSucceeded(new EventHandler<WorkerStateEvent>() {
#Override
public void handle(WorkerStateEvent event) {
runningTasks.remove(task.getTitle());
}});

Resources