I have a MenuItem "load" that opens four tabs in the GUI. However, when I click "load" again. Instead of closing the previous tabs opened. It simply appends after it. How can I close them automatically?
load.setOnAction(e -> {
cb.loadBinary();
pb.loadPersonBinary();
tb.loadTextbookBinary();
Tab tabS = new Tab();
tabS.setText("Student");
studentScene = new SceneStudent(this, pb);
tabS.setContent(studentScene.getPane());
root.setCenter(tabPane);
Tab tabF = new Tab();
tabF.setText("Faculty");
facultyScene = new SceneFaculty(this, pb);
tabF.setContent(facultyScene.getPane());
root.setCenter(tabPane);
Tab tab = new Tab();
tab.setText("Text Book");
textScene = new SceneTextBook(this, tb);
tab.setContent(textScene.getPane());
root.setCenter(tabPane);
Tab tabC = new Tab();
tabC.setText("Course");
courseScene = new SceneCourse(this, cb);
tabC.setContent(courseScene.getPane());
tabPane.setTabClosingPolicy(TabClosingPolicy.ALL_TABS);
MenuItem buttonClicked = (MenuItem) e.getTarget();
if(buttonClicked == load){
tabPane.getTabs().removeAll();
}
tabPane.getTabs().addAll(tabS, tabF, tab, tabC);
root.setCenter(tabPane);
});
Instead of checking if the MenuItem that was clicked was your "load" item, removing, then adding, just do: tabPane.getTabs().setAll(tabS, tabF, tab, tabC);
See documentation here.
Related
I have a contextMenu and I added it to a button. I want the contextMenu to be displayed when I left-click(PRIMARY) on the button. How can I do this? Because by default just right-clicking does this.
I tried this way but it did not work
Button sortBy = new Button();
ContextMenu sortByMenu = new ContextMenu();
sortByMenu.addEventFilter(MouseEvent.MOUSE_PRESSED, ev -> {
if (ev.getButton() == MouseButton.PRIMARY) {
//does not do anything
}
});
sortBy.setContextMenu(sortByMenu);
MenuButton
MenuButton is a button which, when clicked or pressed, will show a ContextMenu.
Example code from the javadoc:
MenuButton m = new MenuButton(
"Eats"
);
m.getItems().addAll(
new MenuItem("Burger"),
new MenuItem("Hot Dog")
);
ChoiceBox
You could also consider a ChoiceBox, depending on what you are trying to do.
The ChoiceBox is used for presenting the user with a relatively small set of predefined choices from which they may choose.
ChoiceBox<String> cb = new ChoiceBox<>();
cb.getItems().addAll(
"item1",
"item2",
"item3"
);
To select a sortBy sorting field from a list of choices, a ChoiceBox would probably be a good fit.
I have the following code:
TextField vSuchenTextfield = new TextField("");
vLblSuchfeld.setMnemonicParsing(true);
vSuchenTextfield.setPromptText("Suchbegriff eingeben");
vLblSuchfeld.setLabelFor(vSuchenTextfield);
when I click on the vLblSuchfeld label with the mouse,
the text field vSuchenTextfield is not activated. What am I doing wrong?
If you want to bring focus to the textfield when you click on the label, I suggest you implement the following (untested) code:
TextField vSuchenTextfield = new TextField("");
vLblSuchfeld.setMnemonicParsing(true);
vSuchenTextfield.setPromptText("Suchbegriff eingeben");
vLblSuchfeld.setOnMouseClicked(event -> {
vSuchenTextfield.requestFocus();
});
Is there a way where I could add a permanent tab in javafx dynamically? I mean, I have already created a code which can add a tab dynamically on a button click, but when I close the window and open it again, the tab I just added is gone.
Here's my code snippet..
//on button click
#FXML
private void addNewTab(ActionEvent event) {
add_tab_button.setOnAction(e -> {
main_tab.getTabs().add(createTab()); //main_tab is the TabPane
main_tab.getSelectionModel().selectLast();
});
}
//the new tab to be created
private Tab createTab() {
tabIndex++;
Tab newTab = new Tab ("Tab " + tabIndex);
//content of tab goes here..
return newTab;
}
Everything goes well, except that, again, when I close the window and open it again, the newly created tab gets removed.. Is there a way to add it permanently? As if I'm overriding the fxml file of the view?
Hello this is my question, In order to let a user add tabs with a new button would i need to create a whole new border panes and tab panes for this to work?
No you don't need if you have already created tabPane previously.
Button btn = new Button("add new tab");
btn.setOnAction( (e) -> {
tabPane.getTabs.add(new Tab("new tab"), new VBox(new Label(Content)) );
});
There does not seem to be API for programmatically "selecting" ContextMenu items? By selecting I mean the equivalent of tapping up and down keys (or hovering the mouse over an item). I really only need to select the first item, when a ContextMenu is shown. I attempted to fire a down keyevent upon showing the menu, but nothing happened.. perhaps I constructed the event wrongly.
To get this working, we could use some private API. ContextMenu skin (ContextMenuSkin) uses a ContextMenuContent object, as a container with all the items.
We just need to request the focus for the first of these items.
But for this we could just use some lookups to find the first menu-item CSS selector. This has to be done after the stage has been shown.
This example will show a context menu with focus on the first item:
#Override
public void start(Stage primaryStage) {
MenuItem cmItem1 = new MenuItem("Item 1");
cmItem1.setOnAction(e->System.out.println("Item 1"));
MenuItem cmItem2 = new MenuItem("Item 2");
cmItem2.setOnAction(e->System.out.println("Item 2"));
final ContextMenu cm = new ContextMenu(cmItem1,cmItem2);
Scene scene = new Scene(new StackPane(), 300, 250);
primaryStage.setScene(scene);
primaryStage.show();
scene.setOnMouseClicked(t -> {
if(t.getButton()==MouseButton.SECONDARY){
cm.show(scene.getWindow(),t.getScreenX(),t.getScreenY());
// Request focus on first item
cm.getSkin().getNode().lookup(".menu-item").requestFocus();
}
});
}
For me solution provided in accepted answer didn't work correctly as item was only highlighted but not really selected (<Enter> was not accepting a value).
Instead of that constructing a proper KeyEvent did the work except a bug that only after first letter popup was working correctly.
Finally I combined both and got what I'd wanted:
// 'this' is related to parent component of ContextMenu
popup.show(this, x, y);
// Request focus on first item (sort of hack)
popup.getSkin().getNode().lookup(".menu-item").requestFocus();
this.fireEvent(new KeyEvent(
KeyEvent.KEY_PRESSED, "", "",
KeyCode.DOWN, false, false, false, false));