I want to add and remove new items on menu click. Now here is my menu
Menu m = new Menu("Click For Options");
// create menuitems
MenuItem m1 = new MenuItem("Coin Calculator");
m.getItems().add(m1);
Now I have a scene but in the same scene on clicking m1 I want to create textArea and add to scene and validate user input on click OK
//create event for menu items
m1.setOnAction(e-> {
TextArea text1=new TextArea("Enter total coin to be exchanged");
TextArea text2=new TextArea("Enter coin type to exchange to");
VBox newBox=new VBox();
newBox.getChildren().removeAll();
newBox.getChildren().addAll(text1,text2);
Group group = new Group(newBox);
newBox.getChildren().addAll(group);
});
Its giving long lists of exceptions
Exception in thread "JavaFX Application Thread" java.lang.IllegalArgumentException: Children: cycle detected: parent = VBox#313448d4, node = Group#2beac4d5
at javafx.graphics/javafx.scene.Parent$3.onProposedChange(Parent.java:549)
at javafx.base/com.sun.javafx.collections.VetoableListDecorator.addAll(VetoableListDecorator.java:234)
at javafx.base/com.sun.javafx.collections.VetoableListDecorator.addAll(VetoableListDecorator.java:103)
at assignment1/com.york.algorithm.CoinSorterGUI.lambda$0(CoinSorterGUI.java:101)
at javafx.base/com.sun.javafx.event.CompositeEventHandler.dispatchBubblingEvent(CompositeEventHandler.java:86)
at javafx.base/com.sun.javafx.event.EventHandlerManager.dispatchBubblingEvent(EventHandlerManager.java:238)
at javafx.base/com.sun.javafx.event.EventHandlerManager.dispatchBubblingEvent(EventHandlerManager.java:191)
at javafx.base/com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:58)
at javafx.base/com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:114)
at javafx.base/com.sun.javafx.event.EventUtil.fireEventImpl(EventUtil.java:74)
at javafx.base/com.sun.javafx.event.EventUtil.fireEvent(EventUtil.java:49)
at javafx.base/javafx.event.Event.fireEvent(Event.java:198)
at javafx.controls/javafx.scene.control.MenuItem.fire(MenuItem.java:465)
.....
You can do like this;
TextArea text1=new TextArea("Enter total coin to be exchanged");
TextArea text2=new TextArea("Enter coin type to exchange to");
VBox newBox=new VBox();
Group group = new Group();
m1.setOnAction(e-> {
group.getChildren().clear();
group.getChildren().addAll(text1,text2 );
newBox.getChildren().clear();
newBox.getChildren().add(group);
});
Related
Can somebody explain how to use Dialogue box from above library through XML. Without coding the dialog box using java, Can't I just design it from scene builder by dragging and dropping component.
I have already tried it but when I run the program dialog box is not visible.
Would be really helpful if someone can share a working example or even a link
without using this method.
How to create a dialog using JFXDialog of JFoenix in JavaFX
Double height = posCenterAnchor.getHeight();
Double width = posCenterAnchor.getWidth();
StackPane stackPane = new StackPane();
AnchorPane.setTopAnchor(stackPane, 20.0); // adding anchor pane margins
AnchorPane.setLeftAnchor(stackPane, 20.0);
AnchorPane.setRightAnchor(stackPane, 20.0);
AnchorPane.setBottomAnchor(stackPane, 20.0);
posCenterAnchor.getChildren().add(stackPane);
JFXDialogLayout jfxDialogLayout = new JFXDialogLayout();
Parent parent;
try {
parent = FXMLLoader.load(getClass().getResource("/Views/SelectCustomer.fxml"));
jfxDialogLayout.getChildren().add(parent);
JFXDialog jfxDialog = new JFXDialog(stackPane, jfxDialogLayout, JFXDialog.DialogTransition.CENTER, true);
jfxDialog.show();
} catch (Exception e) {
e.printStackTrace();
}
just figured it out..all i had to do is ,just crate the content of the dialog box in a separate layout file and through code by creating a new dialog object then adding that layout to a dialog box...simple...wonder where went wrong..
I am new in JavaFX, I fetched database value into table view form after that when I click on table row then I want to load another FXML page and load the database value into textfield.
To load another FXML file and show it in a new opened window (Main.class.getResource(...) has to be adjusted):
Stage stage = new Stage();
Parent root = FXMLLoader.load(Main.class.getResource("your.fxml")));
stage.setTitle("...");
Scene scene = new Scene(root);
stage.setScene(scene);
stage.show();
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)) );
});
I'm new at Stackoverflow and Javafx (and Java at all), so my question can be stupid.
I have a .fxml file with main window (it's include LineChart) and Controller.java, binded to it. And I need make new window with chart options (color and thickness of line). I'm making window with options by event, binded on menuItem in main window.
So, how I can change properties of chart in main window from options window?
I don't understand at all how to get access to LineChart in main window!
This is how I open options window:
void showSettings(ActionEvent event)
throws IOException {
Group root = new Group();
Stage stage = new Stage();
AnchorPane frame = FXMLLoader.load(getClass().getResource("options.fxml"));
root.getChildren().add(frame);
Scene scene = new Scene(root);
stage.setScene(scene);
stage.show();
}
Can anybody help me?
P.S. Sorry for my bad English level :c
Edit1: I need pass params from new window(options) to already existing (main)!
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));