Javafx how to load button animation then execute method - java

I need help to load first the animation of the button then execute the onAction method because when I click the button, the animation of the button is delayed... How to fix it?
here is the sample code:
public void viewStage(ActionEvent event) {
try {
FXMLLoader fxmlLoader = new FXMLLoader(getClass().getResource("Stage.fxml"));
Parent root1 = (Parent) fxmlLoader.load();
Stage stage = new Stage();
stage.getIcons().add(new Image("/img/Stage.png"));
stage.setTitle("New Stage");
stage.setScene(new Scene(root1));
stage.show();
} catch (Exception e){
System.out.println(e);
}
}

Related

How to know is the main Scene in memory?

I create application in JavaFX with a lot of controllers and Scene.
When I start MainApp.class it creates Stage primaryStage with Scene from startPage.fxml.
There are 2 buttons located which give a choice to open firstWindow or secondWindow.
I have 2 differs way to open that Windows
First one, When I open firstWindow I go back from startPage into MainApp and init firstWindow. It looks in MainApp.class as
public void initFirstWindow() {
try {
FXMLLoader loader = new FXMLLoader();
loader.setLocation(getClass().getResource(fxmlFirstWindow));
rootLayout = (BorderPane) loader.load();
firstWindow controller = loader.getController();
Scene scene = new Scene(rootLayout);
primaryStage.setScene(scene);
primaryStage.show();
} catch (IOException e) {
e.printStackTrace();
}
}
in FirstWindow.class
#FXML
private BorderPane root;
public Controller controller;
...
The second way, I can start my secondWindow from the startPage when I press the button as
private MainApp main;
private BorderPane rootLayout;
private Stage childrenStage;
#FXML
public void btnSecondWindowOpen(ActionEvent actionEvent) {
FXMLLoader loader = new FXMLLoader();
loader.setLocation(getClass().getResource(fxmlSecondWindow));
rootLayout = (BorderPane) loader.load();
SecondWindow controller = loader.getController();
Scene scene = new Scene(rootLayout);
childrenStage = main.getStage();
childrenStage.setScene(scene);
childrenStage.show();
}
And the question is which way is better and is any memory-leaks while i start first or second Window?
As I wrote I have a lot of fxml which openes after first/second buttons, so I need to know.

Change scene within the same window

How can I change the scene within the same window, rather than it opening a new window entirely.
Below is where the selectable options are added to a choicebox, with a listener at the end to "observe" when a selection is made, which, when clicked, changes the scene.
private void formulaOption2(){
list2.removeAll(list2);
String a = "Current Ratio";
String b = "Working Capital Ratio";
String c = "Debt to Equity Ratio";
String d = "Gross Profit Margin";
list2.addAll(a,b,c,d);
ChoiceBox2.getItems().addAll(list2);
//A LISTENER TO OBSERVE WHEN USER SELECTS ITEM
ChoiceBox2.getSelectionModel().selectedItemProperty().addListener( (v, oldValue, newValue) -> {
try {
comboSelect2();
} catch (IOException ex) {
Logger.getLogger(Tab1FXMLController.class.getName()).log(Level.SEVERE, null, ex);
}
} );
}
Below is the code that loads the FXML file:
public void comboSelect2() throws IOException {
if("Current Ratio".equals(ChoiceBox2.getSelectionModel().getSelectedItem())){
FXMLLoader fxmlLoader = new FXMLLoader(getClass().getResource("Tab2FXML.fxml"));
Parent root1 = (Parent) fxmlLoader.load();
Stage stage = new Stage();
stage.setTitle("Current Ratio");
stage.setScene(new Scene(root1));
stage.show();
}
}
Just replace the root of the current scene with the new root you want:
public void comboSelect2() throws IOException {
if("Current Ratio".equals(ChoiceBox2.getSelectionModel().getSelectedItem())){
FXMLLoader fxmlLoader = new FXMLLoader(getClass().getResource("Tab2FXML.fxml"));
Parent root1 = (Parent) fxmlLoader.load();
ChoiceBox2.getScene().setRoot(root1);
}
}

stage without bar (javafx)

I want to make a window like eclipse loading by javafx without the bar ,
what I can do !!?
this loading screen
Just use this line. You won't see any title bar in your application.
stage.initStyle(StageStyle.UNDECORATED);
hihi,
you could try this:
#Override
public void start(Stage primaryStage) {
try {
BorderPane root = new BorderPane();
Scene scene = new Scene(root,400,400);
scene.getStylesheets().add(getClass().getResource("application.css").toExternalForm());
primaryStage.initStyle(StageStyle.UNDECORATED);
scene.setFill(Color.BLACK);
primaryStage.setScene(scene);
primaryStage.show();
} catch(Exception e) {
e.printStackTrace();
}
}

Same Stage, Different Fxml - Javafx

I am new to JavaFX. I have a window loaded with vertical split pane. Here left side of the split page I have couple of buttons. On Each button click I need to load separate fxml on the right side of the split pane. So here I paste screen shot to be clear.
Here as of now I am opening in separate stage, separate scene when search button is hit. Now I need to load Searcher in the right side of baselayout window. Here is some code which loads baseLayout.
#Override
public void start(Stage primaryStage) throws Exception {
this.primaryStage = primaryStage;
this.primaryStage.setTitle("Base Layout");
BaseController.setMlTool(this);
FXMLLoader loader = new FXMLLoader(MLTool.class.getResource("view/Base.fxml"));
baseLayout = (AnchorPane) loader.load();
Scene scene = new Scene(baseLayout);
primaryStage.setScene(scene);
primaryStage.show();
}
Here is some code which loads Searcher on button click.
#FXML
private void initialize(){
System.out.println("Testing");
}
#FXML
private void handleSearchButton(){
System.out.println("Handle Button Called");
Stage search = new Stage();
FXMLLoader loader = new FXMLLoader(MLTool.class.getResource("view/Searcher.fxml"));
search.setTitle("Searcher");
try {
AnchorPane searcherPage = (AnchorPane) loader.load();
Scene scene = new Scene(searcherPage);
search.setScene(scene);
search.show();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
Here how do I load Searcher in BaseLayout's Rightside of split pane.
Hope my question is clear. Thanks for your help in anticipation.
You can take pane on right side and then load fxml as child node of that pane. Stage is same for all time and you can load fxml in pane.
ex: MainWindow.fxml is perent window. You can load in stage.
Parent root = FXMLLoader.load(getClass().getResource("MainWindow.fxml"));
Scene scene = new Scene(root);
stage.setScene(scene);
stage.show();
Now in MainWindow.fxml you have split pane. Put anchorPan on right side where you want to load fxml on button click. Then add fxml as child in anchorpane.
FXMLLoader fxmlLoader = new FXMLLoader(getClass().getResource(fxml));
Pane cmdPane = (Pane) fxmlLoader.load();
try {
anchCmdController.getChildren().clear();
anchCmdController.getChildren().add(cmdPane);
fadeIn.playFromStart();
} catch (Exception e) {
e.printStackTrace();
}
try to listen to the button click, once its clicked use 'FXMLLoader.load' to load based on the root elenment, typecast it say panel and assign tot he content of the right side.
In Your handler method try with this:
first declare anchorpane. In fxml right pane put anchorpane with anchCmdController name.
#FXML
private AnchorPane anchCmdController;
then update your handleSearchButton event.
#FXML
private void handleSearchButton() throws IOException {
System.out.println("Handle Button Called");
FXMLLoader fxmlLoader = new FXMLLoader(getClass().getResource("view/Searcher.fxml"));
Pane cmdPane = (Pane) fxmlLoader.load();
try {
/* NEED TO DECLARE ANCHOR PANE" */
anchCmdController.getChildren().clear();
anchCmdController.getChildren().add(cmdPane);
} catch (Exception e) {
e.printStackTrace();
}
}

How to open another window in JavaFX 2?

I have made a simple JavaFX application, but I would like the main window to open a secondary window when the user clicks a button.
What would be the simplest way to accomplish this?
Button b = new Button();
b.setOnAction(new EventHandler<ActionEvent>() {
#Override public void handle(ActionEvent e) {
Stage stage = new Stage();
//Fill stage with content
stage.show();
}
});
try this
try {
FXMLLoader fxmlLoader = new FXMLLoader(getClass().getResource("FXML.fxml"));
Parent root1 = (Parent) fxmlLoader.load();
Stage stage = new Stage();
stage.setScene(new Scene(root1));
stage.show();
((Node) (event.getSource())).getScene().getWindow().hide();
} catch (Exception e) {
e.printStackTrace();
}

Categories

Resources