0

I'm trying to make a program with some filters of image by using JavaFx, so I need two button at least, one is a file chooser to open an image, and another one will be a choice box that allows to choose a filter.

My problem is how could a choice box get the path name or a file object from the file chooser.

here is my program unfinished :

import javafx.application.Application;
import javafx.collections.FXCollections;
import javafx.scene.control.Button;
import javafx.scene.control.ChoiceBox;
import javafx.scene.layout.*;
import javafx.stage.FileChooser;
import javafx.stage.Stage;
import javafx.scene.Scene;
import javafx.scene.image.Image;
import javafx.scene.image.ImageView;
import javax.imageio.ImageIO;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;

public class Filter extends Application{

    public void start(final Stage stage) {
        stage.setTitle("FilterShop");

        final FileChooser fileChooser = new FileChooser();
        final Button openButton = new Button("Select a photo");

        ChoiceBox<String> choiceBox = new ChoiceBox<>();
        choiceBox.getItems().add("Choose a Filter");

        choiceBox.getItems().addAll("Remove watermark", "Brightness", "Grey", "Mosaic");
        choiceBox.getSelectionModel().selectFirst();
        final Pane stac = new Pane();

        openButton.setOnAction(e -> {
            File file = fileChooser.showOpenDialog(stage);
            if (file != null) {
                Image image = new Image(file.toURI().toString());
                ImageView imageView = new ImageView(image);
                imageView.setX(50);
                imageView.setY(50);

                imageView.setFitWidth(300);
                imageView.setFitHeight(470);

                imageView.setPreserveRatio(true);
                stac.getChildren().add(imageView);
            }
        });

        choiceBox.setOnAction(event1 -> {
            if (choiceBox.getValue() == "Mosaic") {
                try {
                    BufferedImage imagen = ImageIO.read(/* A file object is needed here. */ );
                    new Mosaic().mosaico(imagen, 80, 80);
                } catch (IOException ie) {
                    System.err.println("I/O Error");
                    ie.printStackTrace(System.err);
                }
            }
        });

        openButton.setLayoutX(300);
        openButton.setLayoutY(350);
        choiceBox.setLayoutX(430);
        choiceBox.setLayoutY(350);
        stac.getChildren().addAll(openButton, choiceBox);
        stage.setScene(new Scene(stac, 800, 400));
        stage.show();
    }

    public static void main(String[] args) {
        Application.launch(args);
    }
}
EssExx
  • 49
  • 6
  • I am sorry if I am missing something but why don't you define the File object `file` outside of the `openButton.setOnAction` event listener. That way it won't be out of scope in `choiceBox.setOnAction` event listener and you can use it there. – Eralp Sahin Sep 20 '18 at 02:09
  • Java only allow an anonymous function to refer a outside variable which is marked as final. – EssExx Sep 23 '18 at 04:21

1 Answers1

2

I am not sure what is the exact issue you are facing. Firstly FileChooser is not a Button. It is a helper class to interact with ToolKit to open the OS specific file chooser and return the results. And obvisously, it will no keep record of the returned results.

It is your duty to keep a reference of the retrieved file. This can be done in "N" number of ways. As you question focuses on get getting the value from the open button, I would suggest the below approach.

openButton.setOnAction(e -> {
            File file = fileChooser.showOpenDialog(stage);
            openButton.getProperties().put("FILE_LOCATION", file.getAbsolutePath());
            ...
});
choiceBox.setOnAction(event1 -> {
            if (choiceBox.getValue() == "Mosaic") {
                File file = new File(openButton.getProperties().get("FILE_LOCATION").toString());
            }
        });
Sai Dandem
  • 8,229
  • 11
  • 26
  • I tried your suggestion, and it worked well ! that's so helpful, you are so kind, thank you soo much ! :) – EssExx Sep 20 '18 at 03:28
  • 1
    Glad it helped you :). If your question is answered/solved can you mark it accordingly. Thanks !! – Sai Dandem Sep 20 '18 at 03:44