Home > Blockchain >  How do I remove scroll buttons from scroll bar in a scrollPane Javafx
How do I remove scroll buttons from scroll bar in a scrollPane Javafx

Time:12-29

Here`s code from css file:

.scroll-bar .button
{
visibility: hidden;
-fx-background-color: black;
}

But it has no effect on the scroll bar buttons:

please give me a hint how to solve this

CodePudding user response:

I came up with something which seems to do what you want. I make absolutely no guarantees that it will continue to work for later JavaFX versions.

What I did was look at the default modena style sheet. Then try to remove all padding and insets related to the scroll bar buttons, effectively making them invisible, and apply a preferred size to the scroll bars (because they were basing the preferred size off of the buttons).

There are probably more effective ways of handling this and hiding things. And perhaps it could be done with fewer rules, but anyway, this is what I came up with and it appeared to work.

Also, if you make the scroll pane really small, the default functionality is to hide the thumb and allow movement around the scroll pane by only the buttons. In your case the buttons aren't there, so don't let the scroll pane become too small or you lose panning capabilities for the scroll pane.

Anyway, here is some example code:

import javafx.application.Application;
import javafx.geometry.Insets;
import javafx.scene.Scene;
import javafx.scene.control.ScrollPane;
import javafx.scene.layout.VBox;
import javafx.scene.text.Text;
import javafx.stage.Stage;

import java.util.stream.Collectors;
import java.util.stream.IntStream;

public class NoButtonScroll extends Application {

    private static final Lorem lorem = new Lorem();

    private static final String NO_BUTTON_SCROLL_BARS = """
            data:text/css,
            .scroll-bar:vertical {
                -fx-pref-width: 1.12em;
            }
            .scroll-bar:horizontal {
                -fx-pref-height: 1.12em;
            }
            .scroll-pane > .scroll-bar:horizontal > .increment-button,
            .scroll-pane > .scroll-bar:horizontal > .decrement-button {
                -fx-padding: 0 0 0 0;
            }
            .scroll-pane > .scroll-bar:vertical > .increment-button,
            .scroll-pane > .scroll-bar:vertical > .decrement-button {
                -fx-padding: 0 0 0 0;
            }
            .scroll-bar > .increment-button,
            .scroll-bar > .decrement-button {
                -fx-padding: 0 0 0 0;
            }
            .scroll-bar:horizontal > .increment-button,
            .scroll-bar:horizontal > .decrement-button {
                -fx-background-insets: 0 0 0 0 , 0 0 0 0, 0 0 0 0, 0 0 0 0 0;
            }
            .scroll-bar:vertical > .increment-button,
            .scroll-bar:vertical > .decrement-button {
                -fx-background-insets: 0 0 0 0, 0 0 0 0, 0 0 0 0;
            }
            .scroll-bar:horizontal > .decrement-button > .decrement-arrow {
                -fx-padding: 0 0 0 0;
            }
            .scroll-bar:horizontal > .increment-button > .increment-arrow {
                -fx-padding: 0 0 0 0;
            }
            .scroll-bar:vertical > .decrement-button > .decrement-arrow {
                -fx-padding: 0 0 0 0;
            }
            .scroll-bar:vertical > .increment-button > .increment-arrow {
                -fx-padding: 0 0 0 0;
            }
            """;

     @Override public void start(Stage stage) {
         ScrollPane standardScrollPane = new ScrollPane(generateText());
         ScrollPane noButtonScrollPane = new ScrollPane(generateText());
         noButtonScrollPane.getStylesheets().add(NO_BUTTON_SCROLL_BARS);

         final VBox layout = new VBox(10, standardScrollPane, noButtonScrollPane);
         layout.setPadding(new Insets(10));
         layout.setPrefSize(600, 400);

         stage.setScene(new Scene(layout));
         stage.show();
    }

    private Text generateText() {
        Text sampleText = new Text(lorem.nextText(500));
        sampleText.setWrappingWidth(800);

        return sampleText;
    }

    // class for generating example text for test data, not integral to the solution.
    private static final class Lorem {
        private static final String[] lorem = "lorem ipsum dolor sit amet consectetur adipiscing elit sed do eiusmod tempor incididunt ut labore et dolore magna aliqua".split(" ");
        private int idx = 0;

        public String nextWord() {
            return lorem[getAndIncrementIdx()];
        }

        public String nextText(int nWords) {
            return IntStream.range(0, nWords)
                    .mapToObj(i -> nextWord())
                    .collect(Collectors.joining(" "));
        }

        private int getAndIncrementIdx() {
            int retVal = idx;

            idx = (idx   1) % lorem.length;

            return retVal;
        }
    }

    public static void main(String[] args) { launch(args); }

}

CodePudding user response:

I saw a solution for removing scroll arrows on JavaFX context menu, maybe this could help:

.context-menu .scroll-arrow {
 -fx-padding: 0 0 0 0;
}

.context-menu .menu-item {
 -fx-border-style: solid;
 -fx-border-color: transparent;
}

Source: https://gist.github.com/oowekyala/ccc67aa8663e788c072d3824b1c3d22a

  • Related