Hi guys first of allsorry if it is and stupid question. I am quite new in JavaFX development.
Scenario
I am building a menu for an app with the TreeView component of JavaFX8
Problem description
The problem is that when the selected item is selected again, the handler is not fired. ie: If i select CHILD 1.1, doSomething() is fired. But if it is selected again without selecting other TreeItem previously doSomething() is not fired. The code looks like this:
TreeItem<String> root = new TreeItem<>("ROOT");
root.setExpanded(true);
TreeItem<String> child1 = new TreeItem<>("CHILD 1");
child1.setExpanded(true);
TreeItem<String> child2 = new TreeItem<>("CHILD 2");
TreeItem<String> child11 = new TreeItem<>("CHILD 1.1");
tvMenu.getSelectionModel().selectedItemProperty().addListener((v, oldValue, newValue) -> {
String selectedItem =v.getValue().getValue();
if (selectedItem.equals("CHILD 1.1")){
doSomething();
}
How could I deal with this problem?
Thank you in advance.
EDIT 03/10/2017
Thanks to #James_D the problem was resolved here:
How do I make a mouse click event be acknowledged by a TreeItem in a TreeView?
Related
I'm developing an small application and I have a problem when creating the menu bar.
This is my start method:
public void start(#SuppressWarnings("exports") Stage prymaryStage) throws Exception {
// Stats menu
Menu statsMenu = new Menu("Stats");
// PairName menu
Menu pairNameMenu = new Menu("Choose pair");
// Stats Menu items
MenuItem gStats = new MenuItem("General stats");
// Pair list
ArrayList<String> pairNameList = DatabaseMethods.returnPairNameList();
// PairName items (probably i will have to change)
for (String item : pairNameList) {
pairNameMenu.getItems().add(new MenuItem(item));
}
statsMenu.getItems().addAll(gStats, pairNameMenu);
// Main menu bar
MenuBar menuBar = new MenuBar();
menuBar.getMenus().addAll(statsMenu);
// BorderPane settings
BorderPane borderPane = new BorderPane();
borderPane.setTop(menuBar);
Scene scene = new Scene(borderPane, 1200, 800);
prymaryStage.setTitle("English minimal pair training");
prymaryStage.setScene(scene);
prymaryStage.show();
}
The problem I have is in this part of the code:
ArrayList<String> pairNameList = DatabaseMethods.returnPairNameList();
// PairName items (probably i will have to change)
for (String item : pairNameList) {
pairNameMenu.getItems().add(new MenuItem(item));
}
I was trying to create the items of a submenu from an ArrayList. This data is fetch from a database and the data is returned in form of an ArrayList. I didn't find any other way to do the menu items than pairNameMenu.getItems().add(new MenuItem(item)); inside the for loop.
Now I want to handle the click in the items but I don't know how to do it. I've tried with .setOnAction but Eclipse says the .add(new MenuItem(item)) can't be use in that case and recomends .addAll and the same happens, Eclipse says that's an error and recomends .add
I tried to add this code after new MenuItem(item)
.addEventHandler(new EventHandler<ActionEvent>() {
public void handle(ActionEvent even) {
}
})
But it didn't work either.
I'm pretty new to Java and JavaFX, this is my first project so sorry if this is a very basic question.
Thank you for your time
You have to loop through pairNameMenu items after you have created and added all the items to pairNameMenu:
pairNameMenu.getItems().foreach((item) ->{
item.addEventHandler.....
....
....
});
or do something like below when creating the MenuItems:
for (String item : pairNameList) {
MenuItem tempMenuItem = new MenuItem(item);
tempMenuItem..addEventHandler.....
....
....
pairNameMenu.getItems().add(tempMenuItem);
}
I have a view, using TreeView as a tree view display. I want to manually set a child node as the selected state. How should I set it?
TreeView selections work basically the same as ListView selections, as long as you ignore indices. Just pass the TreeItem you want to select to the select method of the selection model:
#Override
public void start(Stage primaryStage) throws Exception {
TreeItem<String> c1 = new TreeItem<>("child 1");
TreeItem<String> c2 = new TreeItem<>("child 2");
TreeItem<String> root = new TreeItem<>("root");
root.getChildren().addAll(c1, c2);
TreeView<String> tv = new TreeView<>(root);
// select c2
tv.getSelectionModel().select(c2);
Scene scene = new Scene(tv);
primaryStage.setScene(scene);
primaryStage.show();
}
Firstly, sorry for my English. I hope, you unterstand it.
Last Month I started developing a little programm with FXML/ JavaFX.
I have two screens, and I'm switching between them. This is not the problem.
The problem is: On one Screen i have a listview, where i can choose an item. When i have chosen an item, i want to open a new tab on the other screen with the content of the item. Therefore, i have to click on a button.
When i have chosen an item, i can print out the selected item, but how do I open a new tab on the other screen. Both Screens a two different FXML and are activited from the controller. How can I add a Tab, although loading fxml?
public class Controller{
#FXML
private ListView<String> lv;
#FXML
private Button check;
#FXML
public void projectview (Event e3) throws Exception{
Stage stage = null;
Parent root = null;
if(e3.getSource()==check){
//Check is the declaration for the Button on the screen with the listview
String projectview= lv.getSelectionModel().getSelectedItem();
stage = (Stage) check.getScene().getWindow();
root = FXMLLoader.load(getClass().getResource("FXML1.fxml"));
//Here I want to add a tab in FXML1.fxml
Scene scene = new Scene(root);
stage.setScene(scene);
stage.show();
}
}
}
If something is missing or not clear, please ask. I read other similar questions, but i don't know, what to do.
Thanks for help
I want to create context menu for treeitems in a treeview. The thing is I want to display different context menu for each treeItem. How to implement this?
Foe example I want to create "Add Employee" for Acc Dept and "Add Supporter" for IT support.
Based on name of the treeitem the context menu needs to be displayed.
public TreeModel() {
MenuItem addMenuItem = new MenuItem("Create Tab");
addMenu.getItems().add(addMenuItem);
addMenuItem.setOnAction(new EventHandler() {
#Override
public void handle(Event t) {
TreeItem newEmployee =
new TreeItem<>("New Tab");
getTreeItem().getChildren().add(newEmployee);
}
});
contextMenuProperty().bind(
Bindings.when(Bindings.equal(itemProperty(),"TABS"))
.then(addMenu)
.otherwise((ContextMenu)null));
}
This works. #James thanks a lot for your excellent article :)
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 ?
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();
}
});
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 MenuItems 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.