[FIXED] Zugriff auf Datei zwischen 2 Ereignishandlern

Ausgabe

Ich schreibe ein Programm, um zwischen 2 Excel-Dateien zu arbeiten. aber ich bin verwirrt darüber, wie ich auf diese 2 Dateien zugreifen kann, wenn ich meinen dritten Event-Handler erreiche, da die vorherigen 2 Event-Handler außerhalb des Gültigkeitsbereichs liegen.

import java.io.File;
import java.io.FileInputStream;

import org.apache.poi.xssf.usermodel.XSSFSheet;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;

import javafx.application.Application;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.geometry.Insets;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.Label;
import javafx.scene.layout.GridPane;
import javafx.stage.DirectoryChooser;
import javafx.stage.FileChooser;
import javafx.stage.Stage;


public class MyScene extends Application{


    public void start(Stage PrimaryStage) {
        GridPane pane = new GridPane();
        Button input1 = new Button("Select vendor sheet");
        Button input2 = new Button("Select master sheet");
        Button output1 = new Button("Updated File Destination");
        Button execute = new Button("Execute");
        FileChooser inputFile = new FileChooser();
        inputFile.setTitle("Open XLSX file");
        DirectoryChooser outputFile = new DirectoryChooser();
        outputFile.setTitle("Select Destination");

Ich importiere die erste Excel-Datei hier

        input1.setOnAction(
                new EventHandler<ActionEvent>() {
                    public void handle(final ActionEvent e) {
                        File file = inputFile.showOpenDialog(PrimaryStage);
                        System.out.print(file.getPath());
                        Label label1 = new Label(file.getPath());
                        pane.add(label1, 1, 0);
                        }   
                }
                );

Zweite Datei hier

        input2.setOnAction(
                new EventHandler<ActionEvent>() {
                    public void handle(final ActionEvent e) {
                        File file1 = inputFile.showOpenDialog(PrimaryStage);
                        Label label2 = new Label(file1.getPath());
                        pane.add(label2, 1, 1);                         
                        }           
                }
                );
        output1.setOnAction(
                new EventHandler<ActionEvent>() {
                    public void handle(final ActionEvent e) {
                        File file2 = outputFile.showDialog(PrimaryStage);
                        System.out.print(file2.getPath());
                        Label label3 = new Label(file2.getPath());
                        pane.add(label3, 1, 2);                         
                        }           
                }
                );

Ich möchte hier an den 2 Dateien arbeiten, aber Datei und Datei 1 liegen außerhalb des Gültigkeitsbereichs

        execute.setOnAction(
                new EventHandler<ActionEvent>() {
                    public void handle(final ActionEvent e) {
                        XSSFWorkbook wb = new XSSFWorkbook(file1);
                        XSSFWorkbook wb2 = new XSSFWorkbook(file2);
                        
                                                    
                        }           
                }
                );
        
        pane.setHgap(5);
        pane.setVgap(5);
        pane.setPadding(new Insets(2));
        pane.add(input1, 0, 0);
        pane.add(input2, 0, 1);
        pane.add(execute, (int) 1.5, 3);
        pane.add(output1, 0, 2);
        Scene scene1 = new Scene(pane);
        PrimaryStage.setScene(scene1);
        PrimaryStage.show();
        
        
        
    }
public static void main(String[]args) {
    launch(args);
    
}

        
}

Lösung

In Ihrer Methode zur Auswahl der Excel-Datei verweisen Sie auf pane:

pane.add(label1, 1, 0);

Grundsätzlich können Sie dasselbe für die Dateien tun, müssen jedoch eine spezielle Behandlung für den Fall hinzufügen, dass die Dateien nicht initialisiert werden, da dies beim Öffnen der Fall ist:

public void start(Stage PrimaryStage) {
    File file1, file2;
    ...
    input2.setOnAction(
    new EventHandler<ActionEvent>() {
        public void handle(final ActionEvent e) {
        file1 = inputFile.showOpenDialog(PrimaryStage);
        Label label2 = new Label(file1.getPath());
        pane.add(label2, 1, 1);                         
        }           
    });
    output1.setOnAction(
    new EventHandler<ActionEvent>() {
        public void handle(final ActionEvent e) {
        file2 = outputFile.showDialog(PrimaryStage);
        System.out.print(file2.getPath());
        Label label3 = new Label(file2.getPath());
        pane.add(label3, 1, 2);                         
        }           
    });
    ...
    execute.setOnAction(
        new EventHandler<ActionEvent>() {
            public void handle(final ActionEvent e) {
                if (file1 != null) {
                    XSSFWorkbook wb = new XSSFWorkbook(file1);
                }
                if (file2 != null) {
                    XSSFWorkbook wb2 = new XSSFWorkbook(file2);
                }
                ...
            }           
    });

Mit dieser Änderung haben Sie den Gültigkeitsbereich der Methode definiert file1und file2können von Lambda aus darauf zugreifen.


Beantwortet von –
hotzst


Antwort geprüft von –
David Marino (FixError Volunteer)

0 Shares:
Leave a Reply

Your email address will not be published. Required fields are marked *

You May Also Like