Calling a function on the start of a JavaFX project using SceneBuilder - java

I created a GUI for my project using SceneBuilder and I set up all the button ID's and created a controller for the FXML file. I want to have a live clock running in the text area on launch and throughout the program. This is my first time using FXML to create a project in JavaFX so I'm confused as to where I should place this code. Normally the code works in a simple program without FXML and it is this code:
package com.example;
import example;
public class Layout extends Application {
TextArea clock;
public void start(Stage stage) throws FileNotFoundException {
clock = new TextArea();
clock.setEditable(false);
BorderPane bp = new BorderPane();
bp.setTop(clock);
refreshClock();
Scene scene = new Scene(bp);
stage.setScene(scene);
stage.show();
}
}
private void refreshClock()
{
Thread refreshClock = new Thread()
{
public void run()
{
while (true)
{
Date dte = new Date();
String topMenuStr = " " + dte.toString();
clock.setText(topMenuStr);
try
{
sleep(3000L);
}
catch (InterruptedException e)
{
// TODO Auto-generated catch block
e.printStackTrace();
}
} // end while ( true )
} // end run thread
};
refreshClock.start();
}
When I attempt to do it my current progress in my Ui controller class, nothing pops up in the Text Area despite what code I do and I'm not sure what to do next. Should this code be in my main .java file? Here is what I tried:
package application;
import java.util.Date;
import javafx.fxml.FXML;
import javafx.fxml.FXMLLoader;
import javafx.scene.control.Button;
import javafx.scene.control.TextField;
import javafx.scene.control.TextArea;
public class UiController {
#FXML
private TextArea clockTextArea;
private void refreshClock()
{
Thread refreshClock = new Thread()
{
public void run()
{
while (true)
{
Date dte = new Date();
String topMenuStr = " " + dte.toString();
clockTextArea.setText(topMenuStr);
try
{
sleep(3000L);
}
catch (InterruptedException e)
{
// TODO Auto-generated catch block
e.printStackTrace();
}
} // end while ( true )
} // end run thread
};
refreshClock.start();
}
public void initialize() {
refreshClock();
}

in javafx you work with events in the initialize() method.
E.g. button.setOnAction(e -> System.Exit(0)); or clock.onMouseClicked(e -> System.out.println("Test"));

Related

Auto Close Java FX App likely Sleep mode of window

I am new in Java FX. I expect to close my JavaFX application if the user is inactive for a period of time. In other words App is closed automatically if there are no any mouse event or Key event in for duration It's likely Sleep Mode of Window
I did try the code from Auto close JavaFX application due to innactivity. However My Program doesn't work
I get an example from https://www.callicoder.com/javafx-fxml-form-gui-tutorial/ .
And I edited on RegistrationFormApplication Class
public class RegistrationFormApplication extends Application {
private Timeline timer;
Parent root ;
#Override
public void start(Stage primaryStage) throws Exception{
timer = new Timeline(new KeyFrame(Duration.seconds(3600), new EventHandler<ActionEvent>() {
#Override
public void handle(ActionEvent event) {
// TODO Auto-generated method stub
root = null;
try {
root = FXMLLoader.load(getClass().getResource("/example/registration_form.fxml"));
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
primaryStage.setTitle("Registration Form FXML Application");
primaryStage.setScene(new Scene(root, 800, 500));
primaryStage.show();
}
}));
timer.setCycleCount(Timeline.INDEFINITE);
timer.play();
root.addEventFilter(MouseEvent.ANY, new EventHandler<Event>() {
#Override
public void handle(Event event) {
timer.playFromStart();
}
});
Thanks for help
Get RxJavaFx and run the code. After 4 seconds of inactivity (lack of any events) it will close the app.
import java.util.concurrent.TimeUnit;
import io.reactivex.Observable;
import io.reactivex.schedulers.Schedulers;
import io.reactivex.subjects.PublishSubject;
import javafx.application.Application;
import javafx.application.Platform;
import javafx.scene.Scene;
import javafx.scene.control.TextField;
import javafx.scene.input.InputEvent;
import javafx.stage.Stage;
import javafx.stage.WindowEvent;
public class CloseAfterApp extends Application {
public static void main(String[] args) {
launch(args);
}
#Override
public void start(Stage stage) throws Exception {
Scene scene = new Scene(new TextField());
PublishSubject<InputEvent> sceneEventPublishable = PublishSubject.create();
PublishSubject<WindowEvent> windowEventPublishable = PublishSubject.create();
scene.addEventFilter(InputEvent.ANY, sceneEventPublishable::onNext);
stage.addEventFilter(WindowEvent.ANY, windowEventPublishable::onNext);
Observable.merge(sceneEventPublishable, windowEventPublishable)
.switchMap(event -> Observable.just(event).delay(4, TimeUnit.SECONDS, Schedulers.single()))
.subscribe(event -> Platform.exit());
stage.setScene(scene);
stage.show();
}
}

Start and stop JavaFX background thread with 2 buttons

I am trying to make a simple UI to launch a selenium test that has the ability to start a background thread which launches a browser when the Start Button is pressed and stops the thread and closes it when the Stop button is pressed.
Unfortunately when I click stop after starting it, it does not work. If I let it finish I cannot restart the thread. How would I go about updating this so that I can make it submit a new thread that can be stopped by the stop button.
package application;
import org.openqa.selenium.WebDriver;
import javafx.application.Application;
import javafx.concurrent.Task;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.geometry.Insets;
import javafx.stage.Stage;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.Label;
import javafx.scene.control.TextField;
import javafx.scene.layout.BorderPane;
import javafx.scene.layout.GridPane;
public class Main extends Application {
Stage window;
GridPane grid;
public void start(Stage primaryStage) {
/*
* Set up the stage
*/
window = primaryStage;
window.setTitle("URL LOADER - V1");
grid = new GridPane();
grid.setPadding(new Insets(10,10,10,10));
grid.setVgap(8);
grid.setHgap(10);
window.setResizable(false);
/*
* URL input
*/
Label URLLabel = new Label("URL");
GridPane.setConstraints(URLLabel,0,0);
TextField URLTextField = new TextField();
URLTextField.setPromptText("https://www.google.com");
GridPane.setConstraints(URLTextField,1,0);
/*
* Create Buttons
*/
Button buttonStart = new Button("Create");
GridPane.setConstraints(buttonStart,1,6);
Button buttonStop = new Button("Stop");
GridPane.setConstraints(buttonStop,1,8);
grid.getChildren().addAll(URLLabel,URLTextField, buttonStart, buttonStop);
/*
* Create the scene
*/
Scene scene = new Scene(grid, 300, 300);
window.setScene(scene);
window.show();
Task<Void> task = new Task<Void>(){
#Override
protected Void call() {
new VisitPage().Start(this,URLTextField.getText());;
return null;
}
};
buttonStart.setOnAction(new EventHandler<ActionEvent>() {
/*
* Start Button Clicked
*/
public void handle(ActionEvent event) {
new Thread(task).start();
}
});
buttonStop.setOnAction(new EventHandler<ActionEvent>() {
/*
* Start Button Pressed
*/
public void handle(ActionEvent event) {
System.out.println("Stop Pressed");
}
});
}
public class VisitPage {
private String URL;
Browser BrowserFactory;
ThreadLocal<WebDriver> drivers;
WebDriver Browser;
public void Start(Task<Void> task, String URL) {
while (true) {
if (task.isCancelled())
{
System.out.println("Canceling...");
System.out.println("Stop Pressed");
Browser.close();
Browser.quit();
BrowserFactory.CloseDriver(drivers);
task.cancel();
}
else
{
/*
* Create Browser Factor to make ThreadLocal Browsers
*/
BrowserFactory = new Browser(1, 1);
drivers = BrowserFactory.SpawnBrowser();
/*
* Grab a Browser
*/
Browser = BrowserFactory.SpawnDriver(drivers);
/*
* Visit and scrape
*/
Browser.get(URL);
/*
* Wait 5 Seconds before closing
*/
try {
Thread.sleep(5000);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
Browser.close();
Browser.quit();
BrowserFactory.CloseDriver(drivers);
}
}
}
}
public static void main(String[] args) {
launch(args);
}
}
According to documentation
As with FutureTask, a Task is a one-shot class and cannot be reused. See Service for a reusable Worker.
So you have to create new task for each run. So I added task as field in Main:
Stage window;
GridPane grid;
Task<Void> task;
Then create task when start button is clicked:
buttonStart.setOnAction(new EventHandler<ActionEvent>() {
/*
* Start Button Clicked
*/
#Override
public void handle(ActionEvent event) {
if(task != null) {
System.out.println("Task already running");
return;
}
task = new Task<Void>() {
#Override
protected Void call() {
new VisitPage().start(this, URLTextField.getText());
;
return null;
}
};
Thread thread = new Thread(task);
thread.setDaemon(true);
thread.start();
}
});
On stop button click you have to cancel task:
buttonStop.setOnAction(new EventHandler<ActionEvent>() {
#Override
public void handle(ActionEvent event) {
if(task == null) {
System.out.println("Task not running");
return;
}
System.out.println("Stop Pressed");
task.cancel();
task = null;
}
});
This will do nothing, because it is your responsibility to end task when it is cancelled, and you are not ending your infinite loop.
So your VisitPage should look like this (I skipped testing details, since I do not have them on classpath):
public class VisitPage {
public void start(Task<Void> task, String URL) {
while (!task.isCancelled()) {
System.out.println("Running test");
try {
Thread.sleep(5000);
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println("Test run ended");
}
System.out.println("Canceling...");
System.out.println("Stop Pressed");
return;
}
}
Some minor points:
Technically task.cancel() would end your thread sometimes if you would not catch InterruptedException that is thrown if your thread is sleeping.
I am not sure how your code compiled but I had to make some variables final so they can be used in handlers: (never mind, from Java SE 8 local variables can be effectively final)
final TextField URLTextField = new TextField();
//...
final Task<Void> task = new Task<Void>(){
//...
I would define created thread as daemon so it will not keep running when you close your UI without stopping tests:
Thread thread = new Thread(task);
thread.setDaemon(true);
thread.start();
I also renamed Start method to start

How can I make the content of a Label change after one second using JavaFX?

I'm trying make a Splash Screen, so I need that some words stays appearing. I used o Thread, but I don't know how can I make a loop for label appear and after one second it changes.
package br.com.codeking.zarsystem.splash;
import javafx.concurrent.Task;
import javafx.concurrent.WorkerStateEvent;
import javafx.event.EventHandler;
import javafx.fxml.FXML;
import javafx.scene.control.Label;
public class ControllerSplash {
#FXML
Label lblLoading;
#FXML
private void initialize() {
System.out.println("app start");
lblLoading.setStyle("-fx-font-size: 16px;" + "-fx-font-family: Ubuntu;"
+ " -fx-text-fill: white;");
here I've tried to repeat this step 10 times, but it don't works
while (i <= 10) {
Task<Void> sleeper = new Task<Void>() {
#Override
protected Void call() throws Exception {
try {
Thread.sleep(1500);
} catch (Exception e) {
e.printStackTrace();
}
return null;
}
};
sleeper.setOnSucceeded(new EventHandler<WorkerStateEvent>() {
#Override
public void handle(WorkerStateEvent event) {
lblLoading.setText("to change " + i);
}
});
new Thread(sleeper).run();
i++;
}
}
}
I can't execute this in a for loop? So I don't have idea what I
have to do... I'm looking for, but nothing helps me. Can you? Thank
very much!
You can use a PauseTransition:
PauseTransition pauseTransition = new PauseTransition(Duration.seconds(1));
pauseTransition.setOnFinished(e -> lblLoading.setText("complete"));
pauseTransition.play();

Java - File save does not work

I would like to save an ArrayList's content to file (the user should choose the .txt's location) but I am not sure how to do that since that code does not work properly.
Do you have any idea how to do that?
package vizsgaquiz;
import java.io.File;
import java.util.ArrayList;
import javafx.application.Application;
import javafx.fxml.FXMLLoader;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.stage.FileChooser;
import javafx.stage.Stage;
public class VizsgaQuiz extends Application {
ArrayList<String> kerdeslista = new ArrayList<String>();
String a ="a";
#Override
public void start(Stage stage) throws Exception {
Parent root = FXMLLoader.load(getClass().getResource("Foablak.fxml"));
Scene scene = new Scene(root);
stage.setScene(scene);
stage.setTitle("Quiz Játék");
stage.show();
save();
}
public void save(){
kerdeslista.add(a);
FileChooser fileChooser = new FileChooser();
//Set extension filter
FileChooser.ExtensionFilter extFilter = new FileChooser.ExtensionFilter("TXT files (*.txt)", "*.txt");
fileChooser.getExtensionFilters().add(extFilter);
//Show save file dialog
File file = fileChooser.showSaveDialog(stage);
if(file != null){
SaveFile(kerdeslista, file);
}
}
/**
* #param args the command line arguments
*/
public static void main(String[] args) {
launch(args);
}
}
There are several issues that may be causing this problem. For starters, this code wouldn't compile. This is because you call the variable stage in the method save which "died" in the method start. To call stage in save, you either need to pass it to save or save it as a field. The second issue is that the method SaveFile doesn't seem to exist. An example of SaveFile might look something like the code included below. Please note that I updated the method save to take in a Stage and I changed the name of the method SaveFile to saveFile to match Java naming conventions. Also, the code below prints each value of the arraylist on a new line, which you may not want.
package vizsgaquiz;
import java.io.File;
import java.io.PrintWriter;
import java.util.ArrayList;
import javafx.application.Application;
import javafx.fxml.FXMLLoader;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.stage.FileChooser;
import javafx.stage.Stage;
public class VizsgaQuiz extends Application {
ArrayList<String> kerdeslista = new ArrayList<String>();
String a ="a";
#Override
public void start(Stage stage) throws Exception {
Parent root = FXMLLoader.load(getClass().getResource("Foablak.fxml"));
Scene scene = new Scene(root);
stage.setScene(scene);
stage.setTitle("Quiz Játék");
stage.show();
save(stage);
}
public void save(Stage stage){
kerdeslista.add(a);
FileChooser fileChooser = new FileChooser();
//Set extension filter
FileChooser.ExtensionFilter extFilter = new FileChooser.ExtensionFilter("TXT files (*.txt)", "*.txt");
fileChooser.getExtensionFilters().add(extFilter);
//Show save file dialog
File file = fileChooser.showSaveDialog(stage);
if(file != null){
saveFile(kerdeslista, file);
}
}
/**
* #param args the command line arguments
*/
public static void main(String[] args) {
launch(args);
}
public static void saveFile(ArrayList<String> list, File file) {
try {
PrintWriter out = new PrintWriter(file);
for (String val : list)
out.println(val + "\n");
out.close();
} catch (Exception e) {
e.printStackTrace();
}
}
}
Here is an example how to save a new file to specified directory and name of file from FileDialog , Strings taken from a vector of Strings.It works for me !
public static void SaveFileTo(Vector<String> myLines) {
FileOutputStream f = null;
DataOutputStream h = null;
FileDialog d = new FileDialog(new JFrame(), "Save", FileDialog.SAVE);
d.setVisible(true);
String dir;
dir = d.getDirectory();
File oneFile = new File(dir + d.getFile());
try {
oneFile.createNewFile();
} catch (IOException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
try {
f = new FileOutputStream(oneFile);
h = new DataOutputStream(f);
for (String de : myLines) {
h.writeBytes(de);
}
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}finally {
try {
h.close();
f.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}

Threads collision in JavaFX Custom Console

I've made a Java/JavaFX console, and now I face with an exception: Console reports an Internal error.The error is: java.lang.IllegalStateException: Not on FX application thread; currentThread = Thread-5. The code of the Console:
package core.console;
import javafx.concurrent.Service;
import javafx.fxml.FXMLLoader;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.scene.control.TextArea;
import javafx.stage.Stage;
import java.io.*;
import java.util.ResourceBundle;
public class Console implements Runnable{
private Console(ResourceBundle resourceBundle) throws IOException {
FXMLLoader loader = new FXMLLoader(this.getClass().getResource("Console.fxml"), resourceBundle);
Parent root = (Parent) loader.load();
controller = loader.getController();
Scene scene = new Scene(root);
stage = new Stage();
stage.setScene(scene);
textArea = controller.getTextArea();
show();
PipedOutputStream pout=new PipedOutputStream(this.pin);
System.setOut(new PrintStream(pout,true));
PipedOutputStream pout2=new PipedOutputStream(this.pin2);
System.setErr(new PrintStream(pout2,true));
System.setIn(new PipedInputStream(this.pout3));
reader = new Thread(this);
reader.setDaemon(true);
reader.start();
reader2 = new Thread(this);
reader2.setDaemon(true);
reader2.start();
}
public static Console getInstance(ResourceBundle resourceBundle) {
if (console == null) {
try {
console = new Console(resourceBundle);
} catch (IOException e) {
e.printStackTrace(); //To change body of catch statement use File | Settings | File Templates.
}
}
return console;
}
public void show() {
stage.show();
}
#Override
public synchronized void run()
{
try
{
while (Thread.currentThread()==reader)
{
try {
this.wait(100);
} catch(InterruptedException ie) {}
if (pin.available()!= 0)
{
String input=this.readLine(pin);
controller.appendText(input);
}
if (quit) return;
}
while (Thread.currentThread()==reader2)
{
try {
this.wait(100);
} catch(InterruptedException ie) {}
if (pin2.available()!= 0)
{
String input = this.readLine(pin2);
controller.appendText(input);
}
if (quit) return;
}
} catch (Exception e)
{
controller.appendText("\nConsole reports an Internal error.");
controller.appendText("The error is: "+e);
}
}
private synchronized String readLine(PipedInputStream in) throws IOException
{
String input="";
do
{
int available=in.available();
if (available==0) break;
byte b[]=new byte[available];
in.read(b);
input=input+new String(b,0,b.length);
}while( !input.endsWith("\n") && !input.endsWith("\r\n") && !quit);
return input;
}
private static Console console = null;
private ConsoleController controller;
private Stage stage;
private TextArea textArea;
private Thread reader;
private Thread reader2;
private final PipedInputStream pin=new PipedInputStream();
private final PipedInputStream pin2=new PipedInputStream();
private final PipedOutputStream pout3=new PipedOutputStream();
}
When starting the application, the console gives me described above exception, but everthing works. But if an exception is generated by the application, the console doesn't show it and everything became locked. What did I do wrong?
JavaFX is a single-threaded toolkit. You should never query or update the UI from the background threads. So you need to wrap all the calls to the JFX classes with
Platform.runLater(new Runnable() {
#Override
public void run() {
// Update/Query the FX classes here
}
});
In your context, the code in the Console.run() method is executed off the JavaFX application thread, so it should not be directly modifying UI objects by invoking controller.appendText(). In your case all of the controller.appendText() calls should be wrapped in the Platform.runLater construct defined above.

Categories