Reputation:
I am looking to resize the window to make fit an ImageView that changes size when a button is pressed. The ImageView is contained within an AnchorPane. I can't seem to find any resources that explain how to do this nor have I been able to find anything within SceneBuilder. Thanks!
<AnchorPane prefHeight="400" prefWidth="600" style="-fx-background-color: #282828;" xmlns="http://javafx.com/javafx/8.0.171" xmlns:fx="http://javafx.com/fxml/1" fx:controller="application.Controller">
<children>
<HBox maxHeight="-Infinity" maxWidth="-Infinity" AnchorPane.bottomAnchor="0.0" AnchorPane.leftAnchor="0.0" AnchorPane.topAnchor="0.0">
<children>
<VBox prefHeight="200.0" prefWidth="100.0" style="-fx-background-color: #535353;">
<children>
<Pane prefHeight="50.0" prefWidth="100.0" />
<Button fx:id="topButton" mnemonicParsing="false" onAction="#handleTopButton" prefHeight="25.0" prefWidth="100.0" text="Top" />
<Button fx:id="sideButton" mnemonicParsing="false" onAction="#handleSideButton" prefHeight="25.0" prefWidth="100.0" text="Side" />
<Button fx:id="frontButton" mnemonicParsing="false" onAction="#handleFrontButton" prefHeight="25.0" prefWidth="100.0" text="Front" />
<Pane prefHeight="50.0" prefWidth="100.0" />
<Label fx:id="scaleLabel" prefHeight="17.0" prefWidth="103.0" text="Scale" textAlignment="CENTER" textFill="WHITE" />
<Slider fx:id="scale" max="3.0" min="1.0" />
<Label fx:id="sliceLabel" text="Slice" textAlignment="CENTER" textFill="WHITE" />
<Slider fx:id="slice" disable="true" />
<Pane prefHeight="50.0" prefWidth="100.0" />
<Button fx:id="mipButton" disable="true" mnemonicParsing="false" onAction="#handleMipButton" prefHeight="25.0" prefWidth="100.0" text="MIP" />
<Button mnemonicParsing="false" prefHeight="25.0" prefWidth="100.0" text="Histogram" />
<Button fx:id="exitButton" mnemonicParsing="false" onAction="#handleExitButton" prefHeight="25.0" prefWidth="100.0" text="Exit" />
</children>
</VBox>
<ImageView fx:id="imageView"/>
</children>
</HBox>
</children>
Upvotes: 0
Views: 319
Reputation: 20913
Node
, e.g. BorderPaneHere is a very minimal implementation of the above.
NOTE: Replace url with the actual URL of your image in the Image
constructor in the below code.
import javafx.application.Application;
import javafx.geometry.Pos;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.image.Image;
import javafx.scene.image.ImageView;
import javafx.scene.layout.BorderPane;
import javafx.scene.layout.HBox;
import javafx.stage.Stage;
public class ImgVwTst extends Application {
@Override
public void start(Stage primaryStage) throws Exception {
BorderPane root = new BorderPane();
Image img = new Image("url");
ImageView imgVw = new ImageView(img);
root.setCenter(imgVw);
HBox hBox = new HBox();
hBox.setAlignment(Pos.CENTER);
Button nextImageButton = new Button("next");
nextImageButton.setOnAction(e -> {Image img2 = new Image("url");
imgVw.setImage(img2);
primaryStage.sizeToScene();});
hBox.getChildren().add(nextImageButton);
root.setBottom(hBox);
Scene scene = new Scene(root);
primaryStage.setScene(scene);
primaryStage.show();
}
public static void main(String[] args) {
launch(args);
}
}
Upvotes: 1