Over a million developers have joined DZone.
{{announcement.body}}
{{announcement.title}}

Image viewer in JavaFX

DZone's Guide to

Image viewer in JavaFX

·
Free Resource
Root 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()); }
Topics:

Opinions expressed by DZone contributors are their own.

THE DZONE NEWSLETTER

Dev Resources & Solutions Straight to Your Inbox

Thanks for subscribing!

Awesome! Check your inbox to verify your email so you can start receiving the latest in tech news and resources.

X

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}