JavaFX TextArea Hiding Scroll Bars
Asked Answered
E

3

9

I have a TextArea() and would like to hide the vertical/horizontal scroll bars. I see that the control seems to have a built in scroll-pane that shows as needed.

    TextArea numberPane = new TextArea();

    numberPane.setEditable(false);
    numberPane.setMaxWidth( 75 );

    // Set the characteristics of our line number pane
    numberPane.setId( "line-number-pane" );

In my CSS file I have the follow settings.

    #line-number-pane
    {
        -fx-text-fill: white;
        -fx-background-color: black;
        -fx-font: 12px "Courier New";
        -fx-font-family: "Courier New";
        -fx-font-weight: bold;
    }

    #line-number-pane .scroll-pane
    {
        -fx-hbar-policy : never;
        -fx-vbar-policy : never;
    }

As expected the text area font/color/size works just fine. However, the scroll-pane policy doesn't seem to work.

Should I be able to hide the scroll bars via the CSS file or is there some code that will do the trick.

Thanks.

Eisen answered 8/1, 2013 at 0:56 Comment(1)
It works with JavaFX 11.Boomerang
B
18

From How can I hide the scroll bar in TextArea?:

Remove Horizontal Scrollbar

textArea.setWrapText(true);

Remove Vertical Scrollbar

ScrollBar scrollBarv = (ScrollBar)ta.lookup(".scroll-bar:vertical");
scrollBarv.setDisable(true);

CSS

.text-area .scroll-bar:vertical:disabled {
    -fx-opacity: 0;
}
Bushire answered 11/11, 2013 at 7:57 Comment(3)
What is that "ta" in "ta.lookup"?River
Been a very long time since this was answered, but I think it should read textArea (or more specifically, ta is a reference to the TextArea).Bushire
TextArea instance or class? Cause I replaced that with reference to the instance and I got NullExceptionError. So nothing has been found.River
A
1

I just did it very simply using a StyleSheet:

CSS

.text-area .scroll-bar:vertical {
    -fx-pref-width: 1;
    -fx-opacity: 0;
}
.text-area .scroll-bar:horizontal {
    -fx-pref-height: 1;
    -fx-opacity: 0;
}

No need for all that whacky code.

Acumen answered 13/5, 2020 at 15:6 Comment(0)
S
0

I observed code of TextAreaSkin class, and found, that a void layoutChildren(x, y, w, h) method, which is called "during the layout pass of the scenegraph" and de facto, each time, when something happens with a control, contains a code, which changes hbarPolicy and vbarPolicy between AS_NEEDED and NEVER, according to the current state of control.

So, looks like, there is no chance to do somethign with it, using a css.

Try to just make scrollbars invisible. But, as I see code of ScrollPaneSkin, scrollBars are created once, but their visibility state seems to change during the control is working, so, instead of using setVisible(false) (which will be ignored in the nearest layout), try to use a setOpacity(0.0). (I'm not sure, it will work, but it worth to try).

Also, instead of CSS using, you can apply a recursive search of scrollBars in a control structure, using a Parent.getChildrenUnmodifiable() method, and make them invisible manually.

Stomatitis answered 8/1, 2013 at 1:39 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.