Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
 package modena;
 
 import  javafx.beans.value.ChangeListener;
 import  javafx.beans.value.ObservableValue;
 import  javafx.scene.control.ChoiceBox;
 import  javafx.scene.control.Label;
 import  javafx.scene.control.ScrollPane;
 import  javafx.scene.control.ToolBar;
 import  javafx.scene.layout.BorderPane;

Container for samplePage that has scrolling and knows how to navigate to sections
public class SamplePageNavigation extends BorderPane {
    private SamplePage samplePage = new SamplePage();
    private ScrollPane scrollPane = new ScrollPane();
    private boolean isLocalChange = false;
    public SamplePageNavigation() {
        .setId("SamplePageScrollPane");
        setCenter();
        ToolBar toolBar = new ToolBar();
        toolBar.setId("SamplePageToolBar");
        toolBar.getStyleClass().add("bottom");
        toolBar.getItems().add(new Label("Go to section:"));
        final ChoiceBox<SamplePage.SectionsectionChoiceBox = new ChoiceBox<>();
        sectionChoiceBox.getSelectionModel().selectedItemProperty().addListener(new ChangeListener<SamplePage.Section>() {
            @Override public void changed(ObservableValue<? extends SamplePage.SectionobservableSamplePage.Section oldValueSamplePage.Section newValue) {
                setCurrentSection(newValue);
            }
        });
        sectionChoiceBox.getItems().addAll(.getSections());
        toolBar.getItems().add(sectionChoiceBox);
        setBottom(toolBar);
        .vvalueProperty().addListener(new ChangeListener<Number>() {
            @Override public void changed(ObservableValue<? extends NumberobservableNumber oldValueNumber newValue) {
                if (!) {
                     = true;
                    // calc scroll position relative to scroll pane content
                    double posPixels = .getLayoutBounds().getHeight() * newValue.doubleValue();
                    // move to top of view port
                    posPixels -=  .getLayoutBounds().getHeight() * newValue.doubleValue();
                    // move to center of view port
                    posPixels +=  .getLayoutBounds().getHeight() * 0.5;
                    // find section that contains view port center
                     = null;
                    for (SamplePage.Section section.getSections()) {
                        if (section.box.getBoundsInParent().getMaxY() > posPixels ) {
                             = section;
                            break;
                        }
                    }
                    sectionChoiceBox.getSelectionModel().select();
                     = false;
                }
            }
        });
    }
    public SamplePage.Section getCurrentSection() {
        return ;
    }
    public void setCurrentSection(SamplePage.Section currentSection) {
        this. = currentSection;
        if (!) {
             = true;
            double pos = 0;
            if (currentSection != null) {
                double sectionBoxCenterY = currentSection.box.getBoundsInParent().getMinY()
                        + (currentSection.box.getBoundsInParent().getHeight()/2);
                // move to center of view port
                pos -=  .getLayoutBounds().getHeight() * 0.5;
                // move to top of view port
                pos +=  .getLayoutBounds().getHeight() * (sectionBoxCenterY / .getLayoutBounds().getHeight());
                // find relative pos
                pos = sectionBoxCenterY / .getLayoutBounds().getHeight();
            }
            .setVvalue(pos);
             = false;
        }
    }
    public SamplePage getSamplePage() {
        return ;
    }
New to GrepCode? Check out our FAQ X