I have an event handler for checking if the hbox was pressed. This event handler uses a parallel thread to check if the button was pressed longer than a set amount of time. If it is, it sets editMode true. This worked for me but suddenly it started to randomly enter the method when it couldn't. It shouldn't be possible for the loop to enter the if statement for checking if more than 800ms have passed, more than one time, because it sets edit mode to true which should break the loop on the next iteration.
But I have noticed that it does enter that if statement again. It does so randomly and a random number of times, sometimes two and sometimes even 5 or 6 times.
This is the code
hBox.addEventHandler(MouseEvent.MOUSE_PRESSED, new EventHandler<MouseEvent>() {
@Override
public void handle(javafx.scene.input.MouseEvent e) {
mousePressed = true;
pillReminder.setSelected(true);
double pressedTime = System.currentTimeMillis();
hBox.setBackground(new Background(new BackgroundFill(Color.CYAN, CornerRadii.EMPTY, Insets.EMPTY)));
new Thread() {
@Override
public void run() {
try {
Thread.sleep(800);
} catch (InterruptedException e) {
e.printStackTrace();
}
if (mousePressed) {
editMode = true;
//cannot thread javafx so need to use platform
Platform.runLater(new Runnable() {
@Override
public void run() {
handleEditMode();
}
});
}
}
}.start();
}
});
hBox.addEventHandler(MouseEvent.MOUSE_RELEASED, new EventHandler<MouseEvent>() {
@Override
public void handle(MouseEvent mouseEvent) {
if (!editMode) {
mousePressed = false;
hBox.setBackground(new Background(new BackgroundFill(Color.LIGHTCYAN, CornerRadii.EMPTY, Insets.EMPTY)));
if (hBox.getBoundsInLocal().contains(new Point2D(mouseEvent.getX(), mouseEvent.getY())))
handleReminderHubReleased(pillReminder);
}
}
});
This is the handleEditMode method and its sub methods:
private void handleEditMode() {
home.getAppManager().getAppBar().getActionItems().add(MaterialDesignIcon.DELETE.button(e ->
handleRemove()));
home.getAppManager().getAppBar().getActionItems().add(MaterialDesignIcon.CANCEL.button(e ->
handleCancelEdit()));
}
private void handleRemove() {
ListIterator<PillHub> iter = HubData.getInstance().getHubs().listIterator();
while (iter.hasNext()) {
if(iter.next().isSelected()) {
iter.previous();
iter.remove();
}
}
handleCancelEdit();
//redo the hub list in home
hubVBox.getChildren().clear();
initialize();
}
private void handleCancelEdit() {
editMode = false;
for(PillHub ph : HubData.getInstance().getHubs()) {
if(ph.isSelected()) {
ph.gethBox().setBackground(new Background(new BackgroundFill(Color.LIGHTCYAN, CornerRadii.EMPTY, Insets.EMPTY)));
ph.setSelected(false);
}
}
home.getAppManager().getAppBar().getActionItems().clear();
}
I found out the issue. It isn't possible to solve with the info you had (very very sorry for that). Every time I opened the view that had this event handler it attached it again. The result is that every hbox got addtional event handlers every time I entered this window. Sorry again for this. I would be glad if a mod closed this.