first post on here so please be gentle...
I am fairly new to JavaFX and have successfully set up quite a complicated GUI which reads a csv file in order to populate certain components within the GUI.
I'm using a timeline in the intialize function for the GUI Controller which fires a button every second on the GUI - the button calls a function which reads the csv file form disc.. all this is working fine.
When I quit/exit the GUI stage I want to stop the timeline from running... but can't seem to manage this...
I have a small function which loads the Stage and also has an event listener to detect when it's closed... what I'd like to do is be able to close the timeline at the commented line... in the try/catch section.
public void Show_MACD() throws IOException
{
Parent root = FXMLLoader.load(getClass().getResource("MACD Turbo.fxml"));
Scene scene = new Scene(root);
Stage stage = new Stage();
stage.setScene(scene);
stage.setTitle("FX AlgoTrader MACD Turbo");
stage.show();
JavaFX.thisstage=stage;
stage.setOnCloseRequest(new EventHandler<WindowEvent>() {
#Override
public void handle(WindowEvent we) {
LoginController sp=new LoginController();
try {
//how can I stop the timeline here?
sp.Show_Products(); // this loads up another stage - a menu in fact
} catch (IOException ex) {
Logger.getLogger(MACD_Controller.class.getName()).log(Level.SEVERE, null, ex);
}
}
});
//System.out.println("running");
}
Here's the section in the initialize function where the timeline is set up and run from....(this is in the same class as the controller called 'MACD_Controller' which is also home to the 'Show_MACD' function which has a event listener for window close events.. that's kind of where I would like to stop the timeline ie when the window closes)
#Override
public void initialize(URL url, ResourceBundle rb) {
final Timeline timeline = new Timeline(new KeyFrame(Duration.seconds(1), new EventHandler<ActionEvent>() {
#Override
public void handle(ActionEvent actionEvent)
{
Refresh.fire(); //Refresh is a button on the GUI which calls the csv file
}
}));
timeline.setCycleCount(Timeline.INDEFINITE);
timeline.play();
}
I know I need to somehow create a reference to 'timeline' so that I can use the 'timeline.stop' function... I've tried all sorts of mumbo jumbo but I keep getting an NPE.
I know this is super basic but I'm a bit stuck..
Cheers
Crispin
Related
what im trying to do is a javaFX window that handles my code while my window is shown so i worked on
public void setStage()(Stage stage){
this.stage = stage;
stage.setonShowing(new EventHandler<WindowEvent>() {
#Override
public void handle(WindowEvent event) {
work(); //What i want to do while my window is visible
}
});
stage.show();
}
setStage() gets his Stage from the class where i load my fxml from....
Everytime my window is popping up the sleep is before the fxml is loaded but after the window showed up so i only have a white window with nothing on it
To be honest im not sure what the real problem is
is it that i sleep before the fxml is loaded or is it my setonShowing() which is not working as it should be?
i also tryed to use setonShown() instead of setonShowing() but nothing changed
what i also tryed is to override the setonShowing in my Parent class but that also didnt work
so to make sure i didnt made anything wrong with my fxml here is the code i use to creat the window:
Parent root;
Stage stage = (Stage) StartSingleplayer_B.getScene().getWindow();
stage.close(); // closing my old window
try {
fxmlLoader = new FXMLLoader(getClass().getResource("Spielfeld.fxml"));
root = (Parent) fxmlLoader.load();
GameController controller = fxmlLoader.getController();
Stage stage2 = new Stage();
controller.setStage(stage2);
stage2.setOnShowing(new EventHandler<WindowEvent>() {
#Override
public void handle(WindowEvent event) {
work();
}
});
stage2.setScene(new Scene(root));
stage2.show();
} catch(Exception e) {
e.printStackTrace();
}
i really appreaciate your help so it would be nice if someone could give me a clue what i did wrong and / or how i could fix it
I have written a controller for two windows /stages.
The first window is opened in the MainClass. The second in the Controller, if the user clicks onto a button.
How can I get the TextFields from second.fxml in the applyFor()-method?
Thanks.
#FXML
protected void requestNewAccount(ActionEvent event) {
try {
FXMLLoader fxmlLoader = new FXMLLoader(getClass().getResource("second.fxml")); // TextFields in there
Parent root = (Parent) fxmlLoader.load();
Stage stage = new Stage();
stage.initModality(Modality.APPLICATION_MODAL);
stage.setTitle("Second Window");
Scene scene = new Scene(root);
String css = MainOnlineCustomer.class.getResource("/style.css").toExternalForm();
scene.getStylesheets().clear();
scene.getStylesheets().add(css);
stage.setScene(scene);
stage.show();
} catch (IOException e) {
logger.error(e);
}
}
/**
* closes the "second"-Window
* #param event
*/
#FXML
protected void cancel(ActionEvent event) {
final Node source = (Node) event.getSource();
final Stage stage = (Stage) source.getScene().getWindow();
stage.close();
}
#FXML
protected void applyFor(ActionEvent event) {
// get values from TextField in second.fxml here!!!
}
It's not good to share controllers between fxmls unless they serve the same purpose. Here both fxml seem to serve a different purpose (account management, login or something similar for one of them and creating a new account for the other). What is even worse is that those classes do not share the same controller instance, which means the small (and probably only) benefit you could get from using the same controller, is not used here. You should better use different controllers.
Since you use Modality.APPLICATION_MODAL as modality, I'd recommend using showAndWait instead of show to open the new stage. This will enter a nested event loop, which allows the UI to remain responsive and continues after the invocation of showAndWait once the stage is closed.
Furthermore add a method to the controller of second.fxml that allows you to retrieve the result.
Example
This creates a Person object with given name and family name.
"primary window (opening the "inner" stage)
FXMLLoader loader = new FXMLLoader(getClass().getResource("second.fxml"));
Stage subStage = new Stage();
subStage.initModality(Modality.APPLICATION_MODAL);
subStage.setTitle("Second Window");
Scene scene = new Scene(loader.load());
subStage.setScene(scene);
subStage.showAndWait();
Optional<Person> result = loader.<Supplier<Optional<Person>>>getController().get();
if (result.isPresent()) {
// do something with the result
}
controller for "inner" content
public class SecondController implements Supplier<Optional<Person>> {
#FXML
private TextField givenName;
#FXML
private TextField familyName;
private boolean submitted = false;
// handler for submit action
#FXML
private void submit() {
submitted = true;
givenName.getScene().getWindow().hide();
}
// handler for cancel action
#FXML
private void cancel() {
givenName.getScene().getWindow().hide();
}
#Override
public Optional<Person> get() {
return submitted ? Optional.of(new Person(givenName.getText(), familyName.getText())) : Optional.empty();
}
}
Note that you can gain access to any data available to the controller this way. I wouldn't recommend accessing any nodes (like TextFields) directly though, since this makes changing the UI harder.
Using the Supplier interface here is not necessary, but I chose to do this to achieve a loose coupling between SecondController and the main window.
I Want a way to run some code when i open a NewWindow from the main Controller in JavaFX .
Here is my main controller have :
public class FXMLDocumentController implements Initializable {
//consider all variables and or elements initialized
final Stage newWindow = new Stage();
#FXML
private void searchButtonAction(ActionEvent event) {
//Clicking on Search button triggers this event handler and Search Window will open with all the results
newWindow.show();
}
#Override
public void initialize(URL url, ResourceBundle rb) {
try {
newWindow.initModality(Modality.APPLICATION_MODAL);
//Loader to load the Search Window
FXMLLoader loader = new FXMLLoader(
getClass().getResource("SearchWindow.fxml")
);
Scene scene = new Scene(loader.load());
newWindow.setScene(scene);
SearchWindowController controller = loader.<SearchWindowController>getController();
controller.mainWindow = this;
} catch (Exception e) {
System.err.println(e.getMessage());
}`
}`}`
Here is my new window controller :
public class SearchWindowController implements Initializable {
#FXML
void searchButtonAction() {
//Code i want to run when this window opens
} catch (Exception e) {
System.err.print(e);
}
}
/**
* Initializes the controller class.
*
* #param url
* #param rb
*/
#Override
public void initialize(URL url, ResourceBundle rb) {
//empty
//call searchButtonAction() when this window opens ???
}
So basically the new window opens but i want to run some code i have when it loads . I have gone through some places and have noted that i can make a start method which should run when the new window starts. But i am unable to get it to run in my code . Also an other method i have seen is putting an event handler .
My question again being if I were to implement the above methods, where exactly should i put those method/Event Handler.I want my searchButtonAction() to be called by Default when the window loads. A different approach would also help. Some examples would be appreciated.
Edit:I want the output to be displayed on the newWindow , its basically some text that is taken from the main window and displays it in the new window . (Currently how i am working around it is creating a new button in the new window and then putting a onClickAction event on the button to run searchButtonAction() ). I want searchButtonAction() to run by Default . How do I initialize it so that searchButtonAction() is called when I open that new window.
Thank you in advance.
I had this issue. I had specified a wrong controller in the fxml file. It worked when I specified the correct controller to the fxml file.
I have a little issue close a secondary stage after clicking the close at the top right corner.
I'm using fxml with controller class, i need a way to handle this situation.
Here is what i do but i get a nullpointer exception :
#Override
public void initialize(URL location, ResourceBundle resources) {
Stage stage = (Stage) tbTabPaneHome.getScene().getWindow();
stage.setOnCloseRequest(e -> {
Platform.exit();
System.exit(0);
});
}
Because the stage not yet intialized completly, so any other ideas ?
Since the Scene nor the Stage are created yet, you can't call them or you get a NPE, as you already mentioned.
One way to install the event handler on the stage will be listening to changes in the sceneProperty() of tbTabPaneHome.
Once the node is added to the scene, that property will give you the Scene instance.
But the scene is not added to the Stage yet, so you need to wait till this is done, with Platform.runLater():
public void initialize() {
tbTabPaneHome.sceneProperty().addListener((obs, oldScene, newScene) -> {
Platform.runLater(() -> {
Stage stage = (Stage) newScene.getWindow();
stage.setOnCloseRequest(e -> {
Platform.exit();
System.exit(0);
});
});
});
}
Did you try to deal with your secondary stage entirely in the main stage controller?
I want to hide or show a help windows from a button or help menu in my main application controller. Something like the following:
public Button helpBtn;
Stage anotherStage = new Stage();
boolean secondaryInitialyzed = false;
boolean secondaryShowing = false;
public void showOrHideHelp(ActionEvent actionEvent) throws IOException {
if (!secondaryInitialyzed){
Parent anotherRoot = FXMLLoader.load(getClass().getResource("mySecondaryStage.fxml"));
anotherStage.setTitle("Secondary stage");
Scene anotherScene = new Scene(anotherRoot, 500, 350);
anotherStage.setScene(anotherScene);
secondaryInitialyzed = true;
}
if (secondaryShowing){
anotherStage.hide();
secondaryShowing = false;
helpBtn.setText("Show Help");
}
else {
anotherStage.show();
secondaryShowing = true;
helpBtn.setText("Hide Help");
}
It does work, and there might be a way for you to handle your setOnCloseRequest within the main controller.
I have the opposing issue, i.e preventing closing the secondary stage window by clicking the close at the top right corner. I'll look into setOnCloseRequest and see if there is a way there.
I also have an other unrelated problem: can I position the secondary in reference to the primary one?
Do you know how to wait for the user's input in a for loop? I don't mean the showAndWait() method, because I am not opening a new dialogue stage for the user. So for example, each round of the for loop should be waiting for the user to push a button before going ahead with the next round.
How is it possible? Many thanks!
UPDATE:
Now it came to my mind, that it would work with a while(buttonNotPressed){} but is it a good solution? I mean the while loop is running in this case as crazy until the user won't push the button. Or doest it work somehow similarly with wait methods?
Imagine it as a session:
User starts session with handleStart() You give the user 5 questions, one after one. In every iteration, the user can answer the upcoming question and he can save or submit the answer by handleSaveButton() You process the answer as you want, and go ahead with the next iteration. The point is, that the iteration must stop, until the save button hasn't been pressed.
Don't do it like that. The FX toolkit, like any event-driven GUI toolkit, already implements a loop for the purposes of rendering the scene graph and processing user input each iteration.
Just register a listener with the button, and do whatever you need to do when the button is pressed:
button.setOnAction(event -> {
// your code here...
});
If you want the action to change, just change the state of some variable each time the action is performed:
private int round = 0 ;
// ...
button.setOnAction(event -> {
if (round < 5) {
System.out.println("Round "+round);
System.out.println("User's input: "+textArea.getText());
round++ ;
}
});
I recently ran into a similar problem where I wanted something to be executed with an interval (if that's what you mean), until the user fired an event. I found 3 ways to do this:
UPDATE
You should use the stop/cancel method for the custom runnable and timer or else the thread will still be running when you exit the application. Timeline seems do it by itself.
Using a Timer:
Timer timer = new Timer();
TimerTask task = new TimerTask() {
#Override
public void run() {
System.out.println("Printed every second.");
}
};
timer.scheduleAtFixedRate(task, 0, 1000);
//timer.cancel();
With a TimeLine:
Timeline tl = new Timeline(new KeyFrame(Duration.millis(1000), e -> {
System.out.println("Timeline");
}));
tl.setCycleCount(Timeline.INDEFINITE);
tl.play();
//tl.stop();
Or making your own runnable class:
public class Runner implements Runnable {
private final Thread thread = new Thread(this);
private boolean run;
#Override
public void run() {
while(run) {
try {
System.out.println("Printed from loop");
Thread.sleep(1000);
} catch (InterruptedException e) {
run = false;
}
}
}
public void start() {
run = true;
thread.start();
}
public void stop() {
if(run) {
thread.interrupt();
System.out.print("Thread has stopped.");
}
}
}
And then when a person clicks fx. a button the event would stop using the example James_D posted:
Button btn = new Button("Button");
btn.setOnAction(e -> {
timer.cancel();
tl.stop();
runner.stop();
});
In my case, for inside for, had to create 2 index in class, use:
//start method
Timer timer = new Timer();
TimerTask task = new TimerTask()
{
public void run()
{
Platform.runLater(()->{
//... code to run after time, calling the same mehtod, with condition to stop
});
}
};
timer.schedule(task, time);
//end method
Had to use recursive method, incrementing the index with conditions, cause the tasks were been schedule all at the same time, without wait time.
I do not know if it is rigth, but was the solution that i found.
Hope it helps.
ALTERNATIVE SOLUTION W/O PAUSING:
I'm creating a game where I want the user to pick the game difficulty before the game starts. Instead of trying to pause the program midway through, I just put the next step of the code in a separate method which you call once a button is clicked:
private static difficulty;
public static void main(String[] args) {
try {
Application.launch(args);
} catch (UnsupportedOperationException e) {
}
}
public void start(Stage startStage) {
HBox buttons = new HBox();
Button easyButton = new Button("Easy");
Button mediumButton = new Button("Medium");
Button hardButton = new Button("Hard");
buttons.getChildren().addAll(easyButton, mediumButton, hardButton);
buttons.setAlignment(Pos.CENTER);
hbox.getChildren().addAll(buttons);
Scene startScene = new Scene(buttons, 200, 200);
startStage.setScene(startScene);
startStage.show(); // MENU
EventHandler<ActionEvent> playEasy = new EventHandler<ActionEvent>() {
#Override
public void handle(ActionEvent actionEvent) {
difficulty = 1; // SET DIFFICULTY
startStage.close(); // CLOSE MENU
play(); // RUN GAME ON EASY
}
};
EventHandler<ActionEvent> playMedium = new EventHandler<ActionEvent>() {
#Override
public void handle(ActionEvent actionEvent) {
difficulty = 2; // SET DIFFICULTY
startStage.close(); // CLOSE MENU
play(); // RUN GAME ON MEDIUM
}
};
EventHandler<ActionEvent> playHard = new EventHandler<ActionEvent>() {
#Override
public void handle(ActionEvent actionEvent) {
difficulty = 3; // SET DIFFICULTY
startStage.close(); // CLOSE MENU
play(); // RUN GAME ON HARD
}
};
easyButton.setOnAction(playEasy);
mediumButton.setOnAction(playMedium);
hardButton.setOnAction(playHard);
}
public void play() {
// WRITE GAME CODE HERE
}
To solve your specific problem, you could probably pass the startStage into the play method and then just update the scene there...but regardless I do hope this helps someone whos having trouble on how to use buttons! :)