-1

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

Bruce
  • 7
  • 3
  • I don’t like questions like this that ask to do something strange with no context or explanation as to why. Why use an interactive control like a ScrollPane if the user cannot interact with it? – jewelsea Jul 06 '23 at 17:31
  • I dont mean that, i just dont want to select the scroll pane while still maintaining clicking functions of buttons inside the scroll pane – Bruce Jul 07 '23 at 10:40
  • Yes, I misunderstood what you are asking. And this is why you need to better ask your question: by providing context as to what you are doing, what you expect to happen, why you are doing it and what is not working. Include a [mcve] that replicates the issue so somebody can copy and run it without any effort or change, then see for themselves exactly what went wrong. Otherwise, you will continue to receive no answers or answers that don't actually help you, or you tackle an [xy problem](https://xyproblem.info/). – jewelsea Jul 07 '23 at 10:52
  • I updated the question, i hope you can answer it, sorry for not adding helpful stuff, thank you for fixing my mistakes :) – Bruce Jul 08 '23 at 00:59

1 Answers1

0

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.

JoFyNi
  • 1
  • 2
  • It doesnt work, i cant click the buttons inside the scroll pane, i want to add other thing, the buttons is inside a pane which is inside the scroll pane, maybe its the problem – Bruce Jul 07 '23 at 10:39
  • 1
    Can you share more of your code? – JoFyNi Jul 07 '23 at 11:06
  • I updated the question with the fxml file and a picture to better understand the problem, I hope you can fix it – Bruce Jul 08 '23 at 01:00