Javafx Disable Scrolling by Mousewheel in ScrollPane - javafx

Does anayone know how to disable scrolling by Mousewheel in a ScrollPane?

The following works for me:
scrollPane.addEventFilter(ScrollEvent.ANY, new EventHandler<ScrollEvent>() {
#Override
public void handle(ScrollEvent event) {
if (event.getDeltaY() > 0) {
zoomIn();
} else {
zoomOut();
}
event.consume();
}});
You may find that you also need something like the following:
scrollPane.setOnScroll(new EventHandler<ScrollEvent>() {
#Override
public void handle(ScrollEvent event) {
if (event.getDeltaY() > 0) {
zoomIn();
} else {
zoomOut();
}
event.consume();
}
});
I added the above elaboration to another answer in this thread, but it hasn't shown up in the public feed from what I can tell. So, I've pasted it in its own answer.
This question is a bit of a duplicate, but it showed up in Google for me first, so I'm answering it. The inspiration for the above is:
Zooming in JavaFx: ScrollEvent is consumed when content size exceeds ScrollPane viewport

I think there is no direct solution.
So I would add an event filter to the ScrollPane for the SCROLL EventType and consume every event. That should prevent any mouse generated scroll events from being delegated to the ScrollPane.

Related

Persistent header fragment (disable animation) in Android TV (leanback)

Anyone knows how to achieve the question in the title? The objective is to avoid the animation that results in the Headers bar disappearing as the Leanback app zooms in on the Row Item once a Header has been clicked.
setHeadersState of BrowseSupportFragment doesn't help. Perhaps something to do with hijacking startHeadersTransitionInternal during OnHeaderClickedListener? If so, any idea how to correctly implement it?
So the problem with this one is that the transition is handled by the method startHeadersTransitionInternal which is package private. Because of this, you can't override it in most situations. However, since it's only package private and not private private, there's a little hack around this that you can do.
First, make a package in your app with the same package name as BrowseSupportFragment. Then make a class in that package that extends BrowseSupportFragment and override the offending method with no implementation. That'd look something like this:
package android.support.v17.leanback.app; // Different for AndroidX
public class HackyBrowseSupportFragment extends BrowseSupportFragment {
#Override
void startHeadersTransitionInternal(boolean withHeaders) {
// Do nothing. This avoids the transition.
}
}
Then, instead of extending BrowseSupportFragment, you'd extend HackyBrowseSupportFragment.
One thing to note that I found with this is that the back button will no longer refocus the headers from one of the rows, so you'll have to do that manually. Other than that, seems to work just fine.
Following #MichaelCeley 's response and based on the original startHeadersTransitionInternal method from BrowseSupportFragment, this implementation that keeps the backstack and listeners works, too.
#Override
void startHeadersTransitionInternal(final boolean withHeaders) {
if (getFragmentManager().isDestroyed()) {
return;
}
if (!isHeadersDataReady()) {
return;
}
new Runnable() {
#Override
public void run() {
if (mBrowseTransitionListener != null) {
mBrowseTransitionListener.onHeadersTransitionStart(withHeaders);
}
if (mHeadersBackStackEnabled) {
if (!withHeaders) {
getFragmentManager().beginTransaction()
.addToBackStack(mWithHeadersBackStackName).commit();
} else {
int index = mBackStackChangedListener.mIndexOfHeadersBackStack;
if (index >= 0) {
FragmentManager.BackStackEntry entry = getFragmentManager().getBackStackEntryAt(index);
getFragmentManager().popBackStackImmediate(entry.getId(),
FragmentManager.POP_BACK_STACK_INCLUSIVE);
}
}
}
}
}.run();
}

Disable HTML based scrolling webview javafx

Im using a custom webview to display my text i made with htmleditor.
The custom webview adjusts the size of the webview so the text fits almost perfectly in it.
I make the webview "throw" its scrollEvent to the scrollpane with vbox in it it, so its not handled by the webview itself.
But still i see the text scroll up and down a bit inside the custom webview when i scroll over it. I read on some other post its the HTML itself that handles the scrolling. But i have no idea how to disable that.
webView.setOnScroll(new EventHandler<ScrollEvent>() {
#Override
public void handle(ScrollEvent scrollEvent) {
vBox.getOnScroll().handle(scrollEvent);
scrollEvent.consume();
}
});
Well for what I understood you don´t want the scrollbar to move your page
Set the scroll bar invisible
scrollBar.setVisible(false);
Disable the scrollbar, so on move the event won´t happen
scrollPane.addEventFilter(ScrollEvent.SCROLL,new EventHandler<ScrollEvent>() {
#Override
public void handle(ScrollEvent event) {
if (event.getDeltaX() != 0) {
event.consume();
}
}
});
Or you can use scroll panes, and set them to fit.
scrollPane.setFitToHeight(true);
scrollPane.setFitToWidth(true);
Hope any of these would help.

Javafx retrievable drag and drop bug

I am creating a javafx application with a drag and drop feature. I need to be able to drag from one pane and drop to the next pane. After dropping, all dropped nodes need to be accessible. The drag and drop is working smoothly. However, I am having a problem in which not all nodes are accessible after being dropped. I would drag a node and click on it and sometimes it would respond, but sometimes a random node would no longer exhibit capabilities when I clicked on it. There was no noticeable pattern (that I could see) about which nodes stopped working and which always worked. If anyone has any idea as to why this might be a problem or how to fix it, it would be greatly appreciated. I have looked everywhere and found only one similar issue a few years ago but the page with their solution had been removed.
I will briefly explain how I set up my code structure.
The nodes I am dragging extend StackPane and I drag these nodes onto a center Pane from an ImageView in VBox. However, both of these are children of a Main layout that extends BorderPane which I have called MainEntrance.
In my node's class I have onMousePressed and onMouseDragged features as well as onContextMenuRequested features. In the class of the MainEntrance, I have drag features, as well, that handle when an object is first dragged onto the child Pane from the other child VBox.
The following are the relevant classes/methods:
VBox class: (where nodes are dragged from)
ImageView iV= new ImageView(image);
iV.setId("image");
iV.setOnDragDetected(new EventHandler<MouseEvent>() {
#Override
public void handle(MouseEvent event) {
dragAndDrop(iV, iV.getId(), event);
}
});
I have drag three ImageViews and these ImageViews are children of a VBox.
MainEntrance:
In this class I have a method that registers my drag events to my pane as such:
PView is my class extension of Pane
public void registerOnDragDropped(PView pView){
pView.setOnDragOver(new EventHandler<DragEvent>() {
#Override
public void handle(DragEvent event) {
event.acceptTransferModes(TransferMode.ANY);
System.out.println("drag over registered");
}
});
pView.setOnDragDropped(new EventHandler<DragEvent>() {
#Override
public void handle(DragEvent event) {
Dragboard db = event.getDragboard();
final String id = db.getString();
double mousePosY = event.getY();
double mousePosX = event.getX();
deviceCounter += 1;
pView.createBoardView(id, deviceCounter, mousePosX, mousePosY);
event.setDropCompleted(true);
}
});
pView.setOnDragExited(new EventHandler<DragEvent>() {
#Override
public void handle(DragEvent event) {
event.acceptTransferModes(TransferMode.ANY);
//computerView.setEffect(null);
event.consume();
}
});
}
Note: this method is in my MainEntrance class (BorderPane). In this class is when I initialize all other child nodes of MainEntrance.
Node: (draggable nodes extend StackPane)
my node's handlers are in a method that I called makeDraggable()
public void makeDraggable(Node node) {
this.setOnMousePressed(new EventHandler<MouseEvent>() {
#Override
public void handle(MouseEvent event) {
node.getScene().setCursor(Cursor.MOVE);
System.out.println("makeDraggable mouse pressed");
}
});
node.setOnMouseReleased(new EventHandler<MouseEvent>() {
#Override
public void handle(MouseEvent event) {
node.getScene().setCursor(Cursor.DEFAULT);
}
});
node.setOnMouseDragged(new EventHandler<MouseEvent>() {
#Override
public void handle(MouseEvent event) {
node.setTranslateX(node.getTranslateX() + event.getX() - 35);
node.setTranslateY(node.getTranslateY() + event.getY() - 35);
event.consume();
node.toFront();
}
});
}
If any other information is needed please let me know. Thank you in advance!
I have figured out the solution to this problem.
The problem was in the rendering order of an attachment I had placed on the node. So when I drag the node on to the screen, I have another node (a line placed on a Pane) that binds the center of the screen to the node. I did not initially think that this was the problem, so I did not even include that class or snippet. However, after playing around with it I realized that this indeed was the problem. I had to add the line to the top of the rendering order. This can be done in the following way in whatever method allows you to create a new node:
Pane line = new Pane();
//add whatever code binds your line to desired places
getChildren.add(0, line);
//add the other node as child

JavaFX opposite function of .requestFocus()?

I'm looking for function in JavaFX, where I can "disable or dismiss" the requested focus.
Here is a screenshot of my program: Screenshot
Every cell is filled with an Eventhandler (onMouseEntered and onMouseExited) and in every onMouseEntered function I have to request the focus like this:
label.requestFocus(). I need to do that, because I'm using KeyEvents to change the content of the cell.
It works fine, but there is a bug: When I move out of the scrollPane, there is still the requested focus on the last entered cell.
How can I solve that issue, without requesting focus for everything around the Scrollpane to fix this bug? Is there a function, where I can dismiss the requested focus, so after exiting a cell, it'll dismiss the focus.
Best regards,
My code:
arbeitetLabel.setOnMouseEntered(new EventHandler<MouseEvent>() {
#Override
public void handle(MouseEvent mouseEvent) {
arbeitetLabel.requestFocus();
arbeitetLabel.setOnKeyPressed(new EventHandler<KeyEvent>() {
#Override
public void handle(KeyEvent keyEvent) {
// Do some keyEvent Stuff
}
});
}
});
arbeitetLabel.setOnMouseExited(new EventHandler<MouseEvent>() {
#Override
public void handle(MouseEvent mouseEvent) {
// Dismiss the requested Focus here?
}
});
I'm passing the focus to the parent, that's it!
arbeitetLabel.getParent().requestFocus()

TextArea does not handle MouseEvent.MOUSE_PRESSED

I am building a JavaFX application and I have a TextArea inserted.
The TextArea has a CSS class assigned (don't know if it matters):
.default-cursor{
-fx-background-color:#EEEEEE;
-fx-cursor:default;
}
There are 2 issues about this TextArea:
-fx-cursor:default; Has no effect as the cursor remains the text cursor. That is weird as i use the same class for a TextField with proper/expected results
The TextArea does not handle MOUSE_PRESSED eventMy code is :
textArea.addEventHandler(MouseEvent.MOUSE_PRESSED, new EventHandler<MouseEvent>() {
#Override
public void handle(MouseEvent event) {
System.out.println("print message");
}
});
Any ideas why?
I want to note that when I changed EventHandler to handle MOUSE_CLICKED everything is fine
I suspect the default handlers for mouse events on the TextArea are consuming the mouse pressed event before it gets to your handler.
Install an EventFilter instead:
textArea.addEventFilter(MouseEvent.MOUSE_PRESSED, new EventHandler<MouseEvent>() {
#Override
public void handle(MouseEvent event) {
System.out.println("mouse pressed");
}
});
The Event filter will get processed before the default handlers see the event.
For your css issue, try
.default-cursor .content {
-fx-cursor: default ;
}

Resources