Получение переменной ref из filechooser в другом месте

в моем проекте, когда я открываю файл (нажимая кнопку), я использую FileChooser, но я не знаю, как работать с этим файлом для других кнопок, вот мой код:

@Override // This method is called by the FXMLLoader when initialization is complete
public void initialize(URL fxmlFileLocation, ResourceBundle resources) {
    assert open != null : "fx:id=\"open\" was not injected: check your FXML file 'Sample.fxml'.";
    assert nowy != null : "fx:id=\"nowy\" was not injected: check your FXML file 'Sample.fxml'.";
    assert kolor != null : "fx:id=\"nowy\" was not injected: check your FXML file 'Sample.fxml'.";
    assert p_k != null : "fx:id=\"p_k\" was not injected: check your FXML file 'Sample.fxml'.";
    assert d_k != null : "fx:id=\"d_k\" was not injected: check your FXML file 'Sample.fxml'.";
    assert t_k != null : "fx:id=\"t_k\" was not injected: check your FXML file 'Sample.fxml'.";
    // initialize your logic here: all @FXML variables will have been injected
  // initialize your logic here: all @FXML variables will have been injected
    p_k.setToggleGroup(group);
    p_k.setSelected(true);
    d_k.setToggleGroup(group);
    t_k.setToggleGroup(group);
    String nazwa;
    open.setOnAction(new EventHandler<ActionEvent>() {

        public void handle(ActionEvent event)  {
            FileChooser fileChooser = new FileChooser();
            fileChooser.setTitle("Open Resource File");

            File selectedFile = fileChooser.showOpenDialog(null);
            name = selectedFile.getPath();
            final nazwa= name;
           if (selectedFile != null) {
                try {


                   Sekwencja2 otwieracz = new Sekwencja2();

                   double [] bufo = otwieracz.sekwencja2(name,klatka);

                    JavaFXImageConversion nowa = new JavaFXImageConversion();

                    Image inn = nowa.getJavaFXImage(bufo, 320, 240,2);

                     iV.setImage(inn);
                } catch (FileNotFoundException ex) {
                    Logger.getLogger(SampleController.class.getName()).log(Level.SEVERE, null, ex);
                } catch (IOException ex) {
                    Logger.getLogger(SampleController.class.getName()).log(Level.SEVERE, null, ex);
                }
           }


                        }


    }

);

    nowy.setOnAction((ActionEvent event) -> {

        try {
            Sekwencja2 otwieracz = new Sekwencja2();

            double [] bufo = otwieracz.sekwencja2(nazwa,klatka);

            JavaFXImageConversion nowa = new JavaFXImageConversion();

            Image inn = nowa.getJavaFXImage(bufo, 320, 240,2);

            iV.setImage(inn);
        } catch (IOException ex) {
            Logger.getLogger(SampleController.class.getName()).log(Level.SEVERE, null, ex);
        }
    });

Конечно, это что-то простое, но у меня нет идей для этого. Надеюсь, вы мне в этом поможете :)


person Katarzyna Kamińska    schedule 16.11.2015    source источник


Ответы (1)


Насколько я понимаю ваш вопрос, я думаю, что это должно вам помочь:

    open.setOnAction(new EventHandler<ActionEvent>() {

    public void handle(ActionEvent event) throws MalformedURLException  {
        FileChooser fileChooser = new FileChooser();
        fileChooser.setTitle("Open Resource File");

        File selectedFile = fileChooser.showOpenDialog(null);

       if (selectedFile != null) {
          //you can set your image to a Image view 
          imageView.setImage(new Image(selectedFile.toURI().toURL().toExternalForm()));
          //or else you can reassign your image path to a string variable so that you can re-use that String variable in all over the class 
          path = selectedFile.toURI().toURL().toExternalForm();  
       }else {
        System.out.println("Error Selection");
       }
person Madushan Perera    schedule 17.11.2015