Keep or set JavaFX Scrollpane position when content resized











up vote
-1
down vote

favorite












I want to set Scrollpane H and V values but after inner content was resized.



Some context - I got following structure:



enter image description here



I attached .setOnScroll(...) (mouse) event to StackPane and when it happens, the inner content is resized (image) to some values (aka zoom). I want to add that StackPane min size is binded to scrollpane viewport size and Pane size binded to image size to keep everything working properly.



Up to this point everything works fine, when content changes size, scrollbars are updated and everything adjust as it supossed to be but I want to keep or rather set scrollbars positions to some values to keep zooming to previous middle point (something like zoom in photoshop). In this case I added this line in my callback to test it:



scrollPane.setHvalue(0.5);


The function itself works but it's overridden later (probably when layout recalculate) and this way it's always wrong. I tested values via event



scrollPane.hvalueProperty().addListener((observable, oldvalue, newvalue) -> {
System.out.println(newvalue);
});


and the output is kind of



0.5
0.45172283226050736
0.5
0.45196805476326296
0.5
0.4522703818369453

// or this when scaled down

0.5
0.5591296121097445
0.5
0.5608324439701174


That's why my conclusion is that I succeed setting 0.5 but later layout probably set different value for resized content becuase i changed size restrictions to Pane and StackPane.



My question then how should I do this?



Is there any onResize event to corrent or force my own value? or other way to schedule scroll event or my updates to be done when layout is recalculated?



// EDIT



I cleaned the code and here is example:



Main.java



package application;

import javafx.application.Application;
import javafx.stage.Stage;
import javafx.scene.Scene;
import javafx.scene.layout.BorderPane;
import javafx.fxml.FXMLLoader;


public class Main extends Application {
@Override
public void start(Stage primaryStage) {
try {
BorderPane root = (BorderPane)FXMLLoader.load(getClass().getResource("Sample.fxml"));
Scene scene = new Scene(root,800,600);
scene.getStylesheets().add(getClass().getResource("application.css").toExternalForm());
primaryStage.setScene(scene);
primaryStage.show();
} catch(Exception e) {
e.printStackTrace();
}
}

public static void main(String args) {
launch(args);
}
}


application.css is empty



/* JavaFX CSS - Leave this comment until you have at least create one rule which uses -fx-Property */


Sample.fxml (replace image url)



<?xml version="1.0" encoding="UTF-8"?>

<?import javafx.scene.control.ScrollPane?>
<?import javafx.scene.image.Image?>
<?import javafx.scene.image.ImageView?>
<?import javafx.scene.layout.BorderPane?>
<?import javafx.scene.layout.Pane?>
<?import javafx.scene.layout.StackPane?>

<BorderPane xmlns="http://javafx.com/javafx/8.0.171" xmlns:fx="http://javafx.com/fxml/1" fx:controller="application.SampleController">
<center>
<ScrollPane fx:id="scrollPane" prefHeight="600.0" prefWidth="800.0" BorderPane.alignment="CENTER">
<content>
<StackPane fx:id="stackPane">
<children>
<Pane fx:id="clipContainer">
<children>
<ImageView fx:id="img" pickOnBounds="true" preserveRatio="true">
<image>
<Image url="@../../../../Desktop/sample.jpg" />
</image>
</ImageView>
</children>
</Pane>
</children>
</StackPane>
</content>
</ScrollPane>
</center>
</BorderPane>


and SampleController.java



package application;

import java.net.URL;
import java.util.ResourceBundle;
import javafx.beans.binding.Bindings;
import javafx.fxml.FXML;
import javafx.fxml.Initializable;
import javafx.scene.control.ScrollPane;
import javafx.scene.image.ImageView;
import javafx.scene.layout.Pane;
import javafx.scene.layout.StackPane;


public class SampleController implements Initializable {


@FXML
private Pane clipContainer;

@FXML
private StackPane stackPane;

@FXML
private ImageView img;

@FXML
private ScrollPane scrollPane;

final double SCALE_DELTA = 1.1;

private double imgW;
private double imgH;
private double scale = 1;


@Override
public void initialize(URL location, ResourceBundle resources) {


clipContainer.setStyle("-fx-background-color: #999999");
stackPane.setStyle("-fx-background-color: #CCCCCC");

imgW = img.getImage().getWidth();
imgH = img.getImage().getHeight();

// bind max and min to adjust to new image bounds

stackPane.minWidthProperty().bind(Bindings.createDoubleBinding(() ->
scrollPane.getViewportBounds().getWidth(), scrollPane.viewportBoundsProperty()
));
stackPane.minHeightProperty().bind(Bindings.createDoubleBinding(() ->
scrollPane.getViewportBounds().getHeight(), scrollPane.viewportBoundsProperty()
));

clipContainer.maxWidthProperty().bind(Bindings.createDoubleBinding(() ->
img.getFitWidth(), img.fitWidthProperty()
));
clipContainer.maxHeightProperty().bind(Bindings.createDoubleBinding(() ->
img.getFitHeight(), img.fitHeightProperty()
));

// initial scale
img.setFitWidth(imgW * 1);
img.setFitHeight(imgH * 1);

// on mouse scroll
stackPane.setOnScroll(event -> {
event.consume();

if (event.getDeltaY() == 0) {
return;
}

double scaleFactor =
(event.getDeltaY() > 0)
? SCALE_DELTA
: 1/SCALE_DELTA;


scale *= scaleFactor;

img.setFitWidth(imgW * scale);
img.setFitHeight(imgH * scale);

// HERE i want to do something to keep my image where it was
scrollPane.setHvalue(0.5);

});

scrollPane.hvalueProperty().addListener((observable, oldvalue, newvalue) -> {
System.out.println(newvalue);
});

}
}









share|improve this question
























  • Why did you even use a ScrollPane? Using ScrollPane to resize instead of scrolling up/down/left/right sounds like bad usage to me. setOnScroll() can be used on any node.
    – Jai
    Nov 20 at 5:47










  • @Jai I don't use ScrollPane to resize but mouse scroll event to resize inner image. Scrollpane is used to navigate zoomed image - it's exactly the same situation like zoomed canvas in photoshop and you navigate by scrollbars. The problem I got is that I want to keep image in position before zooming that's why i try to set scrollbar to some position but whatever I set it will be changed later, probably after layout realize that inner content changed size and scrollbar max, min and value have to be updated I belive.
    – Griva
    Nov 20 at 6:14










  • Please provide a Minimal, Complete, and Verifiable example that demonstrates the problem.
    – kleopatra
    Nov 20 at 9:15










  • @kleopatra Sorry I did not do it before, code was total mess and I had to clear it much. I thought it's rather easy problem and will be easy task to fix it - my bad. I edited question and added code with example so check it if you can, thank you.
    – Griva
    Nov 20 at 16:18















up vote
-1
down vote

favorite












I want to set Scrollpane H and V values but after inner content was resized.



Some context - I got following structure:



enter image description here



I attached .setOnScroll(...) (mouse) event to StackPane and when it happens, the inner content is resized (image) to some values (aka zoom). I want to add that StackPane min size is binded to scrollpane viewport size and Pane size binded to image size to keep everything working properly.



Up to this point everything works fine, when content changes size, scrollbars are updated and everything adjust as it supossed to be but I want to keep or rather set scrollbars positions to some values to keep zooming to previous middle point (something like zoom in photoshop). In this case I added this line in my callback to test it:



scrollPane.setHvalue(0.5);


The function itself works but it's overridden later (probably when layout recalculate) and this way it's always wrong. I tested values via event



scrollPane.hvalueProperty().addListener((observable, oldvalue, newvalue) -> {
System.out.println(newvalue);
});


and the output is kind of



0.5
0.45172283226050736
0.5
0.45196805476326296
0.5
0.4522703818369453

// or this when scaled down

0.5
0.5591296121097445
0.5
0.5608324439701174


That's why my conclusion is that I succeed setting 0.5 but later layout probably set different value for resized content becuase i changed size restrictions to Pane and StackPane.



My question then how should I do this?



Is there any onResize event to corrent or force my own value? or other way to schedule scroll event or my updates to be done when layout is recalculated?



// EDIT



I cleaned the code and here is example:



Main.java



package application;

import javafx.application.Application;
import javafx.stage.Stage;
import javafx.scene.Scene;
import javafx.scene.layout.BorderPane;
import javafx.fxml.FXMLLoader;


public class Main extends Application {
@Override
public void start(Stage primaryStage) {
try {
BorderPane root = (BorderPane)FXMLLoader.load(getClass().getResource("Sample.fxml"));
Scene scene = new Scene(root,800,600);
scene.getStylesheets().add(getClass().getResource("application.css").toExternalForm());
primaryStage.setScene(scene);
primaryStage.show();
} catch(Exception e) {
e.printStackTrace();
}
}

public static void main(String args) {
launch(args);
}
}


application.css is empty



/* JavaFX CSS - Leave this comment until you have at least create one rule which uses -fx-Property */


Sample.fxml (replace image url)



<?xml version="1.0" encoding="UTF-8"?>

<?import javafx.scene.control.ScrollPane?>
<?import javafx.scene.image.Image?>
<?import javafx.scene.image.ImageView?>
<?import javafx.scene.layout.BorderPane?>
<?import javafx.scene.layout.Pane?>
<?import javafx.scene.layout.StackPane?>

<BorderPane xmlns="http://javafx.com/javafx/8.0.171" xmlns:fx="http://javafx.com/fxml/1" fx:controller="application.SampleController">
<center>
<ScrollPane fx:id="scrollPane" prefHeight="600.0" prefWidth="800.0" BorderPane.alignment="CENTER">
<content>
<StackPane fx:id="stackPane">
<children>
<Pane fx:id="clipContainer">
<children>
<ImageView fx:id="img" pickOnBounds="true" preserveRatio="true">
<image>
<Image url="@../../../../Desktop/sample.jpg" />
</image>
</ImageView>
</children>
</Pane>
</children>
</StackPane>
</content>
</ScrollPane>
</center>
</BorderPane>


and SampleController.java



package application;

import java.net.URL;
import java.util.ResourceBundle;
import javafx.beans.binding.Bindings;
import javafx.fxml.FXML;
import javafx.fxml.Initializable;
import javafx.scene.control.ScrollPane;
import javafx.scene.image.ImageView;
import javafx.scene.layout.Pane;
import javafx.scene.layout.StackPane;


public class SampleController implements Initializable {


@FXML
private Pane clipContainer;

@FXML
private StackPane stackPane;

@FXML
private ImageView img;

@FXML
private ScrollPane scrollPane;

final double SCALE_DELTA = 1.1;

private double imgW;
private double imgH;
private double scale = 1;


@Override
public void initialize(URL location, ResourceBundle resources) {


clipContainer.setStyle("-fx-background-color: #999999");
stackPane.setStyle("-fx-background-color: #CCCCCC");

imgW = img.getImage().getWidth();
imgH = img.getImage().getHeight();

// bind max and min to adjust to new image bounds

stackPane.minWidthProperty().bind(Bindings.createDoubleBinding(() ->
scrollPane.getViewportBounds().getWidth(), scrollPane.viewportBoundsProperty()
));
stackPane.minHeightProperty().bind(Bindings.createDoubleBinding(() ->
scrollPane.getViewportBounds().getHeight(), scrollPane.viewportBoundsProperty()
));

clipContainer.maxWidthProperty().bind(Bindings.createDoubleBinding(() ->
img.getFitWidth(), img.fitWidthProperty()
));
clipContainer.maxHeightProperty().bind(Bindings.createDoubleBinding(() ->
img.getFitHeight(), img.fitHeightProperty()
));

// initial scale
img.setFitWidth(imgW * 1);
img.setFitHeight(imgH * 1);

// on mouse scroll
stackPane.setOnScroll(event -> {
event.consume();

if (event.getDeltaY() == 0) {
return;
}

double scaleFactor =
(event.getDeltaY() > 0)
? SCALE_DELTA
: 1/SCALE_DELTA;


scale *= scaleFactor;

img.setFitWidth(imgW * scale);
img.setFitHeight(imgH * scale);

// HERE i want to do something to keep my image where it was
scrollPane.setHvalue(0.5);

});

scrollPane.hvalueProperty().addListener((observable, oldvalue, newvalue) -> {
System.out.println(newvalue);
});

}
}









share|improve this question
























  • Why did you even use a ScrollPane? Using ScrollPane to resize instead of scrolling up/down/left/right sounds like bad usage to me. setOnScroll() can be used on any node.
    – Jai
    Nov 20 at 5:47










  • @Jai I don't use ScrollPane to resize but mouse scroll event to resize inner image. Scrollpane is used to navigate zoomed image - it's exactly the same situation like zoomed canvas in photoshop and you navigate by scrollbars. The problem I got is that I want to keep image in position before zooming that's why i try to set scrollbar to some position but whatever I set it will be changed later, probably after layout realize that inner content changed size and scrollbar max, min and value have to be updated I belive.
    – Griva
    Nov 20 at 6:14










  • Please provide a Minimal, Complete, and Verifiable example that demonstrates the problem.
    – kleopatra
    Nov 20 at 9:15










  • @kleopatra Sorry I did not do it before, code was total mess and I had to clear it much. I thought it's rather easy problem and will be easy task to fix it - my bad. I edited question and added code with example so check it if you can, thank you.
    – Griva
    Nov 20 at 16:18













up vote
-1
down vote

favorite









up vote
-1
down vote

favorite











I want to set Scrollpane H and V values but after inner content was resized.



Some context - I got following structure:



enter image description here



I attached .setOnScroll(...) (mouse) event to StackPane and when it happens, the inner content is resized (image) to some values (aka zoom). I want to add that StackPane min size is binded to scrollpane viewport size and Pane size binded to image size to keep everything working properly.



Up to this point everything works fine, when content changes size, scrollbars are updated and everything adjust as it supossed to be but I want to keep or rather set scrollbars positions to some values to keep zooming to previous middle point (something like zoom in photoshop). In this case I added this line in my callback to test it:



scrollPane.setHvalue(0.5);


The function itself works but it's overridden later (probably when layout recalculate) and this way it's always wrong. I tested values via event



scrollPane.hvalueProperty().addListener((observable, oldvalue, newvalue) -> {
System.out.println(newvalue);
});


and the output is kind of



0.5
0.45172283226050736
0.5
0.45196805476326296
0.5
0.4522703818369453

// or this when scaled down

0.5
0.5591296121097445
0.5
0.5608324439701174


That's why my conclusion is that I succeed setting 0.5 but later layout probably set different value for resized content becuase i changed size restrictions to Pane and StackPane.



My question then how should I do this?



Is there any onResize event to corrent or force my own value? or other way to schedule scroll event or my updates to be done when layout is recalculated?



// EDIT



I cleaned the code and here is example:



Main.java



package application;

import javafx.application.Application;
import javafx.stage.Stage;
import javafx.scene.Scene;
import javafx.scene.layout.BorderPane;
import javafx.fxml.FXMLLoader;


public class Main extends Application {
@Override
public void start(Stage primaryStage) {
try {
BorderPane root = (BorderPane)FXMLLoader.load(getClass().getResource("Sample.fxml"));
Scene scene = new Scene(root,800,600);
scene.getStylesheets().add(getClass().getResource("application.css").toExternalForm());
primaryStage.setScene(scene);
primaryStage.show();
} catch(Exception e) {
e.printStackTrace();
}
}

public static void main(String args) {
launch(args);
}
}


application.css is empty



/* JavaFX CSS - Leave this comment until you have at least create one rule which uses -fx-Property */


Sample.fxml (replace image url)



<?xml version="1.0" encoding="UTF-8"?>

<?import javafx.scene.control.ScrollPane?>
<?import javafx.scene.image.Image?>
<?import javafx.scene.image.ImageView?>
<?import javafx.scene.layout.BorderPane?>
<?import javafx.scene.layout.Pane?>
<?import javafx.scene.layout.StackPane?>

<BorderPane xmlns="http://javafx.com/javafx/8.0.171" xmlns:fx="http://javafx.com/fxml/1" fx:controller="application.SampleController">
<center>
<ScrollPane fx:id="scrollPane" prefHeight="600.0" prefWidth="800.0" BorderPane.alignment="CENTER">
<content>
<StackPane fx:id="stackPane">
<children>
<Pane fx:id="clipContainer">
<children>
<ImageView fx:id="img" pickOnBounds="true" preserveRatio="true">
<image>
<Image url="@../../../../Desktop/sample.jpg" />
</image>
</ImageView>
</children>
</Pane>
</children>
</StackPane>
</content>
</ScrollPane>
</center>
</BorderPane>


and SampleController.java



package application;

import java.net.URL;
import java.util.ResourceBundle;
import javafx.beans.binding.Bindings;
import javafx.fxml.FXML;
import javafx.fxml.Initializable;
import javafx.scene.control.ScrollPane;
import javafx.scene.image.ImageView;
import javafx.scene.layout.Pane;
import javafx.scene.layout.StackPane;


public class SampleController implements Initializable {


@FXML
private Pane clipContainer;

@FXML
private StackPane stackPane;

@FXML
private ImageView img;

@FXML
private ScrollPane scrollPane;

final double SCALE_DELTA = 1.1;

private double imgW;
private double imgH;
private double scale = 1;


@Override
public void initialize(URL location, ResourceBundle resources) {


clipContainer.setStyle("-fx-background-color: #999999");
stackPane.setStyle("-fx-background-color: #CCCCCC");

imgW = img.getImage().getWidth();
imgH = img.getImage().getHeight();

// bind max and min to adjust to new image bounds

stackPane.minWidthProperty().bind(Bindings.createDoubleBinding(() ->
scrollPane.getViewportBounds().getWidth(), scrollPane.viewportBoundsProperty()
));
stackPane.minHeightProperty().bind(Bindings.createDoubleBinding(() ->
scrollPane.getViewportBounds().getHeight(), scrollPane.viewportBoundsProperty()
));

clipContainer.maxWidthProperty().bind(Bindings.createDoubleBinding(() ->
img.getFitWidth(), img.fitWidthProperty()
));
clipContainer.maxHeightProperty().bind(Bindings.createDoubleBinding(() ->
img.getFitHeight(), img.fitHeightProperty()
));

// initial scale
img.setFitWidth(imgW * 1);
img.setFitHeight(imgH * 1);

// on mouse scroll
stackPane.setOnScroll(event -> {
event.consume();

if (event.getDeltaY() == 0) {
return;
}

double scaleFactor =
(event.getDeltaY() > 0)
? SCALE_DELTA
: 1/SCALE_DELTA;


scale *= scaleFactor;

img.setFitWidth(imgW * scale);
img.setFitHeight(imgH * scale);

// HERE i want to do something to keep my image where it was
scrollPane.setHvalue(0.5);

});

scrollPane.hvalueProperty().addListener((observable, oldvalue, newvalue) -> {
System.out.println(newvalue);
});

}
}









share|improve this question















I want to set Scrollpane H and V values but after inner content was resized.



Some context - I got following structure:



enter image description here



I attached .setOnScroll(...) (mouse) event to StackPane and when it happens, the inner content is resized (image) to some values (aka zoom). I want to add that StackPane min size is binded to scrollpane viewport size and Pane size binded to image size to keep everything working properly.



Up to this point everything works fine, when content changes size, scrollbars are updated and everything adjust as it supossed to be but I want to keep or rather set scrollbars positions to some values to keep zooming to previous middle point (something like zoom in photoshop). In this case I added this line in my callback to test it:



scrollPane.setHvalue(0.5);


The function itself works but it's overridden later (probably when layout recalculate) and this way it's always wrong. I tested values via event



scrollPane.hvalueProperty().addListener((observable, oldvalue, newvalue) -> {
System.out.println(newvalue);
});


and the output is kind of



0.5
0.45172283226050736
0.5
0.45196805476326296
0.5
0.4522703818369453

// or this when scaled down

0.5
0.5591296121097445
0.5
0.5608324439701174


That's why my conclusion is that I succeed setting 0.5 but later layout probably set different value for resized content becuase i changed size restrictions to Pane and StackPane.



My question then how should I do this?



Is there any onResize event to corrent or force my own value? or other way to schedule scroll event or my updates to be done when layout is recalculated?



// EDIT



I cleaned the code and here is example:



Main.java



package application;

import javafx.application.Application;
import javafx.stage.Stage;
import javafx.scene.Scene;
import javafx.scene.layout.BorderPane;
import javafx.fxml.FXMLLoader;


public class Main extends Application {
@Override
public void start(Stage primaryStage) {
try {
BorderPane root = (BorderPane)FXMLLoader.load(getClass().getResource("Sample.fxml"));
Scene scene = new Scene(root,800,600);
scene.getStylesheets().add(getClass().getResource("application.css").toExternalForm());
primaryStage.setScene(scene);
primaryStage.show();
} catch(Exception e) {
e.printStackTrace();
}
}

public static void main(String args) {
launch(args);
}
}


application.css is empty



/* JavaFX CSS - Leave this comment until you have at least create one rule which uses -fx-Property */


Sample.fxml (replace image url)



<?xml version="1.0" encoding="UTF-8"?>

<?import javafx.scene.control.ScrollPane?>
<?import javafx.scene.image.Image?>
<?import javafx.scene.image.ImageView?>
<?import javafx.scene.layout.BorderPane?>
<?import javafx.scene.layout.Pane?>
<?import javafx.scene.layout.StackPane?>

<BorderPane xmlns="http://javafx.com/javafx/8.0.171" xmlns:fx="http://javafx.com/fxml/1" fx:controller="application.SampleController">
<center>
<ScrollPane fx:id="scrollPane" prefHeight="600.0" prefWidth="800.0" BorderPane.alignment="CENTER">
<content>
<StackPane fx:id="stackPane">
<children>
<Pane fx:id="clipContainer">
<children>
<ImageView fx:id="img" pickOnBounds="true" preserveRatio="true">
<image>
<Image url="@../../../../Desktop/sample.jpg" />
</image>
</ImageView>
</children>
</Pane>
</children>
</StackPane>
</content>
</ScrollPane>
</center>
</BorderPane>


and SampleController.java



package application;

import java.net.URL;
import java.util.ResourceBundle;
import javafx.beans.binding.Bindings;
import javafx.fxml.FXML;
import javafx.fxml.Initializable;
import javafx.scene.control.ScrollPane;
import javafx.scene.image.ImageView;
import javafx.scene.layout.Pane;
import javafx.scene.layout.StackPane;


public class SampleController implements Initializable {


@FXML
private Pane clipContainer;

@FXML
private StackPane stackPane;

@FXML
private ImageView img;

@FXML
private ScrollPane scrollPane;

final double SCALE_DELTA = 1.1;

private double imgW;
private double imgH;
private double scale = 1;


@Override
public void initialize(URL location, ResourceBundle resources) {


clipContainer.setStyle("-fx-background-color: #999999");
stackPane.setStyle("-fx-background-color: #CCCCCC");

imgW = img.getImage().getWidth();
imgH = img.getImage().getHeight();

// bind max and min to adjust to new image bounds

stackPane.minWidthProperty().bind(Bindings.createDoubleBinding(() ->
scrollPane.getViewportBounds().getWidth(), scrollPane.viewportBoundsProperty()
));
stackPane.minHeightProperty().bind(Bindings.createDoubleBinding(() ->
scrollPane.getViewportBounds().getHeight(), scrollPane.viewportBoundsProperty()
));

clipContainer.maxWidthProperty().bind(Bindings.createDoubleBinding(() ->
img.getFitWidth(), img.fitWidthProperty()
));
clipContainer.maxHeightProperty().bind(Bindings.createDoubleBinding(() ->
img.getFitHeight(), img.fitHeightProperty()
));

// initial scale
img.setFitWidth(imgW * 1);
img.setFitHeight(imgH * 1);

// on mouse scroll
stackPane.setOnScroll(event -> {
event.consume();

if (event.getDeltaY() == 0) {
return;
}

double scaleFactor =
(event.getDeltaY() > 0)
? SCALE_DELTA
: 1/SCALE_DELTA;


scale *= scaleFactor;

img.setFitWidth(imgW * scale);
img.setFitHeight(imgH * scale);

// HERE i want to do something to keep my image where it was
scrollPane.setHvalue(0.5);

});

scrollPane.hvalueProperty().addListener((observable, oldvalue, newvalue) -> {
System.out.println(newvalue);
});

}
}






java javafx layout scrollbar






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 20 at 16:12

























asked Nov 20 at 4:16









Griva

693725




693725












  • Why did you even use a ScrollPane? Using ScrollPane to resize instead of scrolling up/down/left/right sounds like bad usage to me. setOnScroll() can be used on any node.
    – Jai
    Nov 20 at 5:47










  • @Jai I don't use ScrollPane to resize but mouse scroll event to resize inner image. Scrollpane is used to navigate zoomed image - it's exactly the same situation like zoomed canvas in photoshop and you navigate by scrollbars. The problem I got is that I want to keep image in position before zooming that's why i try to set scrollbar to some position but whatever I set it will be changed later, probably after layout realize that inner content changed size and scrollbar max, min and value have to be updated I belive.
    – Griva
    Nov 20 at 6:14










  • Please provide a Minimal, Complete, and Verifiable example that demonstrates the problem.
    – kleopatra
    Nov 20 at 9:15










  • @kleopatra Sorry I did not do it before, code was total mess and I had to clear it much. I thought it's rather easy problem and will be easy task to fix it - my bad. I edited question and added code with example so check it if you can, thank you.
    – Griva
    Nov 20 at 16:18


















  • Why did you even use a ScrollPane? Using ScrollPane to resize instead of scrolling up/down/left/right sounds like bad usage to me. setOnScroll() can be used on any node.
    – Jai
    Nov 20 at 5:47










  • @Jai I don't use ScrollPane to resize but mouse scroll event to resize inner image. Scrollpane is used to navigate zoomed image - it's exactly the same situation like zoomed canvas in photoshop and you navigate by scrollbars. The problem I got is that I want to keep image in position before zooming that's why i try to set scrollbar to some position but whatever I set it will be changed later, probably after layout realize that inner content changed size and scrollbar max, min and value have to be updated I belive.
    – Griva
    Nov 20 at 6:14










  • Please provide a Minimal, Complete, and Verifiable example that demonstrates the problem.
    – kleopatra
    Nov 20 at 9:15










  • @kleopatra Sorry I did not do it before, code was total mess and I had to clear it much. I thought it's rather easy problem and will be easy task to fix it - my bad. I edited question and added code with example so check it if you can, thank you.
    – Griva
    Nov 20 at 16:18
















Why did you even use a ScrollPane? Using ScrollPane to resize instead of scrolling up/down/left/right sounds like bad usage to me. setOnScroll() can be used on any node.
– Jai
Nov 20 at 5:47




Why did you even use a ScrollPane? Using ScrollPane to resize instead of scrolling up/down/left/right sounds like bad usage to me. setOnScroll() can be used on any node.
– Jai
Nov 20 at 5:47












@Jai I don't use ScrollPane to resize but mouse scroll event to resize inner image. Scrollpane is used to navigate zoomed image - it's exactly the same situation like zoomed canvas in photoshop and you navigate by scrollbars. The problem I got is that I want to keep image in position before zooming that's why i try to set scrollbar to some position but whatever I set it will be changed later, probably after layout realize that inner content changed size and scrollbar max, min and value have to be updated I belive.
– Griva
Nov 20 at 6:14




@Jai I don't use ScrollPane to resize but mouse scroll event to resize inner image. Scrollpane is used to navigate zoomed image - it's exactly the same situation like zoomed canvas in photoshop and you navigate by scrollbars. The problem I got is that I want to keep image in position before zooming that's why i try to set scrollbar to some position but whatever I set it will be changed later, probably after layout realize that inner content changed size and scrollbar max, min and value have to be updated I belive.
– Griva
Nov 20 at 6:14












Please provide a Minimal, Complete, and Verifiable example that demonstrates the problem.
– kleopatra
Nov 20 at 9:15




Please provide a Minimal, Complete, and Verifiable example that demonstrates the problem.
– kleopatra
Nov 20 at 9:15












@kleopatra Sorry I did not do it before, code was total mess and I had to clear it much. I thought it's rather easy problem and will be easy task to fix it - my bad. I edited question and added code with example so check it if you can, thank you.
– Griva
Nov 20 at 16:18




@kleopatra Sorry I did not do it before, code was total mess and I had to clear it much. I thought it's rather easy problem and will be easy task to fix it - my bad. I edited question and added code with example so check it if you can, thank you.
– Griva
Nov 20 at 16:18












1 Answer
1






active

oldest

votes

















up vote
0
down vote













While I am unfamiliar with JavaFX, one would think that you could "force your own value" by resizing the scrollbar bounds whenever it changes. So, execute scrollPane.setHvalue(0.5) within the ChangeListener callback instead:



scrollPane.hvalueProperty().addListener((DoubleProperty observable, double oldvalue, double newvalue) -> {

if (newvalue != 0.5) {
scrollPane.setHvalue(0.5);
}

});





share|improve this answer























  • I am not sure if this is good idea. Do not this create some problems? Calling setHValue will trigger another event + description of callback properties says In general is is considered bad practice to modify the observed value in this method..
    – Griva
    Nov 20 at 5:19










  • @Griva Which is why I bolded and quoted you on forcing your own value. I agree that it is not considered good practice to force anything when the API wants you to do it another way, but for your purposes, "forcing" is the only solution. The JavaFX UI system attempts to arrange the UI one way, and you are trying to hack that arrangement and manually create your own. Your only option would be to implement your own ScrollPane, down to the classes which override your changes.
    – Max K
    Nov 20 at 5:21












  • @Griva see my edits to make sure that setHvaluedoes not loop on itself.
    – Max K
    Nov 20 at 5:21











Your Answer






StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");

StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});

function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});


}
});














draft saved

draft discarded


















StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53386135%2fkeep-or-set-javafx-scrollpane-position-when-content-resized%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown

























1 Answer
1






active

oldest

votes








1 Answer
1






active

oldest

votes









active

oldest

votes






active

oldest

votes








up vote
0
down vote













While I am unfamiliar with JavaFX, one would think that you could "force your own value" by resizing the scrollbar bounds whenever it changes. So, execute scrollPane.setHvalue(0.5) within the ChangeListener callback instead:



scrollPane.hvalueProperty().addListener((DoubleProperty observable, double oldvalue, double newvalue) -> {

if (newvalue != 0.5) {
scrollPane.setHvalue(0.5);
}

});





share|improve this answer























  • I am not sure if this is good idea. Do not this create some problems? Calling setHValue will trigger another event + description of callback properties says In general is is considered bad practice to modify the observed value in this method..
    – Griva
    Nov 20 at 5:19










  • @Griva Which is why I bolded and quoted you on forcing your own value. I agree that it is not considered good practice to force anything when the API wants you to do it another way, but for your purposes, "forcing" is the only solution. The JavaFX UI system attempts to arrange the UI one way, and you are trying to hack that arrangement and manually create your own. Your only option would be to implement your own ScrollPane, down to the classes which override your changes.
    – Max K
    Nov 20 at 5:21












  • @Griva see my edits to make sure that setHvaluedoes not loop on itself.
    – Max K
    Nov 20 at 5:21















up vote
0
down vote













While I am unfamiliar with JavaFX, one would think that you could "force your own value" by resizing the scrollbar bounds whenever it changes. So, execute scrollPane.setHvalue(0.5) within the ChangeListener callback instead:



scrollPane.hvalueProperty().addListener((DoubleProperty observable, double oldvalue, double newvalue) -> {

if (newvalue != 0.5) {
scrollPane.setHvalue(0.5);
}

});





share|improve this answer























  • I am not sure if this is good idea. Do not this create some problems? Calling setHValue will trigger another event + description of callback properties says In general is is considered bad practice to modify the observed value in this method..
    – Griva
    Nov 20 at 5:19










  • @Griva Which is why I bolded and quoted you on forcing your own value. I agree that it is not considered good practice to force anything when the API wants you to do it another way, but for your purposes, "forcing" is the only solution. The JavaFX UI system attempts to arrange the UI one way, and you are trying to hack that arrangement and manually create your own. Your only option would be to implement your own ScrollPane, down to the classes which override your changes.
    – Max K
    Nov 20 at 5:21












  • @Griva see my edits to make sure that setHvaluedoes not loop on itself.
    – Max K
    Nov 20 at 5:21













up vote
0
down vote










up vote
0
down vote









While I am unfamiliar with JavaFX, one would think that you could "force your own value" by resizing the scrollbar bounds whenever it changes. So, execute scrollPane.setHvalue(0.5) within the ChangeListener callback instead:



scrollPane.hvalueProperty().addListener((DoubleProperty observable, double oldvalue, double newvalue) -> {

if (newvalue != 0.5) {
scrollPane.setHvalue(0.5);
}

});





share|improve this answer














While I am unfamiliar with JavaFX, one would think that you could "force your own value" by resizing the scrollbar bounds whenever it changes. So, execute scrollPane.setHvalue(0.5) within the ChangeListener callback instead:



scrollPane.hvalueProperty().addListener((DoubleProperty observable, double oldvalue, double newvalue) -> {

if (newvalue != 0.5) {
scrollPane.setHvalue(0.5);
}

});






share|improve this answer














share|improve this answer



share|improve this answer








edited Nov 20 at 5:23

























answered Nov 20 at 4:58









Max K

346115




346115












  • I am not sure if this is good idea. Do not this create some problems? Calling setHValue will trigger another event + description of callback properties says In general is is considered bad practice to modify the observed value in this method..
    – Griva
    Nov 20 at 5:19










  • @Griva Which is why I bolded and quoted you on forcing your own value. I agree that it is not considered good practice to force anything when the API wants you to do it another way, but for your purposes, "forcing" is the only solution. The JavaFX UI system attempts to arrange the UI one way, and you are trying to hack that arrangement and manually create your own. Your only option would be to implement your own ScrollPane, down to the classes which override your changes.
    – Max K
    Nov 20 at 5:21












  • @Griva see my edits to make sure that setHvaluedoes not loop on itself.
    – Max K
    Nov 20 at 5:21


















  • I am not sure if this is good idea. Do not this create some problems? Calling setHValue will trigger another event + description of callback properties says In general is is considered bad practice to modify the observed value in this method..
    – Griva
    Nov 20 at 5:19










  • @Griva Which is why I bolded and quoted you on forcing your own value. I agree that it is not considered good practice to force anything when the API wants you to do it another way, but for your purposes, "forcing" is the only solution. The JavaFX UI system attempts to arrange the UI one way, and you are trying to hack that arrangement and manually create your own. Your only option would be to implement your own ScrollPane, down to the classes which override your changes.
    – Max K
    Nov 20 at 5:21












  • @Griva see my edits to make sure that setHvaluedoes not loop on itself.
    – Max K
    Nov 20 at 5:21
















I am not sure if this is good idea. Do not this create some problems? Calling setHValue will trigger another event + description of callback properties says In general is is considered bad practice to modify the observed value in this method..
– Griva
Nov 20 at 5:19




I am not sure if this is good idea. Do not this create some problems? Calling setHValue will trigger another event + description of callback properties says In general is is considered bad practice to modify the observed value in this method..
– Griva
Nov 20 at 5:19












@Griva Which is why I bolded and quoted you on forcing your own value. I agree that it is not considered good practice to force anything when the API wants you to do it another way, but for your purposes, "forcing" is the only solution. The JavaFX UI system attempts to arrange the UI one way, and you are trying to hack that arrangement and manually create your own. Your only option would be to implement your own ScrollPane, down to the classes which override your changes.
– Max K
Nov 20 at 5:21






@Griva Which is why I bolded and quoted you on forcing your own value. I agree that it is not considered good practice to force anything when the API wants you to do it another way, but for your purposes, "forcing" is the only solution. The JavaFX UI system attempts to arrange the UI one way, and you are trying to hack that arrangement and manually create your own. Your only option would be to implement your own ScrollPane, down to the classes which override your changes.
– Max K
Nov 20 at 5:21














@Griva see my edits to make sure that setHvaluedoes not loop on itself.
– Max K
Nov 20 at 5:21




@Griva see my edits to make sure that setHvaluedoes not loop on itself.
– Max K
Nov 20 at 5:21


















draft saved

draft discarded




















































Thanks for contributing an answer to Stack Overflow!


  • Please be sure to answer the question. Provide details and share your research!

But avoid



  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.


To learn more, see our tips on writing great answers.





Some of your past answers have not been well-received, and you're in danger of being blocked from answering.


Please pay close attention to the following guidance:


  • Please be sure to answer the question. Provide details and share your research!

But avoid



  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.


To learn more, see our tips on writing great answers.




draft saved


draft discarded














StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53386135%2fkeep-or-set-javafx-scrollpane-position-when-content-resized%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







Popular posts from this blog

Wiesbaden

Marschland

Dieringhausen