7

How to remove the scrollbar automatically in the WebView of Javafx ?

When you click "Cadastre" will open a screen, this screen is in javascript and is unconfigured because of the scrollbar, so we wanted to remove it.

enter image description here

1 Answer 1

18

Normally for a ScrollPane you would do something like this:

scrollPane.setHbarPolicy(ScrollBarPolicy.NEVER);
scrollPane.setVbarPolicy(ScrollBarPolicy.NEVER);

However, the scrollbars inside WebView are not your JavaFX UI control, but a part of the displayed webpage. Thus, you control them with CSS:

body {
    overflow-x: hidden;
    overflow-y: hidden;
}

You can save this as a .css and apply it as a user stylesheet, similarly user style sheets in normal browsers, using this code:

webView.getEngine().setUserStyleSheetLocation("path/to/style.css");

If the user stylesheet you created is a part of your project's resources then you should externalize it so:

webView.getEngine().setUserStyleSheetLocation(getClass().getResource("/path/to/style.css").toExternalForm());
6
  • I'm actually creating a browser and every time I opening screens in javascript the scrollbars appear and desconfiguram the screens.
    – Folie
    Mar 19, 2013 at 12:33
  • Not sure what you mean by opening screens in javascript, you should describe your problem more clearly in your original question
    – Dreen
    Mar 19, 2013 at 12:43
  • When you click "Cadastre" will open a screen, this screen is in javascript and is unconfigured because of the scrollbar, so we wanted to remove it
    – Folie
    Mar 19, 2013 at 12:49
  • I cant make out anything from the picture or your comments, have you tried applying the user stylesheet to the webview as described?
    – Dreen
    Mar 19, 2013 at 13:30
  • This code will remove the scrollbars of any page loaded in the WebView once it is loaded (even when following links to other pages): engine.getLoadWorker().stateProperty().addListener(new ChangeListener<State>() { public void changed(ObservableValue<? extends State> o, State old, final State state) { if (state == State.SUCCEEDED) { System.out.println("Page loaded: " + engine.getLocation()); engine.executeScript("document.style.overflow = 'hidden';"); } } }); Oct 6, 2014 at 12:17

Not the answer you're looking for? Browse other questions tagged or ask your own question.