I am using SceneBuilder to develop a JavaFX application. I have ImageView object and setOnDragDropped is not triggering. I am not getting any errors. For onDragDetected everything is working. I tried these two options: 1) to set it from the SceneBuilder (see the screenshot)
Second way was from the controller code:
#FXML public void sensorDrop(DragEvent event) {
System.out.println("Drooooop!>>");
event.consume();
//TODO More useful code
}
Any thoughts?
The onDragDropped handler is called when you drop something onto the node with which it is registered, not when you drop that node on something. So you need to register the onDragDropped handler on the stack pane.
To make it complete- in order to register it the setOnDragOver has to be registered
label.setOnDragOver(new EventHandler <DragEvent>() {
public void handle(DragEvent event) {
event.acceptTransferModes(TransferMode.ANY);
event.consume();
}
});
I took this answer from:
JavaFX OnDragDropped Not Registering
Related
like the title implies i've got a problem with my application. The application is supposed to run in fullscreen mode (no intention for switching back to window mode), so i designed a footer-bar holding some images (with a Label, in a VBox) so the user could navigate or exit the program.
So after starting the application all Buttons work just fine with touch. Even the Exit-button in my footer-bar responded correctly by opening my custom Dialog. But here starts my Problem. The Dialog is shown by showAndWait()-Method call, but does not respond to Touch-Events. In contrary mouse-events are still processed (i still can use a mouse to click the Buttons in my Dialog and the Dialog is responding correctly).
I hope someone got an idea what i'm doing wrong.
MyDialog.java:
public static boolean showExitDialog(Window owner, ResourceBundle resources) {
LOGGER.info("Showing exit dialog...");
final Dialog<ButtonType> dialog = new Dialog<ButtonType>();
dialog.getDialogPane().getStylesheets().add(MyDialog.getInstace().getCssPath());
dialog.setContentText(resources.getString("label.exitdialog.text"));
dialog.setHeaderText(resources.getString("label.exitdialog.header"));
dialog.initOwner(owner);
dialog.initStyle(StageStyle.TRANSPARENT);
dialog.initModality(Modality.APPLICATION_MODAL);
dialog.getDialogPane().getButtonTypes().add(new ButtonType(resources.getString("btn.Exitdialog.exit"), ButtonData.OK_DONE););
dialog.getDialogPane().getButtonTypes().add(new ButtonType(resources.getString("btn.Exitdialog.cancel"), ButtonData.FINISH));
Optional<ButtonType> result = dialog.showAndWait();
LOGGER.debug("Result: {}", result.get());
if(result.isPresent() && result.get().getButtonData() == ButtonData.OK_DONE) {
LOGGER.info("Closing exit dialog returning true...");
return true;
} else {
LOGGER.info("Closing exit dialog returning false...");
return false;
}
}
In MainApp.java:
private EventHandler<WindowEvent> confirmCloseEventHandler = event -> {
// close event handling logic.
// consume the event if you wish to cancel the close operation.
if(MyDialog.showExitDialog(primaryStage, rb)) {
event.consume();
System.exit(0);
}
};
...
primaryStage.setOnCloseRequest(confirmCloseEventHandler);
In FooterBar.java:
#FXML
private void exitProgramPressedTouch(TouchEvent event) {
event.consume();
controller.getWindow().fireEvent(new WindowEvent(controller.getWindow(), WindowEvent.WINDOW_CLOSE_REQUEST));
}
*Edit* Oh totally forgot: No Exception or anything else is thrown.
I don't know the reason for the described behavior - maybe a bug. However, you could try to listen for ActionEvent instead of TouchEvent. It handles both touch and mouse events:
#FXML
private void exitProgramPressedTouch(ActionEvent event) {
event.consume();
controller.getWindow().fireEvent(new WindowEvent(controller.getWindow(), WindowEvent.WINDOW_CLOSE_REQUEST));
}
Maybe you need also to change the attribute which binds the event listener (from onTouch to onAction) in your FXML file.
Finally, I think, you could avoid System.exit(0); if you consume the close event only when the cancel button has been clicked:
if(!MyDialog.showExitDialog(primaryStage)) {
event.consume();
}
I have created a click handler on a gwt label, but it fails to fire. Whats wrong? Same method works for other widgets like icon etc.
#UiField Label fileName;
---
---
public void addClickHandler() {
fileName.sinkEvents(Event.ONCLICK);
handler = this.addHandler(new ClickHandler() {
#Override
public void onClick(ClickEvent event) {
event.preventDefault();
event.stopPropagation();
Window.alert("UI clicked");
}
}, ClickEvent.getType());
}
As I pointed out in comment. Reason why it is not working is because you are adding native event handler to this, and as i but you need to sink DOM events on element to be able to handle them. As you did not do this for this element but for Label it won't work.
As You want to handle click element on Label, you need to add your handler to fileName and that will work
I am trying to drag and drop labels on top of each other.
I have a list of labels called starsAndBars, and every label has this called on it as it is created:
private void giveDragAndDropProperties(Label label) {
//Enable drag actions to pick up the label
label.setOnDragDetected(new EventHandler<MouseEvent>() {
public void handle(MouseEvent event) {
System.out.println("Drag and drop started!");
Dragboard db = label.startDragAndDrop(TransferMode.ANY);
ClipboardContent content = new ClipboardContent();
int index = starsAndBars.indexOf(label);
content.putString("test");
db.setContent(content);
event.consume();
}
});
label.setOnDragEntered(new EventHandler<DragEvent>() {
public void handle(DragEvent event) {
System.out.println("drag entered!");
}
});
label.setOnDragExited(new EventHandler<DragEvent>() {
public void handle(DragEvent event) {
System.out.println("drag left!");
}
});
//Enable a label to be dropped on this label
label.setOnDragDropped(new EventHandler<DragEvent>() {
public void handle(DragEvent event) {
System.out.println("Drop occurred!");
}
});
}
When I attempt to perform a drag and drop operation, this is what I see:
Drag and drop started!
drag entered!
drag left!
drag entered!
<I release the mouse here>
drag left!
The "onDragDropped" event never registers, as far as I can tell. What am I doing wrong? I can tell that each Label has the drag and drop event handlers since they register the "entered" and "exited" events. This is the only piece of code that deals with Drag and Drop event handlers - so it should be being consumed by something else first.
All of these labels are in an HBox that is within a ScrollPane, if that makes any difference.
Edit: I have now tried putting a drop event on the HBox the labels are contained in with the same result - it never seems to get called. My code for that setOnDragDropped was essentially identical to the one in my other example.
I have tried rewriting both my setOnDragDetected and setOnDragDropped, and I am trying to match them as closely to this example (http://docs.oracle.com/javafx/2/drag_drop/jfxpub-drag_drop.htm) as possible, which has worked for me before.
Edit 2: I have another project that currently has drag and drop events working with events very nearly identical to this, though with different objects - I have tried copying source from that project (and changing the variable names) with no luck. I can include that source as well, if it might help. But it is essentially the same.
What more can I include to help reach an answer?
You need to accept the transfer mode in setOnDragOver, like this:
label.setOnDragOver(new EventHandler <DragEvent>() {
public void handle(DragEvent event) {
event.acceptTransferModes(TransferMode.ANY);
event.consume();
}
});
Having a
public void buttonClick(ClickEvent event) {
MyPopup popup = new MyPopup();
getWindow().addWindow(popup);
log.warn("Added POPUP");
//lot of method calling here then
getWindow().removeWindow(popup);
log.warn("Removed Popup");
}
I would expect to show a popup window and after some milisecundom (after the expensive method calls) it should hide itself. The log says :
2014-02-19 15:26:51 WARN xyzClass:82 - Added POPUP
2014-02-19 15:26:51 WARN xyzClass:135 - Removed Popup
But the truth is that there is no popup showing here.
If i only show it, and not remove it later (the popup will show)
public void buttonClick(ClickEvent event) {
MyPopup popup = new MyPopup();
getWindow().addWindow(popup);
log.warn("Added POPUP");
//lot of method calling here then
log.warn("Removed Popup");
}
My main reason for this i want to achieve a glasspanel/loading screen functionality # Vaadin, and not had found better solution yet. Any solution/description why the popup not shown up i would appreciate
Just do not have time to render it. You add it and immediately remove.
Try this approach, for example:
private MyPopup popup;
public void buttonClick(ClickEvent event) {
Thread workThread = new Thread() {
#Override
public void run() {
// some initialization here
getWindow().removeWindow(popup);
}
};
workThread.start();
popup = new MyPopup();
getWindow().addWindow(popup);
}
Depending on Vaadin version you can make use of ICEPush plugin (Vaadin 6) or built-in feature called Server Push (Vaadin 7).
public void buttonClick(ClickEvent event) {
MyPopup popup = new MyPopup();
getWindow().addWindow(popup);
log.warn("Added POPUP");
// start background thread with ICEPush or ServerPush
}
// Background thread in a separate class
// update UI accordingly when thread finished the job
getWindow().removeWindow(popup);
log.warn("Removed Popup");
Thanks to it you can move your time-consuming operations to another class thus decouple your business logic from the presentation layer. You can find examples of usage in the links above.
I have a form panel (myForm) with a submit button added onto a simple panel. Every time the submit is pressed myForm.addSubmitCompleteHandler is called twice
mySubmit.addClickHandler(new ClickHandler() {
#Override
public void onClick(ClickEvent event) {
myForm.submit();
}});
...
myForm.addSubmitCompleteHandler(new SubmitCompleteHandler() {
#Override
public void onSubmitComplete(SubmitCompleteEvent event) {
// what ever's here happens twice
}
});
I've double checked my code and ordering and placing of widgets and panels. What could possibly be causing this?
What I am trying to achieve is an alert that submission is complete.
It seems like adding the line
event.preventDefault()
in the addClickHandler of the submit button helps. Also make sure you don't have duplicate setAction Calls for the form