5 Replies Latest reply: Jun 20, 2013 5:10 AM by 963782 RSS

    There is no viewportWidthProperty() for the ScrollPane ?

    963782

      A flow pane is wrapped in the scroll pane, and I want the width of the flow pane is just equal to the width of the viewport of scroll pane once the stage shown.

      Consider these codes:

      flowPane.prefWrapLengthProperty().bind(scrollPane.widthProperty());
      

      run with no errors.

      flowPane.prefWrapLengthProperty().bind(new SimpleDoubleProperty(scrollPane.viewportBoundsProperty().get().getWidth()));
      

      run with Exception of nullPointer. I know that obviously it is the "scrollPane.viewportBoundsProperty().get()" that gives null. But I did not find effectively the "scrollPane.viewportWidthProperty()" in the JavaFX api 2.2

       

      Question:

      1. There is no viewportWidthProperty() for the ScrollPane ?

      2. If I should use the calculation "viewportWidth = width of scrollPane - width of the vertical scroll bar"? By the way, can I get the viewport or scroll bar objects from scroll pane object?

       

      Note: In fact, finally, I looked up long time the api, and I found that "setFitToWidth(true)" can realize the desired effect.

        • 1. Re: There is no viewportWidthProperty() for the ScrollPane ?
          James_D

          Since you've answered your own question, mark it as answered: setFitToWidth(true) is the way to do this.

           

          If you do need to bind to the viewport width for some reason, I think you can use

           

          someDoubleProperty.bind(new DoubleBinding() {
          { super.bind(scrollPane.viewportBounds());}
          @Override
          public double computeValue() {
          return scrollPane.getViewportBounds().getWidth();
          }
          });
          

           

          or maybe

          someDoubleProperty.bind(Bindings.selectDouble(scrollPane.viewportBounds(), "width"));
          
          • 2. Re: There is no viewportWidthProperty() for the ScrollPane ?
            963782

            I have practiced the two methods that you suggested. Use the method:

            someDoubleProperty.bind(new DoubleBinding() {  
            { super.bind(scrollPane.viewportBounds());}  
            @Override  
            public double computeValue() {  
            return scrollPane.getViewportBounds().getWidth();  
            }  
            });
            
            

            has the same problem of null pointer exception report. I think because it is due to this code snippet is placed before "primaryStage.show()", "scrollPane.getviewportBounds()" is null.

             

            Another  method:

            someDoubleProperty.bind(Bindings.selectDouble(scrollPane.viewportBoundsProperty(), "width"));

            run without exception, but does not achieve the goal. I think maybe it is because there is no "width" property (class member) defined in the class Bounds.


            All in all, I've found the solution: setFitToWidth(true);

            • 3. Re: There is no viewportWidthProperty() for the ScrollPane ?
              James_D

              So perhaps, if you ever need it:

               

              someDoubleProperty.bind(new DoubleBinding() {    
              { super.bind(scrollPane.viewportBounds());}    
              @Override    
              public double computeValue() {
              Bounds viewportBounds = scrollPane.getViewportBounds();
              if (viewportBounds == null) {
              return 0 ;
              } else {
              return scrollPane.getViewportBounds().getWidth();    
              }
              }    
              });
              
              • 4. Re: There is no viewportWidthProperty() for the ScrollPane ?
                963782

                Thanks, I learned a lot on the program skills.

                • 5. Re: There is no viewportWidthProperty() for the ScrollPane ?
                  963782

                  Hi, I encountered a weird issue:

                  In the scrollpane, I put a choice box or combo box in it. The action sequence that I took:

                  I run the application, the stage shows,  and I click on the box, the popup menu associated appears (it is normal). However, then when I scroll the scroll bar with the mouse, there is exception:

                  java.lang.NullPointerException

                    at com.sun.javafx.scene.control.skin.ScrollBarSkin$8.handle(ScrollBarSkin.java:215)

                    at com.sun.javafx.scene.control.skin.ScrollBarSkin$8.handle(ScrollBarSkin.java:199)

                    at com.sun.javafx.event.CompositeEventHandler.dispatchBubblingEvent(CompositeEventHandler.java:69)

                    at com.sun.javafx.event.EventHandlerManager.dispatchBubblingEvent(EventHandlerManager.java:217)

                    at com.sun.javafx.event.EventHandlerManager.dispatchBubblingEvent(EventHandlerManager.java:170)

                    at com.sun.javafx.event.CompositeEventDispatcher.dispatchBubblingEvent(CompositeEventDispatcher.java:38)

                    at com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:37)

                    at com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:92)

                    at com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:35)

                    at com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:92)

                    at com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:35)

                    at com.sun.javafx.scene.control.skin.ScrollPaneSkin$12.dispatchEvent(ScrollPaneSkin.java:453)

                    at com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:92)

                    at com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:35)

                    at com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:92)

                    at com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:35)

                    at com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:92)

                    at com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:35)

                    at com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:92)

                    at com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:35)

                    at com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:92)

                    at com.sun.javafx.event.EventUtil.fireEventImpl(EventUtil.java:53)

                    at com.sun.javafx.event.EventUtil.fireEvent(EventUtil.java:33)

                    at javafx.event.Event.fireEvent(Event.java:171)

                    at javafx.scene.Scene$MouseHandler.process(Scene.java:3328)

                    at javafx.scene.Scene$MouseHandler.process(Scene.java:3168)

                    at javafx.scene.Scene$MouseHandler.access$1900(Scene.java:3123)

                    at javafx.scene.Scene.impl_processMouseEvent(Scene.java:1563)

                    at javafx.scene.Scene$ScenePeerListener.mouseEvent(Scene.java:2265)

                    at com.sun.javafx.tk.quantum.GlassViewEventHandler$MouseEventNotification.run(GlassViewEventHandler.java:250)

                    at com.sun.javafx.tk.quantum.GlassViewEventHandler$MouseEventNotification.run(GlassViewEventHandler.java:173)

                    at java.security.AccessController.doPrivileged(Native Method)

                    at com.sun.javafx.tk.quantum.GlassViewEventHandler.handleMouseEvent(GlassViewEventHandler.java:292)

                    at com.sun.glass.ui.View.handleMouseEvent(View.java:528)

                    at com.sun.glass.ui.View.notifyMouse(View.java:922)

                    at com.sun.glass.ui.win.WinApplication._runLoop(Native Method)

                    at com.sun.glass.ui.win.WinApplication.access$100(WinApplication.java:29)

                    at com.sun.glass.ui.win.WinApplication$3$1.run(WinApplication.java:73)

                    at java.lang.Thread.run(Thread.java:722)

                   

                  And the scroll bar at that time is not able to be scrolled. But I released the mouse, then try to scroll again the scroll bar, it works.

                   

                   

                  When I click on the box and select an item in the list view, then I scroll the scroll bar, it works normally too.

                   

                  I appreciate the explanation and the solution related. Thanks.