I want to solve my homework and I don't know how to start; the goal is to make 2 GUI Forms in JavaFX. The 1st is home form that contains Button1, and when the user clicks Button1 : show the 2nd Form and close the 1st.
How to do that ? hope to give me examples.
Thanks for reading and helping.
You can do something like this, but please keep in mind that we learn via practicing and training, try to do your own one after having a look at the idea in this example:
import javafx.application.Application;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.Label;
import javafx.scene.layout.StackPane;
import javafx.stage.Stage;
public class TwoForms extends Application {
#Override
public void start(Stage primaryStage) throws Exception {
StackPane root = new StackPane(); // TLC (Top Layer Container) a root container for all other components, which in your case is the Button
Button button = new Button("Go To Second Form"); // the button
root.getChildren().add(button); // add the button to the root
Scene scene = new Scene(root, 500,500); // create the scene and set the root, width and height
primaryStage.setScene(scene); // set the scene
primaryStage.setTitle("First Form");
primaryStage.show();
// add action listener, I will use the lambda style (which is data and code at the same time, read more about it in Oracle documentation)
button.setOnAction(e->{
//primaryStage.close(); // you can close the first stage from the beginning
// create the structure again for the second GUI
// Note that you CAN use the previous root and scene and just create a new Stage
//(of course you need to remove the button first from the root like this, root.getChildren().remove(0); at index 0)
StackPane root2 = new StackPane();
Label label = new Label("Your are now in the second form");
root2.getChildren().add(label);
Scene secondScene = new Scene(root2, 500,500);
Stage secondStage = new Stage();
secondStage.setScene(secondScene); // set the scene
secondStage.setTitle("Second Form");
secondStage.show();
primaryStage.close(); // close the first stage (Window)
});
}
public static void main(String[] args) {
launch();
}
}
Result
After Clicking on the Button -> Second Window.
Related
I've just started using JavaFX and have been trying to add an event that will add text to a textarea and clear a text field when you press the 'send' button. However, I can't seem to check the source of the event in the handle method.
I've tried to search for a solution, but others don't seem to face the same issue - either that or I'm missing something obvious.
import javafx.application.Application;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.geometry.Insets;
import javafx.geometry.Pos;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.Menu;
import javafx.scene.control.MenuBar;
import javafx.scene.control.TextArea;
import javafx.scene.control.TextField;
import javafx.scene.layout.GridPane;
import javafx.scene.layout.HBox;
import javafx.scene.layout.Priority;
import javafx.scene.layout.VBox;
import javafx.stage.Stage;
public class ApplicationMain extends Application implements EventHandler<ActionEvent>{
Stage window;
// Main Method
public static void main(String[] args) {
launch(args);
}
// Scene Method
#SuppressWarnings("static-access")
#Override
public void start(Stage primaryStage) {
// Window Stuff
window = primaryStage;
window.setTitle("Chat Application");
// Setup Grid Layout
GridPane grid = new GridPane();
grid.setAlignment(Pos.TOP_LEFT);
grid.setHgap(10);
grid.setStyle("-fx-background-color: #272828;");
// MenuBar
MenuBar menu = new MenuBar();
menu.setPrefWidth(1000);
menu.setPrefHeight(20);
// Creation of File + Help
Menu file = new Menu("File");
Menu help = new Menu("Help");
// Add the Menus to the MenuBar
menu.getMenus().add(file);
menu.getMenus().add(help);
// Add MenuBar to Scene
menu.setVisible(true);
grid.add(menu, 0, 0);
// Text Area Stuff
TextArea area = new TextArea();
area.setPrefWidth(1000);
area.setPrefHeight(700);
area.setEditable(false);
area.setStyle("-fx-control-inner-background: #313233;");
// Add Text Area to Grid
grid.add(area, 0, 1);
// Text Field
TextField enter = new TextField();
enter.setPromptText("Type here...");
enter.setMaxWidth(920);
enter.setMaxHeight(30);
enter.setStyle("-fx-padding: 5px;");
// Button
Button send = new Button("Send!");
// Set the Handler for the Send Button Event
send.setOnAction(this);
// Use of HBox to Space out Text Field & Send Button
HBox row = new HBox();
row.setSpacing(10);
row.setHgrow(enter, Priority.ALWAYS);
row.getChildren().addAll(enter, send);
// Use of VBox to Space out Text Field
VBox box = new VBox();
box.setSpacing(10);
box.setPadding(new Insets(10));
box.getChildren().add(row);
// Add HBox in VBox to Grid
grid.add(box, 0, 2);
// Scene Stuff
Scene scene = new Scene(grid, 1000, 750);
window.setScene(scene);
// Display the Window
window.show();
}
// Event Handler
#Override
public void handle(ActionEvent event) {
if (event.getSource() == send) {
}
}
}
Whenever I try to check if the source was the button 'send', it doesn't show up - as if it's not accessible by the method. I'm unsure of how to fix this.
There is a few things wrong with this code but we can fix it no problemo.
First learn naming conventions and stick to them as #kleopatra says if you google java naming conventions you will be overloaded with many results read a few
Next you shouldn't call a Stage a window there is already another object that has that name so it may confuse others but if its only for you its ok I guess
I wouldn't #SuppressWarnings("static-access") as you have done if you have an error fix it don't ignore it
The send.setOnAction(this); is not the way to handle events remove your implements EventHandler<ActionEvent> you can use the event handler by setting it like this
send.setOnAction(event -> sendToTextArea(enter.getText(), area));
And this is what the method you are calling should look like
private void sendToTextArea(String string, TextArea textArea){
//textArea.setText(string);Use setText if you want to set the whole area to something
textArea.appendText(string+"\n");//Use appendText to append add new line because chat app
}
Eveything else looks good here is what your final product should look like
public class Main extends Application {
private Stage stage;
#Override
public void start(Stage primaryStage) {
// stage Stuff
stage = primaryStage;
stage.setTitle("Chat Application");
// Setup Grid Layout
GridPane grid = new GridPane();
grid.setAlignment(Pos.TOP_LEFT);
grid.setHgap(10);
grid.setStyle("-fx-background-color: #272828;");
// MenuBar
MenuBar menu = new MenuBar();
menu.setPrefWidth(1000);
menu.setPrefHeight(20);
// Creation of File + Help
Menu file = new Menu("File");
Menu help = new Menu("Help");
// Add the Menus to the MenuBar
menu.getMenus().add(file);
menu.getMenus().add(help);
// Add MenuBar to Scene
menu.setVisible(true);
grid.add(menu, 0, 0);
// Text Area Stuff
TextArea area = new TextArea();
area.setPrefWidth(1000);
area.setPrefHeight(700);
area.setEditable(false);
area.setStyle("-fx-control-inner-background: #313233;");
// Add Text Area to Grid
grid.add(area, 0, 1);
// Text Field
TextField enter = new TextField();
enter.setPromptText("Type here...");
enter.setMaxWidth(920);
enter.setMaxHeight(30);
enter.setStyle("-fx-padding: 5px;");
// Button
Button send = new Button("Send!");
// Set the Handler for the Send Button Event
send.setOnAction(event -> sendToTextArea(enter, area));
// Use of HBox to Space out Text Field & Send Button
HBox row = new HBox();
row.setSpacing(10);
row.setHgrow(enter, Priority.ALWAYS);
row.getChildren().addAll(enter, send);
// Use of VBox to Space out Text Field
VBox box = new VBox();
box.setSpacing(10);
box.setPadding(new Insets(10));
box.getChildren().add(row);
// Add HBox in VBox to Grid
grid.add(box, 0, 2);
// Scene Stuff
Scene scene = new Scene(grid, 1000, 750);
stage.setScene(scene);
// Display the stage
stage.show();
}
private void sendToTextArea(TextField textField, TextArea textArea){
//textArea.setText(string);Use setText if you want to set the whole area to something
//textArea.clear();and .clear to clear all text from the TextArea
textArea.appendText(textField.getText()+"\n");//Use appendText to append add new line because chat app
textField.clear();
}
public static void main(String[] args) { launch(args); }
}
Make your TextArea and TextField class variables:
private TextArea area;
private TextField enter;
Change their initialization:
// Text Area Stuff
area = new TextArea();
// Text Field
enter = new TextField();
And your event handler :
// Event Handler
#Override
public void handle(ActionEvent event) {
area.setText(enter.getText());
}
If any of these changes is not clear, do not hesitate to ask.
I tried a lot but just couldn't find any solution. At the moment the opened window(popup window) is always on top but the user can still access the main window. That's how it should be, but it shouldn't be possible to open the same popup window again.
Stage stage = new Stage();
stage.setTitle(panelTitle);
stage.setScene(new Scene(root));
stage.initModality(Modality.WINDOW_MODAL);
stage.setAlwaysOnTop(true);
stage.showAndWait();
Thank you in advance!
As LazerBanana said, I would disable the button that opens the window, and I would enable it when you close it.
Stage stage = new Stage();
button.setDisable(true);
stage.setTitle(panelTitle);
stage.setScene(new Scene(root));
stage.initModality(Modality.WINDOW_MODAL);
stage.setAlwaysOnTop(true);
stage.showAndWait();
// your logic here
button.setDisable(false);
An alternative solution to creating a new one each time is to create one and just setup and show.
public class Stack extends Application {
private final Stage popup = new Stage();
#Override
public void start(Stage stage) throws Exception {
BorderPane root = new BorderPane();
root.setPrefWidth(400);
root.setPrefHeight(200);
Button button = new Button("ClickMePopup");
root.setCenter(button);
button.setOnAction(
event -> {
if (!popup.isShowing()) {
// you dont set modality because after the stage is set to visible second time it will throw an exception.
// Again depends on what you need.
// popup.initModality(Modality.WINDOW_MODAL);
// this focuses the popup and main window is not clickable
// popup.initOwner(stage);
VBox dialogVbox = new VBox(20);
dialogVbox.getChildren().add(new Text("Some Dialog"));
Scene dialogScene = new Scene(dialogVbox, 300, 200);
popup.setScene(dialogScene);
// you can actually put all above into the method called initPopup() or whatever, do it once, and just show it here or just bind the property to the button.
popup.show();
}
});
Scene scene = new Scene(root);
stage.setTitle("Stack");
stage.setScene(scene);
stage.show();
}
public static void main(String[] args) {
launch(args);
}
}
Or disable the button when clicked, but if your popup is not driven by the button or can be opened from other places the first idea would be a bit better in my opinion. Depends on what you need.
Or just create your own class and Springify it.
Here is the scenario, I have a main window and I click on one button it opens a pop up window. In this pop window I have table view that have some data display in it, and it have a one button called select. After select the data from table view, so when I push the select button I want this pop window to close and the data I selected from that to appear in my main window.
So far only thing I can do is extract the data from pop up window, I want it to close aswell with just one click
private void venueDisplay(String title, String message) {
Stage window = new Stage();
//Block events to other windows
window.initModality(Modality.APPLICATION_MODAL);
window.setTitle(title);
window.setMinWidth(400);
HBox hBox = new HBox();
hBox.setPadding(new Insets(10,10,10,10));
hBox.setSpacing(10);
hBox.setMaxHeight(20);
hBox.setAlignment(Pos.BOTTOM_CENTER);
hBox.getChildren().add(selectVenueButton);
//Display all the available venues to choose for allocation
VBox layout = new VBox(10);
venueList = new ListView<>();
ObservableList<Venue> observableVenue = FXCollections.observableArrayList(model.getVenues());
venueList.setItems(observableVenue);
layout.getChildren().addAll(venueList, hBox);
//Display window and wait for it to be closed before returning
Scene scene1 = new Scene(layout,300,500);
window.setScene(scene1);
window.showAndWait();
}
public void selectButtonHandler(EventHandler<ActionEvent> handler) {
selectVenueButton.setOnAction(handler);
}
Please consider this Example, you may take the idea and apply it to your program (Explanation in Comments).
import javafx.application.Application;
import javafx.collections.FXCollections;
import javafx.collections.ObservableList;
import javafx.geometry.Pos;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.ListView;
import javafx.scene.control.SelectionMode;
import javafx.scene.control.TextArea;
import javafx.scene.layout.HBox;
import javafx.scene.layout.VBox;
import javafx.stage.Modality;
import javafx.stage.Stage;
public class GetInfoFromPopUpWindow extends Application{
static TextArea textArea = new TextArea(); // to be filled from the pop-up window
#Override
public void start(Stage primaryStage) throws Exception {
// create the main Window and some simple components
// Suppose it contains a TextArea only for simplicity sake
Button open = new Button("Open Popup Window");
//simple container as a root for testing
HBox root = new HBox();
root.getChildren().addAll(textArea, open);
Scene scene = new Scene(root,610,400);
primaryStage.setScene(scene);
primaryStage.setTitle("Main Window");
primaryStage.show();
//Add Action Listener to the open Button
open.setOnAction(e->{ // lambda expression, read more about it in the Documentation
popUpWindow(); // call the method to open a pop-up wondow(see later)
});
}
public static void popUpWindow(){
VBox root = new VBox();
Button fetchInfo = new Button("Finish");
//create a listView and populate it with some info for testing purpose
// suppose the info you get from some database
ListView<String> listView = new ListView<String>();
ObservableList<String> items = FXCollections.observableArrayList (
"First Item", "Second Item", "Third Item", "Fourth Item");
listView.setItems(items);
//to select more than one item
listView.getSelectionModel().setSelectionMode(SelectionMode.MULTIPLE);
//decoration and size are up to your preference
listView.setPrefWidth(100);
listView.setPrefHeight(100);
root.getChildren().addAll(listView, fetchInfo);
root.setAlignment(Pos.CENTER);
Scene scene = new Scene(root, 250,150);
Stage stage = new Stage();
stage.initModality(Modality.APPLICATION_MODAL);
stage.setScene(scene);
stage.setTitle("Popup Window");
stage.show();
// Add action listener to fetchInfo Button in this Window
fetchInfo.setOnAction(e->{
// take the info from listView and fill it in the TextArea in the main Window
// just for testing purposes
for (String selectedItem : listView.getSelectionModel().getSelectedItems()){
textArea.appendText(selectedItem + " \n");
}
// when it finishes -> close the window and back to the first one
stage.close();
});
}
public static void main(String[] args) {
launch();
}
}
Test
Main Window Before Clicking Any Button
The Pop-Up Window After Clicking On The Button And Selecting Some Items
After Clicking on Finish Button, It Closes The Pop-Up Window and Then Goes Back To Main Menu With The Information (Selected Items)
I think you can just do:
private Venue venueDisplay(String title, String message) {
// existing code..
window.showAndWait();
return venueList.getSelectionModel().getSelectedItem();
}
and then your selectVenueButton just needs to close the window:
selectVenueButton.setOnAction(e -> window.hide());
you want to perform two operation on click select button
Close popup window :
To achieve this set Event Handler on button as below
selectVenueButton.setOnAction(handler);
In handler you can write logic to close popup window as below :
private EventHandler<ActionEvent> handler = new EventHandler<ActionEvent>() {
public void handle(ActionEvent event) {
Object source = event.getSource();
if (source instanceof Button) {
Button btn = (Button) source;
Stage stage = (Stage) btn.getScene().getWindow();
stage.close();
}
}
};
After click on button you want selected data on main window :
To achieve this declare venue list on Class Level Scope (Member Variables), so you can access outside a class.
in Dialog class :
ListView<Venue> venueList;
Access data in main Window :
CustomDialog dialog = new CustomDialog(); //popup class
dialog.showDialog;
Venue selectedItem = dialog.venueList.getSelectionModel().getSelectedItems();
I have a main stage that must open another stage without losing focus of the first one. I know i can call mainWindow.requestFocus() after calling secondWindow.show() but i want to make it to work without the first window even losing focus.
I wanto to do this because the second stage is a notification window with StageStyle.TRANSPARENT, that stays always on top and closes itself after some seconds.
Is there a way to make the second window "unfocusable"?
Do you really need to create a new Stage for showing your notification window? You could also use javafx.stage.Popup which creates transparent windows by default (so you would not need to set StageStyle.TRANSPARENT). Another advantage of using Popup instead of Stage is that it doesn't "steal" the focus from your main stage, wich should be pretty much what you need.
Here is some more information about the popup class: https://docs.oracle.com/javase/8/javafx/api/javafx/stage/Popup.html
And here is a simple example of how to use a popup in your application: https://gist.github.com/jewelsea/1926196
Well, I managed to do this embedding my java fx stage inside a swing JFrame. On JFrame i can do what i want. here is how:
JFrame frame = new JFrame();
frame.setUndecorated(true);
frame.setBackground(new java.awt.Color(0, 0, 0, 0));
final JFXPanel fxPanel = new JFXPanel();
frame.add(fxPanel);
frame.setSize(422, 116);
frame.setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE);
fxPanel.setScene(scene);
frame.setResizable(false);
frame.setAlwaysOnTop(true);
frame.setFocusableWindowState(false); // <- Here is the secret
frame.setVisible(true);
If anyone knows a less dirty way of doing this, i would appreciate.
You need some parameters for this. There is a method called stage.setOnShown() that will be called immediatly after opening the new stage.
But be aware of the code down below, it will open the second stage without any possibility to close it, so you need to kill the app. This could be made better with a timer where the windows automatically will close.
import javafx.application.Application;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.Label;
import javafx.scene.layout.StackPane;
import javafx.stage.Modality;
import javafx.stage.Stage;
import javafx.stage.StageStyle;
public class TwoStage extends Application {
#Override
public void start(Stage primaryStage) {
Button btn = new Button();
btn.setText("Open second stage");
btn.setOnAction((e) -> {
Label l = new Label("I'm a second window");
Scene s = new Scene(l, 100, 100);
Stage s1 = new Stage(StageStyle.TRANSPARENT);
s1.centerOnScreen();
s1.setScene(s);
s1.initModality(Modality.NONE);
s1.setAlwaysOnTop(true);
s1.setOnShown((e1) -> {
primaryStage.requestFocus();
});
s1.show();
});
StackPane root = new StackPane();
root.getChildren().add(btn);
Scene scene = new Scene(root, 300, 250);
primaryStage.setTitle("Two Windows");
primaryStage.setScene(scene);
primaryStage.show();
}
/**
* #param args the command line arguments
*/
public static void main(String[] args) {
launch(args);
}
}
Trying to use event handlers to sync specific button pushes to advance to the next "screen" on an ATM by: hiding the stage, updating the stage with the scene that a button push creates, and then reshowing the stage.
I am curious if this process can only be taken so deep since my button for newCheckingsAccounts isn't doing anything like it should, but I can go backwards on that page and I used the same code more or less to try to keep going forward.
import java.awt.Insets;
import java.util.ArrayList;
import java.util.Scanner;
import javafx.application.Application;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.geometry.Pos;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.TextField;
import javafx.scene.layout.GridPane;
import javafx.scene.layout.StackPane;
import javafx.scene.layout.HBox;
import javafx.scene.layout.BorderPane;
import javafx.scene.paint.Color;
import javafx.scene.shape.Circle;
import javafx.stage.Stage;
public class TestAccount extends Application {
//creates arrays that will store the accounts
ArrayList<Account> Checking = new ArrayList<Account>();
ArrayList<Integer> Savings = new ArrayList<Integer>();
//declares variables
double interest = 0;
double interestRate = 0;
double balance = 0;
double credit = 0;
double initialBalance = 0;
double feeChargedPerTransaction = 0;
Button btMain = new Button("Go Back to Main Menu");
Button btNewAccount = new Button("Make New Account");
Button btExistingAccount = new Button("Access an Existing Account");
Button btNewCheckings = new Button("Make New Checkings");
Button btNewSavings = new Button("Make New Savings");
#Override // Override the start method in the Application class
public void start(Stage primaryStage) {
// Hold two buttons in an HBox
HBox hBox = new HBox();
hBox.setSpacing(10);
hBox.setAlignment(Pos.CENTER);
Button btGoToAccounts = new Button("Go To Accounts Page");
Button btEnd = new Button("End Program");
hBox.getChildren().add(btGoToAccounts);
hBox.getChildren().add(btEnd);
BorderPane borderPane = new BorderPane();
borderPane.setBottom(hBox);
BorderPane.setAlignment(hBox, Pos.CENTER);
// Create a scene and place it in the stage
Scene scene = new Scene(borderPane, 500, 300);
primaryStage.setTitle("Bank of America"); // Set the stage title
primaryStage.setScene(scene); // Place the scene in the stage
primaryStage.show(); // Display the stage
// creates and registers handler and specifies action for button to go to accounts page
btGoToAccounts.setOnAction(new EventHandler<ActionEvent>() {
#Override public void handle(ActionEvent event) {
primaryStage.hide();
// Hold three buttons in an HBox
HBox hBox1 = new HBox();
hBox1.setSpacing(10);
hBox1.setAlignment(Pos.CENTER);
hBox1.getChildren().add(btNewAccount);
hBox1.getChildren().add(btExistingAccount);
hBox1.getChildren().add(btMain);
BorderPane borderPane1 = new BorderPane();
borderPane1.setBottom(hBox1);
BorderPane.setAlignment(hBox1, Pos.CENTER);
Scene scene1 = new Scene(borderPane1, 500, 300);
primaryStage.setTitle("Accounts Page"); // Set the stage title
primaryStage.setScene(scene1); // Place the scene in the stage
primaryStage.show(); // Display the stage
}
});
// creates and registers handler and specifies action for button to go to create new accounts page
btNewAccount.setOnAction(new EventHandler<ActionEvent>() {
#Override public void handle(ActionEvent event) {
primaryStage.hide();
// Hold three buttons in an HBox
HBox hBox2 = new HBox();
hBox2.setSpacing(10);
hBox2.setAlignment(Pos.CENTER);
Button btNewCheckings = new Button("Make New Checkings");
Button btNewSavings = new Button("Make New Savings");
hBox2.getChildren().add(btNewCheckings);
hBox2.getChildren().add(btNewSavings);
hBox2.getChildren().add(btMain);
BorderPane borderPane2 = new BorderPane();
borderPane2.setBottom(hBox2);
BorderPane.setAlignment(hBox2, Pos.CENTER);
Scene scene2 = new Scene(borderPane2, 800, 300);
primaryStage.setTitle("New Accounts"); // Set the stage title
primaryStage.setScene(scene2); // Place the scene in the stage
primaryStage.show(); // Display the stage
}
});
// THIS IS THE BUTTON THAT DOESN'T REGISTER AS BEING CLICKED...havent done the newSavingsAccount button either.want it to take me to new scene where i enter in new account info hit submit and then take me back to the main menu ("scene")
btNewCheckings.setOnAction(new EventHandler<ActionEvent>() {
#Override
public void handle(ActionEvent event) {
HBox hbox3 = new HBox();
Scene scene3 = new Scene(hbox3, 800, 300);
primaryStage.setTitle("Test"); // Set the stage title
primaryStage.setScene(scene3); // Place the scene in the stage
//the Name text field
final TextField name = new TextField();
name.setPromptText("Enter the desired account name which will be used under the access an existing account screen later");
name.setPrefColumnCount(10);
name.getText();
hbox3.getChildren().add(name);
//Defining the initial balance/fee text fields
final TextField initialBalance = new TextField();
final TextField fee = new TextField();
initialBalance.setPromptText("Enter your desired initial balance as a double.");
fee.setPromptText("Enter the agreed upon fee per transaction as a double.");
initialBalance.setPrefColumnCount(15);
fee.setPrefColumnCount(15);
fee.getText();
hbox3.getChildren().add(fee);
initialBalance.getText();
hbox3.getChildren().add(initialBalance);
//Defining the Submit button
Button accountCreation = new Button("Create the Account");
hbox3.getChildren().add(accountCreation);
//Defining the Clear button
Button clear = new Button("Clear");
hbox3.getChildren().add(clear);
primaryStage.show(); // Display the stage
//Setting an action for the Submit button
accountCreation.setOnAction(new EventHandler<ActionEvent>() {
#Override
public void handle(ActionEvent e) {
if ((initialBalance.getText() != null && !initialBalance.getText().isEmpty())) {
CheckingAccount newMember = new CheckingAccount();
newMember.setInitialBalance(Double.parseDouble(initialBalance.toString()));
newMember.setFee((Double.parseDouble(fee.toString())));
Checking.add(newMember);
} else {
System.out.println("no member added");
}
}
});
//Setting an action for the Clear button
clear.setOnAction(new EventHandler<ActionEvent>() {
#Override
public void handle(ActionEvent e) {
name.clear();
initialBalance.clear();
}
});
}
});
// creates and registers handler and specifies action for end button to close the stage
btEnd.setOnAction(new EventHandler<ActionEvent>() {
#Override public void handle(ActionEvent event) {
primaryStage.close();
}
});
// creates and registers handler and specifies action for main menu button to go to first scene
btMain.setOnAction(new EventHandler<ActionEvent>() {
#Override public void handle(ActionEvent event) {
primaryStage.hide();
primaryStage.setTitle("Bank of America"); // Set the stage title
primaryStage.setScene(scene); // Place the scene in the stage
primaryStage.show(); // Display the stage
}
});
}
/**
* The main method is only needed for the IDE with limited
* JavaFX support. Not needed for running from the command line.
*/
public static void main(String[] args) {
launch(args);
}
}
I think removing the overriding of your both buttons in the btNewAccount.setOnAction() should solve your problem:
// creates and registers handler and specifies action for button to go
// to create new accounts page
btNewAccount.setOnAction(new EventHandler<ActionEvent>() {
#Override
public void handle(ActionEvent event) {
primaryStage.hide();
// Hold three buttons in an HBox
HBox hBox2 = new HBox();
hBox2.setSpacing(10);
hBox2.setAlignment(Pos.CENTER);
// ############### removed this ###################
//Button btNewCheckings = new Button("Make New Checkings");
//Button btNewSavings = new Button("Make New Savings");
hBox2.getChildren().add(btNewCheckings);
hBox2.getChildren().add(btNewSavings);
hBox2.getChildren().add(btMain);
BorderPane borderPane2 = new BorderPane();
borderPane2.setBottom(hBox2);
BorderPane.setAlignment(hBox2, Pos.CENTER);
Scene scene2 = new Scene(borderPane2, 400, 300);
primaryStage.setTitle("New Accounts"); // Set the stage title
primaryStage.setScene(scene2); // Place the scene in the stage
primaryStage.show(); // Display the stage
}
});
If you want to instantiate in an ActionHandler of a Button, you need to declare it's ActionHandler in the same Block.
The way you did it (ActionHandler in the start method), it will only work, if you use your class atributes (Buttons) which you have initialized at the top.
Hope I could help :)
Edit: Probably a good tip for this project is looking at FXML files with JavaFX. This might help you: http://code.makery.ch/library/javafx-8-tutorial/part1/