Reputation: 1813
I am developing a project in javafx using NetBeans IDE. Also am new to javafx. I almost created all my classes. Now am trying to link them. What I meant is when a button is pressed from one class, a function of other class should be worked. I need that function to be displayed on the same window. I will provide the code of one of my class here. I didn't included the import statements and other unnecessary parts of the code here:
package login;
public class Login extends Application {
TextField t1,t2,t4,t5;
PasswordField t3;
ComboBox comboBox1,comboBox2,comboBox3;
private Connection connect = null;
private Statement statement = null;
private PreparedStatement preparedStatement = null;
@Override
public void start(Stage stage) {
BorderPane border = new BorderPane();
border.setTop(loginHBox1());
border.setLeft(loginVBox1());
border.setRight(loginVBox2());
Scene scene = new Scene(border,700,550);
stage.setScene(scene);
stage.setResizable(false);
scene.getStylesheets().add
(Login.class.getResource("Login.css").toExternalForm());
stage.show();
}
private HBox loginHBox1() {
HBox hbox = new HBox();
hbox.setPadding(new Insets(15, 12, 10, 180));
hbox.setSpacing(10);
Label lb1=new Label("LOG IN OR CREATE NEW ACCOUNT");
lb1.setAlignment(Pos.CENTER);
lb1.setFont(Font.font("Calibri",FontWeight.BOLD,26));
lb1.setTextFill(Color.BLACK);
hbox.getChildren().addAll(lb1);
return hbox;
}
private VBox loginVBox1() {
VBox vbox = new VBox();
vbox.setPadding(new Insets(20,30,15,50));
vbox.setSpacing(10);
Label lb3=new Label("LOG IN");
lb3.setAlignment(Pos.CENTER);
lb3.setFont(Font.font("Calibri",FontWeight.BOLD,24));
lb3.setTextFill(Color.BLACK);
Label lb1=new Label("Username");
lb1.setAlignment(Pos.CENTER);
lb1.setFont(Font.font("Calibri",FontWeight.BOLD,16));
lb1.setTextFill(Color.BLACK);
TextField t11=new TextField();
t11.setPrefSize(150,30);
Label lb2=new Label("Password");
lb2.setAlignment(Pos.CENTER);
lb2.setFont(Font.font("Calibri",FontWeight.BOLD,16));
lb2.setTextFill(Color.BLACK);
PasswordField pw11=new PasswordField();
pw11.setPrefSize(150,30);
Button b1=new Button("LOG IN");
b1.setFont(Font.font("Calibri",FontWeight.BOLD,16));
b1.setPrefSize(80,5);
btnl2.setOnAction(new EventHandler<ActionEvent>() {
@Override
public void handle(ActionEvent e) {
// THIS IS THE PART WHERE AM CALLING THE FUNCTION OF OTHER CLASS. WHICH FUNCTION
// SHOULD I CALL HERE ?
}
});
vbox.getChildren().addAll(lb3,lb1,t11,lb2,pw11,b1);
return vbox;
}
public static void main(String[] args)
{
launch(args);
}
}
Now I will provide the code of other class. Here also am not including the entire code.
package userpage;
public class UserPage extends Application {
TextField t1,t3,t4;
ComboBox comboBox1;
private Connection connect = null;
private Statement statement = null;
private PreparedStatement preparedStatement = null;
@Override
public void start(Stage stage) {
BorderPane border = new BorderPane();
HBox hbox = addHBox1();
border.setTop(hbox);
border.setRight(addVBox1());
border.setLeft(addVBox2());
border.setBottom(addHBox2());
Scene scene = new Scene(border,700,450);
stage.setScene(scene);
stage.setResizable(false);
scene.getStylesheets().add
(UserPage.class.getResource("UserPage.css").toExternalForm());
stage.show();
}
private HBox addHBox1() {
HBox hbox = new HBox();
hbox.setPadding(new Insets(15, 12, 15, 280));
hbox.setSpacing(10); // Gap between nodes
Label lb1=new Label("HOME PAGE");
lb1.setAlignment(Pos.CENTER);
lb1.setFont(Font.font("Trebuchet MS",FontWeight.BOLD,22));
lb1.setTextFill(Color.BLUEVIOLET);
hbox.getChildren().addAll(lb1);
return hbox;
}
private VBox addVBox1() {
VBox vbox = new VBox();
vbox.setPadding(new Insets(20,40,30,4));
vbox.setSpacing(10);
MenuBar menuBar = new MenuBar();
Menu menuFile1 = new Menu("ADD");
Menu menuFile2 = new Menu("EDIT");
Menu menuFile3 = new Menu("VIEW");
Menu menuFile4 = new Menu("HELP");
MenuItem add1 = new MenuItem("ENTER STUDENT DETAILS");
MenuItem add2 = new MenuItem("ENTER C-MARK");
MenuItem add3 = new MenuItem("ENTER ATTENDANCE");
MenuItem add4 = new MenuItem("EDIT STUDENT DETAILS");
MenuItem add6 = new MenuItem("EDIT C-MARK");
MenuItem add8 = new MenuItem("EDIT ATTENDANCE");
MenuItem add10 = new MenuItem("STUDENT DETAILS");
MenuItem add11 = new MenuItem("C-MARK");
MenuItem add12 = new MenuItem("ATTENDANCE");
MenuItem add13 = new MenuItem("VIEW HELP");
menuFile1.getItems().addAll(add1,add2,add3);
menuFile2.getItems().addAll(add4,add6,add8);
menuFile3.getItems().addAll(add10,add11,add12);
menuFile4.getItems().addAll(add13);
menuBar.getMenus().addAll(menuFile1,menuFile2,menuFile3,menuFile4);
vbox.getChildren().addAll(menuBar);
return vbox;
}
public static void main(String[] args) {
launch(args);
}
}
Upvotes: 0
Views: 198
Reputation: 2393
In a JavaFX application you should have only one class that extends Application
. If UserPage
is supposed to be a UI component why dont you make it a subclass of one of the provided panes (e.g. AnchorPane, BorderPane etc. ). In your case
public class UserPage extends BorderPane{
TextField t1,t3,t4;
ComboBox comboBox1;
private Connection connect = null;
private Statement statement = null;
private PreparedStatement preparedStatement = null;
public UserPage(){
this.setTop(addHBox1());
this.setRight(addVBox1());
this.setLeft(addVBox2());
this.setBottom(addHBox2());
}
//.... Your methods
}
seems most reasonable for me. Remove the Scene
related code from UserPage
. It does not belong there. With the modified UserPage
class your EventHandler
in the Login
class can have this code if you want to open UserPage
in the same Window.
btnl2.setOnAction(new EventHandler<ActionEvent>() {
@Override
public void handle(ActionEvent e) {
((Stage)btnl2.getScene().getWindow()).setScene(new Scene(new UserPage()));
}
});
Or this if you want to open UserPage in another Window.
btnl2.setOnAction(new EventHandler<ActionEvent>() {
@Override
public void handle(ActionEvent e) {
Stage usrpagestage = new Stage();
usrpagestage.setScene(new Scene(new UserPage()));
((Stage)btnl2.getScene().getWindow()).close();
usrpagestage.show();
}
});
If your Login
class is supposed to be a UI component too it should not extend Application
but also one of the provided panes. For example
public class Login extends GridPane{ .... }
And your application startup could look like this
public class Main extends Application{
@Override
public void start(Stage stage) throws Exception {
stage.initStyle(StageStyle.UTILITY);
stage.setResizable(false);
Login loginview = new Login();
Scene scene = new Scene(loginview, 400, 200);
stage.setScene(scene);
stage.show();
}
public static void main(String[] args){
launch(args);
}
}
Would be much cleaner in my opinion.
Upvotes: 1