Is there a way to disable the click in JavaFX ScrollPane?

206 Views Asked by At

I want my ScrollPane to be not clickable but the components inside of it must be active.

I tried this but I can't click inside the ScrollPane.

buttonScrollPane.addEventFilter(MouseEvent.MOUSE_PRESSED, MouseEvent::consume);
buttonScrollPane.addEventFilter(MouseEvent.MOUSE_RELEASED, MouseEvent::consume);
buttonScrollPane.addEventFilter(MouseEvent.MOUSE_DRAGGED, MouseEvent::consume);
buttonScrollPane.addEventFilter(MouseEvent.MOUSE_CLICKED, MouseEvent::consume);

I also tried this css code and still dont work

style="user-select: none;"

here's the fxml code

<?xml version="1.0" encoding="UTF-8"?>

<?import javafx.scene.control.Button?>
<?import javafx.scene.control.Label?>
<?import javafx.scene.control.ScrollPane?>
<?import javafx.scene.control.TextField?>
<?import javafx.scene.layout.Pane?>

<Pane fx:id="showAppsAndSitesPane" maxHeight="-Infinity" maxWidth="-Infinity" minHeight="-Infinity" minWidth="-Infinity" prefHeight="720.0" prefWidth="1280.0" style="user-select: none;" xmlns="http://javafx.com/javafx/8.0.171" xmlns:fx="http://javafx.com/fxml/1" fx:controller="passwordmanager.controllers.Controller">
   <Label fx:id="noOfAppsAndSitesLabel" layoutX="180.0" layoutY="60.0" scaleX="2.0" scaleY="2.0" scaleZ="2.0" text="Apps / Sites" />
   <Button fx:id="addAppAndSiteButton" layoutX="1000.0" layoutY="60.0" mnemonicParsing="false" onAction="#showAddAppsAndSite" scaleX="2.0" scaleY="2.0" scaleZ="2.0" text="Add App / Site" />
   <ScrollPane fx:id="buttonScrollPane" hbarPolicy="NEVER" layoutX="320.0" layoutY="266.0" prefHeight="300.0" prefWidth="640.0" scaleX="2.0" scaleY="2.0" scaleZ="2.0" vbarPolicy="ALWAYS">
         <Pane fx:id="buttonPane" prefHeight="300.0" prefWidth="625.0" />
   </ScrollPane>
   <Pane fx:id="addAppAndSitePane" layoutX="892.0" layoutY="120.0" maxHeight="-Infinity" maxWidth="-Infinity" minHeight="-Infinity" minWidth="-Infinity" prefHeight="278.0" prefWidth="366.0" visible="false">
      <Label alignment="CENTER" layoutX="158.0" layoutY="26.0" prefWidth="80.0" scaleX="3.0" scaleY="3.0" scaleZ="3.0" text="Add App / Site" />
      <TextField fx:id="appOrSiteField" layoutX="135.0" layoutY="95.0" maxHeight="-Infinity" maxWidth="-Infinity" minHeight="-Infinity" minWidth="-Infinity" prefWidth="149.0" promptText="Site" scaleX="2.0" scaleY="2.0" scaleZ="2.0" />
      <Label fx:id="errorLabel" layoutX="135.0" layoutY="155.0" prefHeight="25.0" prefWidth="149.0" scaleX="2.0" scaleY="2.0" scaleZ="2.0" visible="false" />
      <Button fx:id="addAppOrSiteButton" layoutX="135.0" layoutY="155.0" maxHeight="-Infinity" maxWidth="-Infinity" minHeight="-Infinity" minWidth="-Infinity" mnemonicParsing="false" onAction="#addAppsAndSite" prefWidth="149.0" scaleX="2.0" scaleY="2.0" scaleZ="2.0" text="Add" />
      <Button fx:id="cancelButton" layoutX="135.0" layoutY="215.0" maxHeight="-Infinity" maxWidth="-Infinity" minHeight="-Infinity" minWidth="-Infinity" mnemonicParsing="false" onAction="#hideAddAppsAndSite" prefWidth="149.0" scaleX="2.0" scaleY="2.0" scaleZ="2.0" text="Cancel" />
   </Pane>
</Pane>

As you can see, I can click/select the scroll pane which shouldn't be, unlike the JScrollPane in java swing which you can't select/click the JscrollPane, only the scroll bar, so thats my problem

1

There are 1 best solutions below

3
JoFyNi On

To make a scroll pane non-clickable while still allowing interaction with the components inside it, you can use a combination of event filters and event handlers. Here's an approach you can try:

buttonScrollPane.addEventFilter(MouseEvent.ANY, MouseEvent::consume);
buttonScrollPane.addEventFilter(ScrollEvent.ANY, ScrollEvent::consume);
buttonScrollPane.addEventHandler(MouseEvent.MOUSE_ENTERED, e -> {
    buttonScrollPane.setMouseTransparent(true);
});

buttonScrollPane.addEventHandler(MouseEvent.MOUSE_EXITED, e -> {
    buttonScrollPane.setMouseTransparent(false);
});

Explanation:

The addEventFilter method is used to consume all mouse events (including MOUSE_PRESSED, MOUSE_RELEASED, MOUSE_DRAGGED, and MOUSE_CLICKED) on the scroll pane, preventing them from being processed further. Additionally, you can consume ScrollEvent using addEventFilter to disable scrolling on the scroll pane. To allow interaction with the components inside the scroll pane, we set the mouseTransparent property of the scroll pane to true when the mouse enters the scroll pane, and set it back to false when the mouse exits. This property determines whether the node (in this case, the scroll pane) should be transparent to mouse events or not. By following this approach, the scroll pane itself will be non-clickable, but the components inside it will still receive mouse events and be clickable.