I am building currently an application using JavaFx with an extra feature that displays the current date and time in the top corner of the scene. Since I am new to JavaFX, I don't know how to implement this one.
I tried to use an old code in swing but I got an IllegalStateException Error.
Here's my code.
MainMenuController.java
#FXML private Label time;
private int minute;
private int hour;
private int second;
#FXML
public void initialize() {
Thread clock = new Thread() {
public void run() {
for (;;) {
DateFormat dateFormat = new SimpleDateFormat("hh:mm a");
Calendar cal = Calendar.getInstance();
second = cal.get(Calendar.SECOND);
minute = cal.get(Calendar.MINUTE);
hour = cal.get(Calendar.HOUR);
//System.out.println(hour + ":" + (minute) + ":" + second);
time.setText(hour + ":" + (minute) + ":" + second);
try {
sleep(1000);
} catch (InterruptedException ex) {
//...
}
}
}
};
clock.start();
}
MainMenu.fxml
<children>
<Label fx:id="time" textFill="WHITE">
<font>
<Font name="Segoe UI Black" size="27.0" />
</font>
</Label>
<Label fx:id="date" textFill="WHITE">
<font>
<Font name="Segoe UI Semibold" size="19.0" />
</font>
</Label>
</children>
Main.java
public class Main extends Application {
public static void main(String[] args) {
launch(args);
}
#Override
public void start(Stage primaryStage) throws Exception {
Parent root = FXMLLoader.load(getClass().getResource("view/MainMenu.fxml"));
primaryStage.setScene(new Scene(root,1366, 768));
primaryStage.show();
}
}
As you notice, I tested it printing the live time in the console. Yeah it worked, but the label is still static.
I think you need FX UI Thread Platform.runLater(...) for that, but you can do something like this using Timeline in you controller class,
#FXML
public void initialize() {
Timeline clock = new Timeline(new KeyFrame(Duration.ZERO, e ->
time.setText(LocalDateTime.now().format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss")))
),
new KeyFrame(Duration.seconds(1))
);
clock.setCycleCount(Animation.INDEFINITE);
clock.play();
}
Alternative solution using AnimationTimer - suggested by #James_D,
AnimationTimer timer = new AnimationTimer() {
#Override
public void handle(long now) {
time.setText(LocalDateTime.now().format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss")));
}
};
timer.start();
The second approach using AnimationTimer seems much cleaner and more accurate than with Timeline.
You can check the POC of both approaches here.
The #Shekhar Rai answer works well, but here is a shorter version who works pretty well too.
#FXML
Label dateTime;
#Override
public void initialize(URL location, ResourceBundle resources) {
initClock();
}
private void initClock() {
Timeline clock = new Timeline(new KeyFrame(Duration.ZERO, e -> {
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
dateTime.setText(LocalDateTime.now().format(formatter));
}), new KeyFrame(Duration.seconds(1)));
clock.setCycleCount(Animation.INDEFINITE);
clock.play();
}
The main advantage is that you dont have to define every variables (seconds, minutes, ...)
i have a simple method it may helps you
private JFXDatePicker txtDateVenteAV;
txtDateVenteAV.setValue(LocalDate.now());
Related
I'm working on an app that uses JavaFX as GUI to control a PApplet that runs in a different window.
I've managed to make the two things appear and work, but when I try to load files in the PApplet class, I get a warning that says "The sketch path is not set" and an error like this: "java.lang.RuntimeException: Files must be loaded inside setup() or after it has been called."
I'm guessing that I might not have initialized the PApplet properly.
Here is my javafx.Application class
public class Main extends Application {
#Override
public void start(Stage primaryStage) {
try {
Parent root = FXMLLoader.load(getClass().getResource("application.fxml"));
Scene scene = new Scene(root,400,400);
scene.getStylesheets().add(getClass().getResource("application.css").toExternalForm());
primaryStage.setTitle("Clusters");
primaryStage.setScene(scene);
primaryStage.show();
} catch(Exception e) {
e.printStackTrace();
}
}
public static void main(String[] args) {
PApplet.main("application.Controller");
launch(args);
}
}
Here is my PApplet class
public class Controller extends PApplet {
private ArrayList<File> files;
private ArrayList<PImage> images;
public Controller() {
}
public void settings() {
size(640, 360);
}
public void setup() {
background(0);
images = new ArrayList<PImage>();
files = new ArrayList<File>();
}
public void draw() {
}
public void importImages() {
// Open File Chooser
FileChooser dialog = new FileChooser();
dialog.setTitle("Import Images");
FileChooser.ExtensionFilter extFilter = new FileChooser.ExtensionFilter("Image files (*.jpg, *.png, *.tiff)", "*.jpg", "*.png", "*.tiff");
dialog.getExtensionFilters().add(extFilter);
List<File> imported_files = dialog.showOpenMultipleDialog(new Stage());
System.out.println(imported_files);
for (File f: imported_files) {
images.add(loadImage(f.getAbsolutePath()));
System.out.println(f.getName() + " loaded");
}
}
}
This is how I link the importImages() method to the FXXML file
<Menu mnemonicParsing="false" text="File">
<items>
<MenuItem fx:id="file_open" mnemonicParsing="false" text="Open" />
<MenuItem fx:id="file_save" mnemonicParsing="false" text="Save" />
<MenuItem fx:id="file_save_as" mnemonicParsing="false" text="Save As" />
<MenuItem fx:id="file_import" mnemonicParsing="false" onAction="#importImages" text="Import" />
</items>
</Menu>
My guess is that Processing functions like loadImage() need to be called from Processing's main thread, and JavaFX functions like dialog.showOpenMultipleDialog() need to be called from the JavaFX Application Thread.
Putting work on the JavaFX Application Thread is pretty straightforward. Read Concurrency in JavaFX and JavaFX working with threads and GUI (or just google something like "JavaFX thread" for a ton of results). But basically you can call the Platform.runLater() function to put work on the JavaFX Application Thread.
Going the other way and putting work on the Processing thread is a bit more complicated. You can read more about concurrency here.
To test this theory, I'd do something simple like this:
public class Controller extends PApplet {
boolean loadImages = false;
// other variables you need to communicate between threads
public void draw() {
if(loadImages){
// call loadImages()
loadImages = false;
}
}
// call this from JavaFX
public void importImages() {
// set other variables you need here
loadImages = true;
}
}
If this works, you can do something fancier like maintaining a queue of events.
So I'm trying to play indefinitly a song on a background thread, but when the music ends it does not loop as it was supose to.
Tried the offered solution but yet no joy! Here is the code for the main class, hope this helps in the resolution of the issue.
Even tried to loop the thread, but no joy...
Not sure why it's ending after playing the full file once, but not sure how to solve it!
Here is the code I have. Any help is welcome
public class Main extends Application {
Media sugar = new Media(this.getClass().getResource("sounds/t1coSugar.wav").toExternalForm());
/**
* #param args the command line arguments
*/
public static void main(String[] args) {
Application.launch(Main.class, args);
}
#Override
public void start(Stage primaryStage)
{
primaryStage.setTitle("pacman");
primaryStage.setWidth(MazeData.calcGridX(MazeData.GRID_SIZE_X + 2)); //stage size x
primaryStage.setHeight(MazeData.calcGridY(MazeData.GRID_SIZE_Y + 5)); //stage size y
//splash screen
//end of splash screen
final Group root = new Group();
final Scene scene = new Scene(root);
root.getChildren().add(new Maze());
primaryStage.setScene(scene);
primaryStage.show();
int playbackgroundmusic = playbackgroundmusic();
}
private int playbackgroundmusic()
{
Runnable task = new Runnable() {
#Override
public void run() {
playSugar(); //method of the music
}
};
// Run the task in a background thread
Thread backgroundThread = new Thread(task);
// Terminate the running thread if the application exits
backgroundThread.setDaemon(true);
// Start the thread
backgroundThread.start();
return 0;
}
public void playSugar()
{
MediaPlayer mediaplayer = new MediaPlayer(sugar);
mediaplayer.volumeProperty().setValue(0.4);
mediaplayer.setStartTime(Duration.seconds(0));
mediaplayer.setStopTime(Duration.seconds(67));
mediaplayer.setAutoPlay(true);
mediaplayer.setCycleCount(MediaPlayer.INDEFINITE);
mediaplayer.play();
}
I've used Scenebuilder to place few shapes in my GUI (simplified version of my project). I would like the shapes to change colours but wait 2 seconds between changing colours. I want these changes to happen in my controller class after a button is pressed.
Circle1.setFill(YELLOW)
Wait(2 seconds)
Circle2.setFill(BLUE)
I'm not sure how to do that. I have read online about threading, but I don't really understand how to implement that from my Main and into my Controller class. Also, I could not really find any examples online. My Main class looks like:
public class Main extends Application {
public static void main(String[] args) {
launch(args);
}
#Override
public void start(Stage primaryStage) throws Exception {
BorderPane root = (BorderPane)FXMLLoader.load(getClass().getResource("File.fxml"));
Scene scene = new Scene(root);
scene.getStylesheets().add(getClass().getResource("application.css").toExternalForm());
primaryStage.setScene(scene);
primaryStage.show();
}
}
Please help. Also, if you could provide an example would be helpful for me to understand as I could not really find one online that gives an example of this.
Answering this question is easiest through an example I believe. So I've created a small Traffic Light application, since it allows me to use Circle and a timed sequence similar to your problem, whilst being a familiar concept for all.
I'll be using java.util.Timer alongside java.util.TimerTask for handling the sequence of lights. You may choose to use some animation / time line in JavaFX, but I think that is overkill for this kind of task.
I include the three files used in this project:
FXMLTrafficLight.fxml - which defines my FXML layout
FXMLTrafficLightController.java - my FXML controller
TrafficLightApplication.java - for completeness my subclass of Application, this is just the boiler plate.
FXMLTrafficLight.fxml
Not a fancy layout, just a VBox with three circles redLight, amberLight and greenLight, plus two Button objects startLights and stopLights used to start and stop the timer.
<VBox fx:id="root" id="VBox" xmlns:fx="http://javafx.com/fxml/1" fx:controller="javafxtimer.FXMLTrafficLightController">
<children>
<Circle fx:id="redLight" radius="100"></Circle>
<Circle fx:id="amberLight" radius="100"></Circle>
<Circle fx:id="greenLight" radius="100"></Circle>
<Button fx:id="startLights" text="Start Lights" onAction="#startLights"></Button>
<Button fx:id="stopLights" text="Start Lights" onAction="#stopLights"></Button>
</children>
</VBox>
FXMLTrafficLightController.java
I've included the model/state in the controller for simplicity. Whether a light is red / amber / green is determined by a boolean flag. The initial state is set in the initialize() method, and is updated by calling updateState().
When startLights(ActionEvent) is invoked (the EventHandler for startLights) a new Timer is constructed with a TimerTask implementation that first invokes updateState() on the thread created by the Timer and then invokes updateLights() which changes the color of the lights based on the current state on the JavaFX Application Thread using Platform.runLater(Runnable).
Note: the TimerTask itself will not be run on the JavaFX Application Thread, hence the need to use Platform.runLater(Runnable) for updating the GUI.
When stopLights(ActionEvent) is invoked, it will cancel the Timer.
Note that both startLights(ActionEvent) and stopLights(ActionEvent) toggle which Button objects are enabled on the interface as well.
public class FXMLTrafficLightController implements Initializable {
#FXML
private Circle redLight;
#FXML
private Circle amberLight;
#FXML
private Circle greenLight;
#FXML
private Button startLights;
#FXML
private Button stopLights;
private Timer timer;
private static final int DELAY = 2000; // ms
private boolean red, amber, green;
#Override
public void initialize(URL url, ResourceBundle rb) {
red = true;
amber = false;
green = false;
stopLights.setDisable(true);
updateLights();
}
#FXML
private void startLights(ActionEvent e) {
toggleButtons();
timer = new Timer();
timer.schedule(new TimerTask() {
#Override
public void run() {
// Not run on the JavaFX Application Thread!
updateState();
// Using Platform.runLater(Runnable) to ensure updateLights()
// is run on the JavaFX Application Thread
Platform.runLater(new Runnable() {
#Override
public void run() {
updateLights();
}
});
}
}, 0, DELAY); // no initial delay, trigger again every 2000 ms (DELAY)
}
#FXML
private void stopLights(ActionEvent e) {
toggleButtons();
timer.cancel();
}
private void toggleButtons() {
startLights.setDisable(!startLights.isDisable());
stopLights.setDisable(!stopLights.isDisable());
}
private void updateState() {
if (red && !amber && !green) {
amber = true;
} else if (red && amber && !green) {
red = false;
amber = false;
green = true;
} else if (!red && !amber && green) {
green = false;
amber = true;
} else {
red = true;
amber = false;
green = false;
}
}
private void updateLights() {
redLight.setFill(red ? Color.RED : Color.GREY);
amberLight.setFill(amber ? Color.ORANGE : Color.GREY);
greenLight.setFill(green ? Color.GREEN : Color.GREY);
}
}
TrafficLightApplication.java
For completeness... Just the standard boiler plate with file names changed.
public class TrafficLightApplication extends Application {
#Override
public void start(Stage stage) throws Exception {
Parent root = FXMLLoader.load(getClass().getResource("FXMLTrafficLight.fxml"));
Scene scene = new Scene(root);
stage.setScene(scene);
stage.show();
}
public static void main(String[] args) {
launch(args);
}
}
I'm trying to get my JavaFX Preloader splash sccreen to show up before my application. I'm using Eclipse IDE and when I click "run", half the time the splash screen will display correctly and the other half of the time I will get a gray or black screen instead of where the image should be.
I'm not sure what the issue is to cause it to only display correctly sometimes.
SplashController:
public class SplashController extends Preloader {
private static final double WIDTH = 676;
private static final double HEIGHT = 227;
private Stage preloaderStage;
private Label progressText;
private Pane splashScreen;
public SplashController() {}
#Override
public void init() throws Exception {
ImageView splash =
new ImageView(new Image(Demo.class.getResource("pic.png").toString()));
progressText =
new Label("VERSION: " + getVersion() + " ~~~ Loading plugins, please wait...");
splashScreen = new VBox();
splashScreen.getChildren().addAll(splash, progressText);
progressText.setAlignment(Pos.CENTER);
}
#Override
public void start(Stage primaryStage) throws Exception {
this.preloaderStage = primaryStage;
Scene splashScene = new Scene(splashScreen);
this.preloaderStage.initStyle(StageStyle.UNDECORATED);
final Rectangle2D bounds = Screen.getPrimary().getBounds();
this.preloaderStage.setScene(splashScene);
this.preloaderStage.setX(bounds.getMinX() + bounds.getWidth() / 2 - WIDTH / 2);
this.preloaderStage.setY(bounds.getMinY() + bounds.getHeight() / 2 - HEIGHT / 2);
this.preloaderStage.show();
}
}
And then in my main class Demo I simply have:
public class Demo extends Application {
#Override
public void start(Stage stage) throws Exception {
FXMLLoader loader = new
FXMLLoader(Demo.class.getResource("FXMLDocument.fxml"));
GridPane root = loader.load();
--------other app code here---------
}
public static void main(String[] args) {
LauncherImpl.launchApplication(Demo.class, SplashController.class, args);
}
}
Likely, you are executing some long running process on the JavaFX application thread or a thread involved in the application startup, which prevents the smooth operation of the preloader.
I suggest you review an Oracle Preloader sample and compare to your application. Ensure that you are using concurrent features like Task correctly, similar to the linked example. Check the linked sample works in your environment.
Source code (just copied from the Oracle Preloader sample link)
Note how in the start method of the main LongAppInit application class, that a Task and thread is spawned to ensure that the long application initiation does not take place on the JavaFX application thread. Also see how the notifyPreloader() method of application is called at various times within the long application initialization to let the preloader know of the current state of the initialization process so that it can reflect the progress accurately in the UI in real time.
LongAppInitPreloader.java
public class LongAppInitPreloader extends Preloader {
ProgressBar bar;
Stage stage;
boolean noLoadingProgress = true;
private Scene createPreloaderScene() {
bar = new ProgressBar(0);
BorderPane p = new BorderPane();
p.setCenter(bar);
return new Scene(p, 300, 150);
}
public void start(Stage stage) throws Exception {
this.stage = stage;
stage.setScene(createPreloaderScene());
stage.show();
}
#Override
public void handleProgressNotification(ProgressNotification pn) {
//application loading progress is rescaled to be first 50%
//Even if there is nothing to load 0% and 100% events can be
// delivered
if (pn.getProgress() != 1.0 || !noLoadingProgress) {
bar.setProgress(pn.getProgress()/2);
if (pn.getProgress() > 0) {
noLoadingProgress = false;
}
}
}
#Override
public void handleStateChangeNotification(StateChangeNotification evt) {
//ignore, hide after application signals it is ready
}
#Override
public void handleApplicationNotification(PreloaderNotification pn) {
if (pn instanceof ProgressNotification) {
//expect application to send us progress notifications
//with progress ranging from 0 to 1.0
double v = ((ProgressNotification) pn).getProgress();
if (!noLoadingProgress) {
//if we were receiving loading progress notifications
//then progress is already at 50%.
//Rescale application progress to start from 50%
v = 0.5 + v/2;
}
bar.setProgress(v);
} else if (pn instanceof StateChangeNotification) {
//hide after get any state update from application
stage.hide();
}
}
}
LongAppInit.java
public class LongInitApp extends Application {
Stage stage;
BooleanProperty ready = new SimpleBooleanProperty(false);
private void longStart() {
//simulate long init in background
Task task = new Task<Void>() {
#Override
protected Void call() throws Exception {
int max = 10;
for (int i = 1; i <= max; i++) {
Thread.sleep(200);
// Send progress to preloader
notifyPreloader(new ProgressNotification(((double) i)/max));
}
// After init is ready, the app is ready to be shown
// Do this before hiding the preloader stage to prevent the
// app from exiting prematurely
ready.setValue(Boolean.TRUE);
notifyPreloader(new StateChangeNotification(
StateChangeNotification.Type.BEFORE_START));
return null;
}
};
new Thread(task).start();
}
#Override
public void start(final Stage stage) throws Exception {
// Initiate simulated long startup sequence
longStart();
stage.setScene(new Scene(new Label("Application started"),
400, 400));
// After the app is ready, show the stage
ready.addListener(new ChangeListener<Boolean>(){
public void changed(
ObservableValue<? extends Boolean> ov, Boolean t, Boolean t1) {
if (Boolean.TRUE.equals(t1)) {
Platform.runLater(new Runnable() {
public void run() {
stage.show();
}
});
}
}
});;
}
}
I'm new to JavaFX and still learning I have this problem and couldn't find an answer.
I have two FXML files named "one.fxml" and "two.fxml" and they have their own controller classes naming "OneController.java" and "TwoController.java"
I'm want to open a "two.fxml" with onButtonClick from "one.fxml".
The Button Code in "one.fxml" is:
<Button fx:id="clearButton" layoutX="690.0" layoutY="309.0" minHeight="18.0" mnemonicParsing="false" prefHeight="40.0" prefWidth="196.0" text="Clear" onAction="#buttonclearClick"/>
and I have this method in "OneController.java"
private void buttonclearClick(final ActionEvent event)
{
//code to be written?
}
also how do I transfer values of the certain fields from "one.fxml" to "two.fxml"
like:
if "one.fxml" also has this tag:
<Text fx:id="textLabel" fill="#001f4b" layoutX="14.0" layoutY="377.0" strokeType="OUTSIDE" strokeWidth="0.0" text="Powered by StackOverflow">
how do I transfer this text field value(text="Powered by StackOverflow") to "two.fxml" or TwoController.java
How to achieve this?
One Controller
private void buttonclearClick(final ActionEvent event)
{
try
{
FXMLLoader loader = new FXMLLoader(getClass().getResource("two.fxml"));
Stage stage = new Stage();
stage.initModality(Modality.APPLICATION_MODAL);
//stage.initOwner(MainStage.stage);
stage.setScene(new Scene((Pane)loader.load()));
TwoController tsc = loader.<TwoController>getController();
tsc.GettextVal(textLabel.getText()); // Function in TwoController
stage.show();
}
catch(Exception e)
{
e.printStackTrace();
}
}
Two Controller
Make a function GettextVal in two controller
#FXML
void initialize()
{
//Initialize code here
}
public void GettextVal(String txtval)
{
System.out.println("text value from one controller - "+txtval);
}