从其他位置的filechooser获取变量ref



在我的项目中,当我打开一个文件(按下按钮)时,我使用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);
        }
    });

当然,这很简单,但我对此没有任何想法。希望你能帮助我:)

为了理解你的问题,我认为这应该会对你有所帮助:

    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");
       }

最新更新