Issue
I'm writing a prograom to work between 2 excel files. but i get confused on how to get access to these 2 files when I reach my third event handler, since the previous 2 event handlers are out of scope.
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");
I import the first excel file here
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);
}
}
);
Second file here
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);
}
}
);
I want to work on the 2 files here but file, and file 1 are out of scope
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);
}
}
Solution
In your method for selecting the Excel file you reference the pane
:
pane.add(label1, 1, 0);
Basically you can do the same for the files, however you will have to add some special handling for the case, that the files are not initialized, as that happens when you open them:
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);
}
...
}
});
With that change you have defined file1
and file2
on the scope of the method and can access it from within the lambda.
Answered By - hotzst
Answer Checked By - David Goodson (JavaFixing Volunteer)