Reputation: 1813
I am doing a project in javafx using Netbeans IDE. I am new to javafx. I have a menu bar in my project. I need to open a new page on the same windows when clicked on each menu item(not new scene). The code is given below :
private VBox addVBox1() {
final 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");
add1.setOnAction(new EventHandler<ActionEvent>() {
public void handle(ActionEvent t) {
//...WHAT TO INCLUDE HERE ?
}
});
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;
}
In my project, I open new pages when clicking on buttons. Its code is:
btn2.setOnAction(new EventHandler<ActionEvent>() {
@Override
public void handle(ActionEvent e) {
((Stage)btn2.getScene().getWindow()).setScene(new Scene(new Login()));
}
});
Is it possible to implement this code in case of menu item ? How should I edit this code to perform an action when clicked on a menu item ?
Upvotes: 3
Views: 26934
Reputation: 1692
I think this question has not been answered correctly and the question has been misinterpreted.
TomJ asked:
Is it possible to implement this code in case of menu item?
When he says "menu item", Does he mean Menu
or MenuItem
? He's clearing showing the correct code for how to do it for a MenuItem
. I think he's asking about how to do that same thing for a Menu
. I tried doing this for a Menu
, and the code for invoking a handler on a Menu
is slightly different than doing it for a MenuItem
.
Here's a simple code example where the execute method is called when the Execute Menu is clicked:
Menu viewMenu = new Menu("View");
MenuItem viewJobs = new MenuItem("Jobs");
viewJobs.setOnAction(viewJobsAction);
MenuItem viewFileSelection = new MenuItem("File Selection");
viewFileSelection.setOnAction(e->{viewFileSelection());
viewMenu.getItems().addAll(viewJobs, viewFileSelection);
Menu execute = new Menu("Execute");
execute.onShownProperty().setValue(e->{execute());
// if the following line is not added, the onShownProperty event handler
// will never be called!
execute.getItems().addAll(new MenuItem());
Menu help = new Menu("Help");
MenuItem helpItem = new MenuItem("Help");
helpItem.setOnAction(e->{showHelp()}
MenuItem aboutItem = new MenuItem("About");
aboutItem.setOnAction(e->{showAbout()}
help.getItems().addAll(helpItem,aboutItem);
menubar.getMenus().addAll(viewMenu, execute, help);
It's necessary to have a bogus MenuItem
added to the Execute Menu for this to work. For Menu, it's better to use the onShownProperty
or onShowingProperty
rather than using the setOnAction
. Any of them will work, but the setOnAction
will require a second click before the handler will be called.
The JavaDoc for onShowingProperty
for Menu says:
Called just prior to the ContextMenu being shown, even if the menu has no items to show. Note however that this won't be called if the menu does not have a valid anchor node.
It sounds like the code should work without any MenuItem
s added, but it doesn't. It says however that the call won't be made if the menu doesn't have a valid anchor node. I'm not sure what an anchor node is, and I couldn't find any documentation about how to add an anchor node to a menu. Adding the bogus empty MenuItem
clearly made the Execute Menu have a valid anchor node. If someone knows more about anchor nodes, please reply and explain it, but the code example I gave works acceptably.
Upvotes: 1
Reputation: 10979
I don't know exactly what you mean by a new page. There is a Pagination control, but I don't think you mean that. Here's how to add a TextArea but you have to design the UI and choose your own controls.
add1.setOnAction(new EventHandler<ActionEvent>() {
public void handle(ActionEvent t) {
vbox.getChildren().add(new TextArea());
}
});
This is how to make a new window but you may want to ask a question about designing dialog boxes. Here's a SO answer https://stackoverflow.com/a/14168238/2855515
add1.setOnAction(new EventHandler<ActionEvent>() {
public void handle(ActionEvent t) {
Stage stage = new Stage();
Scene scene = new Scene(new VBox());
stage.setTitle("popup");
stage.setScene(scene);
stage.show();
}
});
Upvotes: 6