JavaFx8 VBox Center Image


Idol

I have a JavaFx VBox in a ScrollPane:

VBox container = new VBox();
container.setAlignment(Pos.CENTER);

...

scrollPane.setContent(container);
scrollPane.setFitToWidth(true);
scrollPane.setHbarPolicy(ScrollBarPolicy.NEVER);
scrollPane.setVbarPolicy(ScrollBarPolicy.NEVER);
scrollPane.setMinWidth(150);
scrollPane.setPannable(true);

The size of this VBox never changes, inside i have some labels, one label has a user image like the next image (A)

The image is resized to a certain height, but I don't know the size of the image, so if the width of the image is larger than the width of the VBox, this will happen (part of the image is hidden) (B)

But I don't want this, I want an image like below: (Hide the sides of the image if the image width is greater than the VBox width) [C)

http://i.stack.imgur.com/B3DOK.png

what should I do?

I'm trying to clip a rectangle in which I want to display the center of the image, but the same happens.

imageView.setClip(new Rectangle(centerX - recSize, centerY - recSize, recSize*2, recSize*2));

--------------- with short films ----------------

red = original image

blue = visible part of the image

http://i.stack.imgur.com/mYbyF.png

good (D)

Bad: (E) (Due to the image labels not being properly centered.)

Sorry for the link, I can't place the image directly

Jewel Sea

untie

Set the viewport on the image instead of the clip.

imageView.setViewport(
     new Rectangle2D(500, 320, 420, 300)
);

sample

Here is a sample. It won't do exactly what you want because I can't quite understand what you're trying to do, even with the linked image in your question. But I think it should give you enough background to learn to do what you want.

The example creates an image view as a graph in a scroll pane. The image view applies the viewport to the image and scales the viewport with the preserved ratio. This allows zoomed portions of larger images to be displayed. Kind of like a thumbnail clip (click a thumbnail to show the full image).

Family

import javafx.application.Application;
import javafx.geometry.*;
import javafx.scene.*;
import javafx.scene.control.*;
import javafx.scene.effect.*;
import javafx.scene.image.*;
import javafx.scene.layout.VBox;
import javafx.scene.paint.Color;
import javafx.scene.text.*;
import javafx.stage.*;

// display a captioned image in a viewport.
// click the image to get an expanded view.
public class LabelWithImage extends Application {
    private static final double IMAGE_WIDTH = 150;

    @Override
    public void start(Stage stage) {
        Image image = new Image(IMAGE_LOC);
        ImageView imageView = new ImageView(
                image
        );
        imageView.setViewport(
                new Rectangle2D(500, 320, 420, 300)
        );
        imageView.setFitWidth(IMAGE_WIDTH);
        imageView.setPreserveRatio(true);

        Label labeledImage = createCaptionedImage(
                imageView,
                "Village Home"
        );

        addGlowOnMouseOver(labeledImage);
        labeledImage.setOnMouseClicked(event -> {
            displayFullImage(stage, image);
        });

        VBox vbox = new VBox( // vbox just there to mimic question askers structure.
                labeledImage
        );
        vbox.setPadding(new Insets(10));

        ScrollPane scrollPane = makeScrollable(vbox);

        Scene scene = new Scene(
                scrollPane
        );

        stage.setScene(scene);
        stage.show();

        stage.setMaxWidth(stage.getWidth());
        stage.setMaxHeight(stage.getHeight());
    }

    private void displayFullImage(Stage stage, Image image) {
        Stage displayStage = new Stage();

        displayStage.initStyle(StageStyle.UTILITY);
        displayStage.initModality(Modality.APPLICATION_MODAL);
        displayStage.initOwner(stage);
        displayStage.setScene(
                new Scene(
                        new Group(
                                new ImageView(
                                        image
                                )
                        )
                )
        );
        displayStage.show();
    }

    private void addGlowOnMouseOver(Node node) {
        Glow glow = new Glow();
        DropShadow shadow = new DropShadow(20, Color.GOLD);
        glow.setInput(shadow);

        node.setOnMousePressed(event -> node.setEffect(null));
        node.setOnMouseEntered(event -> node.setEffect(glow));
        node.setOnMouseExited(event -> node.setEffect(null));
    }

    private ScrollPane makeScrollable(Node node) {
        ScrollPane scrollPane = new ScrollPane(node);

        scrollPane.setHbarPolicy(ScrollPane.ScrollBarPolicy.NEVER);
        scrollPane.setVbarPolicy(ScrollPane.ScrollBarPolicy.NEVER);
        scrollPane.setPannable(true);

        return scrollPane;
    }

    private Label createCaptionedImage(ImageView imageView, String caption) {
        Label labeledImage = new Label(caption);

        labeledImage.setFont(Font.font("Athelas", FontPosture.ITALIC, 20));
        labeledImage.setStyle("-fx-background-color: cornsilk");
        labeledImage.setPadding(new Insets(0, 0, 5, 0));
        labeledImage.setGraphic(
                imageView
        );
        labeledImage.setContentDisplay(ContentDisplay.TOP);

        return labeledImage;
    }

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

    private static final String IMAGE_LOC =
            "http://www.imgion.com/images/01/beautiful-village-home.jpg";
            // image courtesy of http://www.imgion.com which provides
            // "free images on large topics to share with your friends and on your blogs."
}

Related


JavaFX: Remove child from Vbox

Stefania : So, I'm new to JavaFX, I'm trying to design an application for a school, and I've been reading a lot of articles about it, but I can't seem to figure out how to do it for my own situation. this is a photo So the big screen is my main screen and the

Is there a way to delete everything in a vBox in JavaFx?

Yin I am trying to do a Javafx project. I have some doubts about this. I put a text list (label) inside the vBox. Is there a way to delete all the text in the vBox? For example, when I click the delete button, everything inside the vBox disappears. Is it possi

Center vBox in BorderPane in JavaFx

Bernd I want to center the hBox with 3 buttons in the center of the panel. This is what I currently have, but I don't know how to do the centering. If anyone can help me. public clas MyBoarderPane extends BoarderPane{ vBox = new VBox(); vBox.setSpacing(10); v

Is there a way to delete everything in a vBox in JavaFx?

Yin I am trying to do a Javafx project. I have some doubts about this. I put a text list (label) inside the vBox. Is there a way to delete all the text in the vBox? For example, when I click the delete button, everything inside the vBox disappears. Is it possi

JavaFX center button in VBox

may I can't center a Button in a VBox in Java code! I used Scene Builder to create a SplitPane with the button on the left AnchorPane in the center of the VBox. I want to recreate a Button in a right AnchorPane in Java code instead of a VBox in FXML. However,

Add border around VBox in javafx?

Mikael Is it possible to add a border line around the entire contents of a VBox in Javafx? Means I have added a lot of components in tis VBox but want to have border around it? So if I have a css file like this: #vbox_style { -fx-border-color: black; -

JavaFX Center VBox in GridPane

Lin Henry Thank you very much for your help with my question. I am using JavaFX and NetBeans IDE. I'm making a very simple launch window for a desktop client. The window currently looks like this image. Current appearance: Desired look (edited with paint): In

JAVAFX how to center Vbox in right part of BorderPane

Christianity I'm having trouble placing a VBox with 3 buttons in the center of the right part of the BorderPane. Is there a way to do this in FXML or CSS? renew: Here is the code in FXML <right> <VBox id="otherButtons_vbox"> <Button text="

Is there a way to delete everything in a vBox in JavaFx?

Yin I am trying to do a Javafx project. I have some doubts about this. I put a text list (label) inside the vBox. Is there a way to delete all the text in the vBox? For example, when I click the delete button, everything inside the vBox disappears. Is it possi

Center vBox in BorderPane in JavaFx

Bernd I want to center the hBox with 3 buttons in the center of the panel. This is what I currently have, but I don't know how to do the centering. If anyone can help me. public clas MyBoarderPane extends BoarderPane{ vBox = new VBox(); vBox.setSpacing(10); v

JavaFX center button in VBox

may I can't center a Button in a VBox in Java code! I used Scene Builder to create a SplitPane with the button on the left AnchorPane in the center of the VBox. I want to recreate a Button in a right AnchorPane in Java code instead of a VBox in FXML. However,

How to center an image in javafx

Osama How can I move the Facebook image so that it is centered perpendicular to the text? Code : GridPane footerPane = new GridPane(); double size = 15; TextFlow textFlow = new TextFlow(); Text text1 = new Text("Get prayer time notifications and daily ha

How to center an image in javafx

Osama How can I move the Facebook image so that it is centered perpendicular to the text? Code : GridPane footerPane = new GridPane(); double size = 15; TextFlow textFlow = new TextFlow(); Text text1 = new Text("Get prayer time notifications and daily ha

Center the image in the center of the card

Zheng Stanley I am trying to center an image in the middle of the card. I've tried applying other styles (like traditional HTML markup) to center, but so far no luck. <article class="photo"> <img src="https://mirror-api-playground.appspot.com/links/filoli-s

Width is not set for javafx VBox project?

JanithOCoder Here is the code I wrote, but the width of the VBox item is not set. I made all panels the same size. I need some help. Thanks in advance! private void initComponents() { this.setPadding(new Insets(0,70,0,70)); this.setSpacing(10); re

Change background color of image view in JavaFX8

divination I'm trying to change the background color ImageViewusing CSS bt can't change... How to set background color ImageViewin Javafx8 ? Can anyone help me? Here is my screenshot: I want to replace the grey background of an image with black . Uchinami Hmm,

Is there a way to delete everything in a vBox in JavaFx?

Yin I am trying to do a Javafx project. I have some doubts about this. I put a text list (label) inside the vBox. Is there a way to delete all the text in the vBox? For example, when I click the delete button, everything inside the vBox disappears. Is it possi

JavaFX 8: Set style for inner VBox does not apply

dragon I was chatting and decided to stick to the following structure: All chat messages consist of an outer VBox and an inner VBox ( chatBlock). The inner VBox contains panes with tab blocks. One pane + tab block for nicknames and another for messages. So I w

JavaFx8 VBox Center Image

Idol I have a JavaFx VBox in a ScrollPane: VBox container = new VBox(); container.setAlignment(Pos.CENTER); ... scrollPane.setContent(container); scrollPane.setFitToWidth(true); scrollPane.setHbarPolicy(ScrollBarPolicy.NEVER); scrollPane.setVbarPolicy(Scroll

Center the image next to the image

EDA I want to center the image next to the image like this: My current code is: CSS .slider-btns img { display: block; margin-left: auto; margin-right: auto; } html <div class="slider-btns"> <img src="assets/img/arrow-left.png" style="width:50px" dat

JavaFX: Remove child from Vbox

Stefania : So, I'm new to JavaFX, I'm trying to design an application for a school, and I've been reading a lot of articles about it, but I can't seem to figure out how to do it for my own situation. this is a photo So the big screen is my main screen and the

Center object in HBox and VBox (JavaFx)

User 15367506 I am making a simple GUI for one of my tasks. I've created a bare-bones interface, but I'm currently having some issues. The program shows everything fine, it's just that I want to center the object for a neater look. What I tried was putting the

Is there a way to delete everything in a vBox in JavaFx?

Yin I am trying to do a Javafx project. I have some doubts about this. I put a text list (label) inside the vBox. Is there a way to delete all the text in the vBox? For example, when I click the delete button, everything inside the vBox disappears. Is it possi

Center vBox in BorderPane in JavaFx

Bernd I want to center the hBox with 3 buttons in the center of the panel. This is what I currently have, but I don't know how to do the centering. If anyone can help me. public clas MyBoarderPane extends BoarderPane{ vBox = new VBox(); vBox.setSpacing(10); v

JavaFX center button in VBox

may I can't center a Button in a VBox in Java code! I used Scene Builder to create a SplitPane with the button on the left AnchorPane in the center of the VBox. I want to recreate a Button in a right AnchorPane in Java code instead of a VBox in FXML. However,

Center VBox in pane

Debrau I am new to javaFx and using fxml to create templates. The one I can't manage to center Vboxis Panetoo big, but I'm sure that must be fairly simple. I also want the border around it VBox. Can someone help? Here is what I have: <TitledPane text="Title" c

JavaFX 8: Set style for inner VBox does not apply

dragon I was chatting and decided to stick to the following structure: All chat messages consist of an outer VBox and an inner VBox ( chatBlock). The inner VBox contains panes with tab blocks. One pane + tab block for nicknames and another for messages. So I w

Center the image next to the image

EDA I want to center the image next to the image like this: My current code is: CSS .slider-btns img { display: block; margin-left: auto; margin-right: auto; } html <div class="slider-btns"> <img src="assets/img/arrow-left.png" style="width:50px" dat

JavaFx8 VBox Center Image

Idol I have a JavaFx VBox in a ScrollPane: VBox container = new VBox(); container.setAlignment(Pos.CENTER); ... scrollPane.setContent(container); scrollPane.setFitToWidth(true); scrollPane.setHbarPolicy(ScrollBarPolicy.NEVER); scrollPane.setVbarPolicy(Scroll