Image viewer in JavaFX
Join the DZone community and get the full member experience.
Join For FreeRoot container is a stack pane since it will attempt to resize its children nodes. JavaFX controls such as ImageView, TextArea, ListView, and Buttons, are added to the GridPane container.
The grid pane container is then added to the root node.
To display the images as a thumbnail, create a class that will extend from ListView and implement a custom cell factory as follows:
private void setCustomCellFactory(){ setCellFactory(new Callback<ListView<ImageView>, ListCell<ImageView>>(){ @Override public ListCell<ImageView> call(ListView<ImageView> arg0) { ListCell<ImageView> cell = new ListCell<ImageView>(){ @Override protected void updateItem(ImageView imageView, boolean flag) { super.updateItem(imageView, flag); if(imageView != null){ imageView.setFitHeight(80); imageView.setFitWidth(100); } setGraphic(imageView); } }; cell.addEventHandler(javafx.scene.input.MouseEvent.MOUSE_CLICKED, new EventHandler<javafx.scene.input.MouseEvent>() { public void handle( javafx.scene.input.MouseEvent event) { @SuppressWarnings("unchecked") ListCell<ImageView> view = (ListCell<ImageView>) event .getSource(); int index = view.getIndex(); ImageView selectedImageView = observableImageItems .get(index); if (selectedImageView != null && selectedImageView.getImage() != null) { controller .getScene() .getCurrentImageView() .setImage( selectedImageView .getImage()); controller.setCurrentIndex(index); } } }); return cell; } }); }
public class PhotoBrowserFxScene extends Scene { private PhotoBrowserFxController controller; private GridPane gridPane; private ImageView currentImageView; private TextArea commentTxtArea; private Button deleteBtn; private Button takePhotoBtn; private Arc leftArrow; private Arc rightArrow; private ThumbnailsImageViewList thumbnails; public PhotoBrowserFxScene(PhotoBrowserFxController controller){ this(controller.getRootContainer(),800,700); this.controller = controller; initialize(); addActions(); } public PhotoBrowserFxScene(Parent root,double width, double height) { super(root,width,height); } private void initialize(){ getGridPane().add(getCurrentImageView(), 0, 0); GridPane.setHalignment(getCurrentImageView(), HPos.CENTER); GridPane.setValignment(getCurrentImageView(), VPos.CENTER); GridPane.setMargin(getCurrentImageView(), new Insets(5)); getGridPane().add(getCommentsTxtArea(), 1, 0); GridPane.setHalignment(getCommentsTxtArea(), HPos.CENTER); GridPane.setValignment(getCommentsTxtArea(), VPos.TOP); GridPane.setMargin(getCommentsTxtArea(), new Insets(20,20,30,20)); getGridPane().add(getDeleteBtn(), 1, 0); GridPane.setHalignment(getDeleteBtn(), HPos.LEFT); GridPane.setValignment(getDeleteBtn(), VPos.BOTTOM); GridPane.setMargin(getDeleteBtn(), new Insets(0,0,0,10)); getGridPane().add(getTakePhotoBtn(), 1, 0); GridPane.setHalignment(getTakePhotoBtn(), HPos.LEFT); GridPane.setValignment(getTakePhotoBtn(), VPos.BOTTOM); GridPane.setMargin(getTakePhotoBtn(), new Insets(0, 0, 0, 90)); getGridPane().add(getLeftArrow(),0,1,2,1); GridPane.setHalignment(getLeftArrow(), HPos.LEFT); GridPane.setValignment(getLeftArrow(), VPos.CENTER); GridPane.setMargin(getLeftArrow(), new Insets(10,10,10,10)); getGridPane().add(getRightArrow(),0,1,2,1); GridPane.setHalignment(getRightArrow(), HPos.RIGHT); GridPane.setValignment(getRightArrow(), VPos.CENTER); GridPane.setMargin(getRightArrow(), new Insets(10,10,10,10)); getGridPane().add(getThumbnailsImages(),0,1,2,1); GridPane.setHalignment(getThumbnailsImages(), HPos.CENTER); GridPane.setValignment(getThumbnailsImages(), VPos.CENTER); GridPane.setMargin(getThumbnailsImages(), new Insets(30,30,30,30)); controller.getRootContainer().getChildren().addAll(getGridPane()); }
JavaFX
Opinions expressed by DZone contributors are their own.
Trending
-
Getting Started With the YugabyteDB Managed REST API
-
How To Use Pandas and Matplotlib To Perform EDA In Python
-
Transactional Outbox Patterns Step by Step With Spring and Kotlin
-
Reactive Programming
Comments