This is the function which called on clicking save button in save wizard FXML.
#FXML
public void Onsave() {
// Handle Button event.
System.out.println(myButton9.getId());
myButton9.setOnAction((event) - > {
String text1 = textfield.getText();
System.out.println(text1);
System.out.println("Save Clicked....");
pane.setVisible(false);
{
Node node = (Node) event.getSource();
// pane.setVisible(false);
Stage stage = (Stage) node.getScene().getWindow();
stage.close();
System.out.println(flag4);
// Call Create Tree View Node function(args1)
//args1 = Test Suite Name
}
});
On clicking the save button an FXML is closed which leads to a main screen where i have to hide a pane. Now when i click on save there is a null pointer exception. The Pane ID in scene builder is "pane". Is there is way somehow that the main FXML and SAVE dialogue FXML can be linked and the on clicking the save button i can get the refrence of the pane and it does not show null value. I tried to use the flag but after the stage is closed the flag changes to its default value. Also if anyone can suggest do help me in creating the tree view in main FXML with the arguements of text1.
Related
I'm creating a signUp page and once the user clicks signUp button I want the scene to switch to my budgetView layout. (Which is an FXML file).
I have tried extending my sign up controller with the Application class, and overrode the start method, but it kept giving me an error. This is the route my teacher tried to get me to try. I have also created another primaryStage in my controller and that worked, but my previous scene didn't close it just created another AnchorPane on top of the existing SignUp Scene. I want to just switch from one FXML view to another, when the event handler is initiated and successful.
Event Handler
try {
SignUpDAO.insertUser(txtFieldEmail.getText(), txtFieldFirst.getText(), txtFieldLast.getText(),
passFieldPassword.getText());
resultArea.setText("User inserted! \n");
// SUPPOSED TO OPEN NEW SCENE THROWS
// Exception in thread "JavaFX Application Thread" java.lang.RuntimeException:
// java.lang.reflect.InvocationTargetException
// start(primaryStage);
Overridden Start Method
// TODO Auto-generated method stub
try {
this.primaryStage = primaryStage;
AnchorPane budgetLayout = FXMLLoader.load(getClass().getResource("MainLayout.fxml"));
// SignUp Layout
Scene scene1 = new Scene(budgetLayout);
primaryStage.setScene(scene1);
primaryStage.show();
Hi I have problem with huge amount of similar drag and drop events for images, which i move with label to other image views.
Example of code:
//drag and drope for imageview01
#FXML
private void handleDragDetected1(MouseEvent event) {
Dragboard db = imageview01.startDragAndDrop(TransferMode.ANY);
ClipboardContent cb = new ClipboardContent();
cb.putImage(imageview01.getImage());
cb.putString(imageview01_label.getText());
db.setContent(cb);
System.out.println("Picture 1 is draged");
}
...
...
//drag and drope for imageview100
#FXML
private void handleDragDetected100(MouseEvent event) {
...
...
}
Is there any solution to make code shorten ?
You could assign the Labels as userData of the corresponding ImageViews which allows you to use the event source to retrieve the Label and therefore allows you to use the same event handler for all ImageViews.
The following example uses a Button and the onMouseClicked event for simplicity, but the same approach works for your problem too:
FXML
<Label text="Hello World" fx:id="label1"/>
<Button text="Print Label 1" onMouseClicked="#click" userData="$label1"/>
Controller
#FXML
private void click(MouseEvent event) {
// retrieve the node the event occured on
Button btn = (Button) event.getSource();
// retrieve Label associated with event source
Label label = (Label) btn.getUserData();
// now we've got all info we need without using any field of the controller
System.out.println(label.getText());
}
(You do not even need a label1 field in the controller.)
I'm currently made an Form with JavaFX.
Always i press a Button, i call the "addAnswer()"-Method.
In that I create a RadioButton, a Label and a delete-Button, which i bundle in a HBox. All that HBoxes i pack in a vBox.
The Problem now is the delete-Button. I want to delte just THAT HBox in which the clicked Button is.
Here is my code:
public void addAnswer() {
this.rB = new RadioButton();
checkAnswer.getToggles().add(rB);
hBox = new HBox();
tF = new TextField();
delAnswer = new Button("Löschen");
delAnswer.setId(Integer.toString(counter));
hBox.getChildren().addAll(rB, tF, delAnswer);
hBox.setId(Integer.toString(counter));
delAnswer.setOnAction(e -> delAnswer(Integer.parseInt(hBox.getId())));
System.out.println(delAnswer.getId());
vBox.getChildren().addAll(hBox);
counter++;
}
public void delAnswer(int e){
vBox.getChildren().remove(delAnswer.getId());
}
i tried this one above but i realized, that all the delAnswers-Buttons have the same ID: the number of how often i pressed the add-Button.
Is there any solution where i can just select that one i pressed with that dynamic way? Cause i don't kow how often somebody will press or delete something.
Thanks
hbox is a field and this is why always the HBox last added is used. (hBox is evaluated, when lambda body is executed, not at the time of the lambda creation). This would be different, if you used a (effectively) final local variable:
final HBox hBoxLocal = hBox;
delAnswer.setOnAction(e -> delAnswer(Integer.parseInt(hBoxLocal.getId())));
However I'd like to present a different solution which would allow you to use the same EventHandler<ActionEvent> for all delete Buttons:
You can get the Node that triggered the event using getSource. From this Node you can get the parent, which is the HBox. You can remove this from the VBox using the remove(Object) method
delAnswer.setOnAction(e -> {
// get button
Node source = (Node) e.getSource();
// remove parent of button from VBox
vBox.getChildren().remove(source.getParent());
});
I think your problem is that you give the same event to all your button,Begin by creating a list that stores your buttons and then increments the value of the ID after affecting it to an item :
List<Button> buttons = new ArrayList<>();
/*
Create Button and call IDEvt method to create new event
for each button
*/
private void IDEvt(Button btn){
btn.setId(String.valueOf(IDRank));
btn.setOnMousePressed(new EventHandler<MouseEvent>() {
#Override
public void handle(MouseEvent event) {
System.out.println(btn.getId());
}
});
IDRank++;
}
I have a TextField, and I would like to do something if the user clicks anywhere that is not the TextField itself.
Apparently, the onMouseClicked event won't trigger if you don't click the node itself, so that wouldn't work.
Listening the focusedProperty may have been a good idea, but the problem is that almost the entirety of my application is not focus traversable, so in many cases clicking outside the textfield won't unfocus it, so the listener won't be notified.
The only thing left in my mind is to put an event filter on the scene itself, intercept mouse clicks, get the click coordinates and determine if they fall within the bounds of the textfield. I find this a little bit overkill and I may be missing something more obvious.
Is there another way to determine if the user clicked anywhere outside my TextField node?
The only thing left in my mind is to put an event filter on the scene itself, intercept mouse clicks, get the click coordinates and determine if they fall within the bounds of the textfield. I find this a little bit overkill and I may be missing something more obvious.
In fact I consider this your best option, but with a variation:
Use the PickResult provided by the MouseEvent to get the target Node and check, if it's in the hierarchy of the Node.
(This can be necessary, if the Node has children; e.g. a TextField also contains a Text element.)
In fact this seems to be the only option that cannot be broken by consuming the event somewhere in the scene graph.
Example
This moves the focus to the root pane in case the user clicks somewhere except the TextField.
public static boolean inHierarchy(Node node, Node potentialHierarchyElement) {
if (potentialHierarchyElement == null) {
return true;
}
while (node != null) {
if (node == potentialHierarchyElement) {
return true;
}
node = node.getParent();
}
return false;
}
#Override
public void start(Stage primaryStage) {
TextField textField = new TextField();
textField.setMinSize(400, Region.USE_PREF_SIZE);
textField.setMaxWidth(400);
textField.setEditable(false);
textField.textProperty().bind(Bindings.when(textField.focusedProperty()).then("Got the Focus!").otherwise("Please give me the focus!"));
StackPane root = new StackPane();
root.getChildren().add(textField);
Scene scene = new Scene(root, 500, 200);
scene.addEventFilter(MouseEvent.MOUSE_CLICKED, evt -> {
if (!inHierarchy(evt.getPickResult().getIntersectedNode(), textField)) {
root.requestFocus();
}
});
primaryStage.setScene(scene);
primaryStage.show();
}
Assume you top level container of your app is Pane. You can apply mouse click event to that pane and inside it handle the mouse click event for the textfield. So this way, if the user clicks on the textfield or clicks somewhere else both events will be notified. Here's is the sample code.
Pane.setOnMouseClicked((MouseEvent evt) -> {
System.out.println("Click outside textfield");
textField.addEventHandler(MouseEvent.MOUSE_CLICKED, new EventHandler<MouseEvent>() {
#Override
public void handle(MouseEvent event) {
System.out.println("textfield clicked");
}
});
});
If you have any other controls in that Pane. This mouse click wont interfere with them, it will be only called when you click directly on the pane.
In my javafx application I have a dialog box which shows the results from a previous action. When first displayed the window just shows as white without the contents. After resizing the contents display, and on the subsequent times the dialog is brought up it behaves normally. I'm sure it's something simple, but it's been bugging me for days while I move to work on other parts. My initialize method and Constructor are empty and something tells me this may be the issue.
In MainApp extends Application:
called from another dialog stage controller after calling close.
public void showEDResult(List<String> path) {
try {
// Load the fxml file and create a new stage for the popup
FXMLLoader loader = new FXMLLoader(MainApp.class.getResource("view/EDResultLayout.fxml"));
VBox page = (VBox) loader.load();
Stage dialogStage = new Stage();
dialogStage.setTitle("Edit Distance Result");
//dialogStage.initModality(Modality.WINDOW_MODAL);
dialogStage.initOwner(primaryStage);
Scene scene = new Scene(page);
dialogStage.setScene(scene);
// Set reference to stage in controller
//BUG -- when first displayed results don't show up until resize window
EDResultController controller = loader.getController();
controller.setDialogStage(dialogStage);
controller.setMainApp(this);
// give controller reference to result
controller.setResult(path);
// give controller reference to scene (cursor)
// Show the dialog and wait until the user closes it
dialogStage.showAndWait();
} catch (IOException e) {
// Exception gets thrown if the fxml file could not be loaded
e.printStackTrace();
}
}
In class ResultController:
public void setResult(List<String> result) {
numStepsLabel.setText(Integer.toString(result.size()-2));
//TODO -- work on layout
String str = buildResultString(result);
pathLabel.setText(str);
}