java - How to set the maximum size of a JavaFX ScrollPane and make Scrollbars appear only when needed? -


i'm trying build single window application know javafx better. quite nice , easy, until did not details...

i have anchorpane main container of other gui elements. realized, high laptop screen (805 px high, 600 px wide), decided put anchorpane inside scrollpane have scroll bars, when shrink window. anchorpane configured in fxml, scrollpane in java source code.

the anchorpane:

<anchorpane maxheight="805.0" prefheight="805.0" prefwidth="600.0" xmlns="http://javafx.com/javafx/8.0.65" xmlns:fx="http://javafx.com/fxml/1" fx:controller="com.jzoli.mp3checker.view.mainwindowcontroller"> 

...

the scrollpane:

public class scrollablemainframe extends scrollpane {  public scrollablemainframe(pane content) {     super();      // set scrollbar policy     this.sethbarpolicy(scrollbarpolicy.as_needed);     this.setvbarpolicy(scrollbarpolicy.as_needed);      // set main window in scroll pane     this.setcontent(content); } 

}

i load fxml, put anchorpane inside scrollpane, , let show:

private final void initwindow() {     try {         // load main window layout fxml file.         url mainwindowurl = mainapp.class.getresource("view/mainwindow.fxml");         fxmlloader loader = new fxmlloader(mainwindowurl, guilabels);         mainwindow = (anchorpane) loader.load();         mainwindowcontroller controller = loader.getcontroller();         controller.setmainappandguilabels(this);          // create scrollable pane, , put inside         scrollablemainframe = new scrollablemainframe(mainwindow);          // show scene containing layout.         scene scene = new scene(scrollablemainframe);         primarystage.setscene(scene);                     primarystage.show();     } catch (ioexception e) {         log.error("error loading gui!", e);     } } 

so far good, window shows , has no scrollbars until not shrink it. i'd maximize window, has no sense make larger (the anchorpane has fixed size), smaller. have figured out, 1 must set max size of primarystage limit actual window, limiting scrollpane has no effect.

and here problem: if want set maxheight , maxwidth primaystage, unwanted results. if want primarystage have same max size anchorpane, window either don't show up, or has scroll bars!

if put line in initwindow mehtod

        // show scene containing layout.         scene scene = new scene(scrollablemainframe);         primarystage.setscene(scene);         // set max window size         primarystage.setmaxheight(scrollablemainframe.getheight());         primarystage.show(); 

nothing appear, apparently 'scrollablemainframe' has no hight @ point.

if place setmaxheight() @ end, like

    primarystage.setscene(scene);     primarystage.show();     // set max window size     primarystage.setmaxheight(scrollablemainframe.getheight()); 

then max hight set, scroll bars appear , stay visible, if window has full size!

does know why, , how set max sizes window, without scroll bars on?

(simply adding numbers max primarystage.setmaxheight(scrollablemainframe.getheight() + 15); doesn't @ all, scroll bars still there!)

thanks, james_d, lead me solution!

it indeed said, scrollbars appear, because primarystage contains title bar, forgot. made me think: how can calculate full size of window based on content, set max size? well, don't need to! logic twisted, works: need ask primarystage actual size, , set maximum. trick is, need after window has been created:

        // create window         primarystage.show();         // set actual size max         primarystage.setmaxheight(primarystage.getheight());         primarystage.setmaxwidth(primarystage.getwidth()); 

Comments

Popular posts from this blog

javascript - jQuery: Add class depending on URL in the best way -

caching - How to check if a url path exists in the service worker cache -

Redirect to a HTTPS version using .htaccess -