Why won’t my JavaFX ImageView show any images?

I am working on a JavaFX gui, but for some reason, none of my ImageViews seem to be showing any images. I tried setting images in SceneBuilder, where they will only show in SceneBuilder and not when the actual program is run. I also tried setting writable images, which also did not show in the program. I am certain that the image paths are correct and I tried printing the pixel ArrayList, which seems to be correct. Why wouldn’t the ImageView work?

My image writer:

public ImageProcessor() {
        width = 1;
        height = 1;
        image = new BufferedImage(1, 1, BufferedImage.TYPE_INT_ARGB);
        monochromeImage = new BufferedImage(1, 1, BufferedImage.TYPE_INT_ARGB);
        pixels = new ArrayList<>();
    }

    // process image given its path
    public void process(String path) {
        try {
            this.image = ImageIO.read(new File(path));
            width = this.image.getWidth();
            height = this.image.getHeight();
            this.monochromeImage = new BufferedImage(this.width, this.height, BufferedImage.TYPE_BYTE_GRAY);
            pixels.clear();
            for (int y = 0; y < this.monochromeImage.getHeight(); ++y) {
                pixels.add(new ArrayList<>());
                for (int x = 0; x < this.width; ++x) {
                    int rgb = this.image.getRGB(x, y);
                    int grayValue = (int) (0.299 * ((rgb >> 16) & 0xFF) + 0.587 * ((rgb >> 8) & 0xFF) + 0.114 * (rgb & 0xFF));
                    this.monochromeImage.setRGB(x, y, (grayValue << 16) | (grayValue << 8) | grayValue);
                    pixels.get(y).add(grayValue);
                }
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    // write and return a WritableImage
    public WritableImage writeImage() {
        WritableImage writableImage = new WritableImage(this.width, this.height);
        PixelWriter pixelWriter = writableImage.getPixelWriter();

        for (int y = 0; y < this.height; ++y) {
            for (int x = 0; x < this.width; ++x) {
                int pixelValue = this.pixels.get(y).get(x);
                pixelWriter.setArgb(x, y, pixelValue);
            }
        }
        return writableImage;
    }

My controller:


public class Controller {
    // normal attributes
    private ImageProcessor imageProcessor;
    private WritableImage image;

    // FXML attributes
    @FXML private Button openImageButton;
    @FXML private ImageView imageView;

    // constructor
    public Controller() {
        imageProcessor = new ImageProcessor();
        image = new WritableImage(1000, 1000);
    }

    // initialize after FXML fields are injected
    @FXML public void initialize() {
        imageView.setImage(image);
    }

    // user chooses an image when the openImageButton is pressed
    @FXML void loadImage() {

        FileChooser fileChooser = new FileChooser();
        FileChooser.ExtensionFilter extFilter = new FileChooser.ExtensionFilter("Images", "*.png", "*.jpg", "*.jpeg", "*.bmp");
        fileChooser.getExtensionFilters().add(extFilter);
        fileChooser.setTitle("Open Image File");

        String userHome = System.getProperty("user.home");
        fileChooser.setInitialDirectory(new File(userHome));

        Stage stage = (Stage) this.openImageButton.getScene().getWindow();
        File selectedFile = fileChooser.showOpenDialog(stage);

        if (selectedFile != null) {
            String path = selectedFile.getAbsolutePath();
            this.imageProcessor.process(path);
            this.imageProcessor.printPixels();
            this.image = this.imageProcessor.writeImage();
            this.imageView.setImage(this.image);
        }

    }


}

Main:

public class Main extends javafx.application.Application {
    @Override
    public void start(Stage stage) throws IOException {
        FXMLLoader fxmlLoader = new FXMLLoader(Main.class.getResource("view.fxml"));
        Scene scene = new Scene(fxmlLoader.load(), 1500, 900);

        Controller controller = fxmlLoader.getController();

        stage.setTitle("Dose Mapper");
        stage.setScene(scene);
        stage.show();
    }

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

ImageView’s FXML line:


<ImageView fx:id="imageView" fitHeight="861.0" fitWidth="909.0" layoutX="506.0" layoutY="113.0" pickOnBounds="true" preserveRatio="true" />

I tried setting an image path using SceneBuilder and creating objects in the program. After debugging, I found that the WritableImage and pixels should be correct. I want to set the ImageView’s image to the WritableImage returned from the function call, but nothing I’ve thrown at it is seeming to cooperate.

New contributor

tsa7 is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật