I was wondering how to add a tick to javafx. By tick i mean a repetitive thread that updates the scene. I have tried an normal thread but I keep getting error that is is not the application thread. I would like it to start in the initialize method here is a heavily simplified version of my MainContol class:
package application;
import java.net.URL;
import java.util.ResourceBundle;
import javafx.fxml.Initializable;
public class MainControl implements Initializable{
public void initialize(URL arg0, ResourceBundle arg1) {
// Tick goes here
}
}
I think what you are looking for is a Timeline. A timeline creates a thread that repeats a set amount of times or infinitely until you terminate it.
Here is how you would at it:
MainControl:
package application;
import java.net.URL;
import java.util.ResourceBundle;
import javafx.animation.KeyFrame;
import javafx.animation.Timeline;
import javafx.animation.TimelineBuilder;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.fxml.Initializable;
import javafx.util.Duration;
#SuppressWarnings("deprecation")
public class MainControl implements Initializable{
public void initialize(URL arg0, ResourceBundle arg1) {
Timeline tick = TimelineBuilder.create()//creates a new Timeline
.keyFrames(
new KeyFrame(
new Duration(10),//This is how often it updates in milliseconds
new EventHandler<ActionEvent>() {
public void handle(ActionEvent t) {
//You put what you want to update here
}
}
)
)
.cycleCount(Timeline.INDEFINITE)
.build();
tick.play();//Starts the timeline
}
}
I hope this helps
If you happen to use Java 8, here is the same thing using ReactFX:
public class MainControl implements Initializable{
public void initialize(URL arg0, ResourceBundle arg1) {
EventStreams.ticks(Duration.ofMillis(10)).subscribe(() -> {
//You put what you want to update here
});
}
}
Related
i have got a question:
i have got a simple Java FX Application where i have a button and when i hit the button a thread task starts which have to change a label every second:
MyController.java
package application;
import java.net.URL;
import java.util.ResourceBundle;
import javafx.event.ActionEvent;
import javafx.fxml.FXML;
import javafx.fxml.Initializable;
import javafx.scene.control.Button;
import javafx.scene.control.Label;
import javafx.scene.control.TextField;
import java.util.concurrent.ScheduledThreadPoolExecutor;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.TimeUnit.*;
import java.util.*;
import java.text.DateFormat;
public class MyController implements Initializable{
#FXML
private Button myButton;
#FXML
private TextField myCountry;
#FXML
public Label myTime;
#Override
public void initialize(URL location, ResourceBundle resources) {
// TODO Auto-generated method stub
}
public void startTimeThread(ActionEvent event) {
ScheduledThreadPoolExecutor eventPool = new ScheduledThreadPoolExecutor(3);
eventPool.scheduleAtFixedRate
(new CheckSystemTime(), 0, 2, TimeUnit.SECONDS);
myButton.setDisable(true);
}
public void setLabel(String text) {
myTime.setText(text);
}
}
Main.java is the standart one
CheckSystemTime.java
package application;
import java.util.*;
import java.text.DateFormat;
public class CheckSystemTime implements Runnable {
//String locale;
public CheckSystemTime(/**String location**/) {
//this.locale = location;
}
public void run() {
Date rightNow;
DateFormat timeFormatter;
String timeOutput;
rightNow = new Date();
Locale currentLocale = new Locale("de");
timeFormatter = DateFormat.getTimeInstance(DateFormat.DEFAULT, currentLocale);
timeOutput = timeFormatter.format(rightNow);
//e.g. MyController.setLabel(timeOutput); doesnt work
}
}
So my question is, how i am able to access the setLabel Method from the CheckSystemTime class?
i hope you are able to help me how i can solve this problem
Add the MyController or a Label or the textProperty of the Label to the constructor of CheckSystemTime. Then you can keep a local reference inside CheckSystemTime. Then you can access it inside your run method. But make sure to wrap every UI related calls inside Platform.runLater().
Context
I just started to learn JavaFX so I'm developing a program to discover it. The program I'm on needs a array, so I used ObservableList. But here is the problem, the items I add to the list are only accessible from the function itself.
Here is the situation :
package fr.cartes;
import fr.cartes.model.MapModel;
import fr.cartes.view.ListMapsController;
import javafx.application.Application;
import javafx.collections.FXCollections;
import javafx.collections.ObservableList;
import javafx.fxml.FXMLLoader;
import javafx.scene.Scene;
import javafx.stage.Stage;
import java.io.IOException;
import java.util.ArrayList;
public class MainApp extends Application {
private Stage primaryStage;
private ObservableList<MapModel> maps = FXCollections.observableArrayList();
public static void main(String[] args){
launch(args);
}
public void start(Stage primaryStage) throws Exception {
this.primaryStage = primaryStage;
this.primaryStage.setTitle("/!\\ Explorateur /!\\");
this.primaryStage.setAlwaysOnTop(true);
initializationOfProgram();
}
public void initializationOfProgram(){
initializeListMaps();
maps.add(new MapModel("Les Etats-Unis d'Amérique", "EU.jpg"));
maps.add(new MapModel("La France d'aujourdh'ui", "FR.jpg"));
for(MapModel mapModel : maps){
System.out.println(mapModel.getMapName());
}
}
public void initializeListMaps(){
for(MapModel map : maps){
System.out.println(map.getMapName());
}
try {
FXMLLoader loader = new FXMLLoader();
loader.setLocation(MainApp.class.getResource("view/ListMaps.fxml"));
Scene scene = new Scene(loader.load());
primaryStage.setScene(scene);
primaryStage.show();
ListMapsController controller = new ListMapsController();
controller.setMainApp(this);
controller.loadMapsOnListView(maps);
}catch(IOException exception){exception.printStackTrace();}
}
public ObservableList<MapModel> getMapsLists(){
return maps;
}
}
For example, if I put a println() test in the initializationOfProgram(), I will have "Les Etats-Unis d'Amérique" and "La France d'aujourd'hui" written on the console, but if I do this println() test in initializeListMaps(), it won't display anything.
Am I missing something obvious here ? Can you help me ?
I'm trying to call a method form a separate class file to my main program java file but when I try to call the method on an object, I get the error symbol cannot be found for the method. Both files are in the same directory.
//This is my main java file for the program
package pwmanager;
import javafx.application.Application;
import javafx.fxml.FXMLLoader;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.stage.Stage;
/**
*
* #author 176878
*/
public class PWManager extends Application {
#Override
public void start(Stage stage) throws Exception {
Parent root = FXMLLoader.load(getClass().getResource("Login.fxml"));
Scene scene = new Scene(root);
stage.setScene(scene);
stage.setTitle("Password Manager");
stage.setPrevStage(); //Error occurs here.
stage.show();
}
/**
* #param args the command line arguments
*/
public static void main(String[] args) {
launch(args);
}
}
//This is the other .java file where the method is declared.
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package pwmanager;
import java.io.IOException;
import java.net.URL;
import java.util.ResourceBundle;
import javafx.event.ActionEvent;
import javafx.event.EventType;
import javafx.fxml.FXML;
import javafx.fxml.FXMLLoader;
import javafx.fxml.Initializable;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.scene.control.*;
import javafx.scene.layout.AnchorPane;
import javafx.scene.layout.GridPane;
import javafx.stage.Stage;
import javax.xml.crypto.Data;
/**
*
* #author 176878
*/
public class FXMLDocumentController implements Initializable {
#FXML
private Button loginButton;
private Button addAcct;
private Button removeAcct;
#FXML
public Stage prevStage;
public Stage currentStage;
#FXML
public void loginButtonAction(ActionEvent event) throws IOException {
System.out.println("You clicked me, logging in!");
Stage stage = new Stage();
FXMLLoader loader = new FXMLLoader(getClass().getResource("MainScreen.fxml"));
Parent mainScreen = (Parent)loader.load();
Scene scene = new Scene(mainScreen);
stage.setScene(scene);
stage.setTitle("Password Manager");
//prevStage.close();
stage.show();
}
#FXML
public void addAcctAction(ActionEvent event) throws IOException {
System.out.println("You clicked me, adding account!");
}
#FXML
public void removeAcctAction(ActionEvent event) throws IOException {
System.out.println("You clicked me, removing account!");
}
#Override
public void initialize(URL url, ResourceBundle rb) {
// TODO
}
}
I need to store the current stage so I can call it back or close the stage out.
setPrevStage is defined in FXMLDocumentController not Stage. You need to inject the former into the main PWManager class so that it can be invoked
controller.setPrevStage(stage);
I have that java class. It's initialized in JavaClassContainer. The problem is that it returns that error: java.lang.NullPointerException. A no set the text to the textfield.
import gnu.io.CommPortIdentifier;
import gnu.io.SerialPort;
import gnu.io.SerialPortEvent;
import gnu.io.SerialPortEventListener;
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.util.Enumeration;
import javafx.fxml.FXML;
import javafx.scene.control.TextField;
public class RXTX implements SerialPortEventListener{
#FXML private static TextField carlos;
// MORE CODE
#FXML
private void GetData(String Data) {
if(Data.contains("Temperature")){
carlos.setText("Hola");
}
}
}
If I put a System.Out.Println for instance it works. So, The problem is in the setText to the TextField no in GetData class.
Ok, I will put also the main controller:
public class FXMLDocumentController implements Initializable {
#FXML
private Label label;
#FXML
private void handleButtonAction(ActionEvent event) {
System.out.println("You clicked me!");
label.setText("Hello World!");
}
#Override
public void initialize(URL url, ResourceBundle rb) {
RXTX main = new RXTX();
main.initialize();
Thread t=new Thread() {
public void run() {
//the following line will keep this app alive for 1000 seconds,
//waiting for events to occur and responding to them (printing incoming messages to console).
try {Thread.sleep(1000000);} catch (InterruptedException ie) {}
}
};
t.start();
System.out.println("Started");
}
}
Well - what makes you believe that your RXTX.carlos is injected a value? You are creating the instance yourself so you are responsible to set a value to the STATIC field there.
I think you should:
learn about how to use a debugger in your IDE
learn more about how FXML works
JavaFX2's TableView features
"Column reordering by the user at runtime".
I'd like to disable this feature for one specific table in my Application.
Looking at the API doc, there is no obvious API hook for this.
There is, however, the columns-property. According to the doc, it represents
The TableColumns that are part of this TableView. As the user reorders the TableView columns, this list will be updated to reflect the current visual ordering.
Hoping that I'd at least be able to reset a change after it occurred, I tried adding a listener to reset changes after the fact.
import javafx.application.Application;
import javafx.collections.ListChangeListener;
import javafx.scene.Scene;
import javafx.scene.control.TableColumn;
import javafx.scene.control.TableView;
import javafx.stage.Stage;
public class TableTest extends Application {
public static void main(String[] args) {
launch(args);
}
#Override
public void start(Stage stage) {
TableView tableView = new TableView();
tableView.getColumns().setAll(new TableColumn(), new TableColumn());
tableView.getColumns().addListener(new ListChangeListener() {
#Override
public void onChanged(Change change) {
if (change.wasPermutated()){
change.reset();
}
}
});
stage.setScene(new Scene(tableView));
stage.show();
}
}
However, the listener aborts with an IllegalStateException when I ask for wasPermutated.
Is there a way to prevent reordering, or at least revert it programatically?
See below a SSCCE that shows that the listener gets called - but the flag is set to added when moving columns. Note that you need to call next() before using the change or you will get an IllegalStateException. See the javadoc of ListChangeListener.Change for a simple canonical example.
import javafx.application.Application;
import javafx.collections.ListChangeListener;
import javafx.scene.Scene;
import javafx.scene.control.TableColumn;
import javafx.scene.control.TableView;
import javafx.stage.Stage;
public class TableTest extends Application {
public static void main(String[] args) {
launch(args);
}
#Override
public void start(Stage stage) {
final TableView tableView = new TableView();
final TableColumn[] columns = {new TableColumn("1"), new TableColumn("2")};
tableView.getColumns().setAll(columns);
tableView.getColumns().addListener(new ListChangeListener() {
public boolean suspended;
#Override
public void onChanged(Change change) {
change.next();
if (change.wasReplaced() && !suspended) {
this.suspended = true;
tableView.getColumns().setAll(columns);
this.suspended = false;
}
}
});
stage.setScene(new Scene(tableView));
stage.show();
}
}