0

I already design my Application in SceneBuilder. I have a button to click on it to choose the excel file to import, also a table view that will be populate from the data inside the excel file.

I get everything working, but the only part is left, is the part that read the excel file and assign to the table view.

How can I read the excel file data and assign the data to my existing table view?

You can see inside my method: importFile() which is working fine, I can click on the button and open up the file choose dialog box and I choose the excel file. Then the next step, I don't know how to do it.

Here's my code:

/* ClockReportFXMLController.java    */

package com.clockreport;

import java.io.File;
import java.net.URL;
import java.util.ResourceBundle;
import javafx.event.ActionEvent;
import javafx.fxml.FXML;
import javafx.fxml.Initializable;
import javafx.scene.control.Button;
import javafx.scene.control.CheckBox;
import javafx.scene.control.TableColumn;
import javafx.scene.control.TableView;
import javafx.stage.FileChooser;
import javafx.stage.Stage;


public class ClockReportFXMLController implements Initializable {

    @FXML
    private TableView<Data> tvData;
    @FXML
    private TableColumn<Data, String> colName;
    @FXML
    private TableColumn<Data, String> colDate;
    @FXML
    private TableColumn<Data, String> colClockIn;
    @FXML
    private TableColumn<Data, String> colClockOut;
    @FXML
    private TableColumn<Data, String> colLate;
    @FXML
    private Button btnImportFile;
    @FXML
    private CheckBox chkCalculateLate;
    @FXML
    private CheckBox chkShowLate;

    /**
     * Initializes the controller class.
     */
    @Override
    public void initialize(URL url, ResourceBundle rb) {
        
    }    

    @FXML
    private void handleButtonAction(ActionEvent event) {
        
        if (event.getSource() == btnImportFile) {
            importFile();
        }
    }

    @FXML
    private void handleCheckboxAction(ActionEvent event) {
        if (event.getSource() == chkShowLate) {
            showLate(chkShowLate.isSelected());
        } else if (event.getSource() == chkCalculateLate) {
            calculateLate(chkCalculateLate.isSelected());
        }
    }

    private void importFile() {
        System.out.println("Run the file import operation");
        String filepath = browseFile();
        
    }
   
    private String browseFile() {
        Stage stage = new Stage();
        FileChooser fileChooser = new FileChooser();
        fileChooser.setTitle("Select File");
        fileChooser.getExtensionFilters().addAll(
                new FileChooser.ExtensionFilter("xls files", "*.xls"),
                new FileChooser.ExtensionFilter("xlsx files", "*.xlsx")
        );
        File file = fileChooser.showOpenDialog(stage);
        String path = file.getPath();
        System.out.println("path: " + path);
        return path;
    }
    

    private void showLate(boolean selected) {
        if (selected) {
            System.out.println("Show late");
        } else {
            System.out.println("Hide late");
        }
    }

    private void calculateLate(boolean selected) {
        if (selected) {
            System.out.println("Calculate late");
        } else {
            System.out.println("Remove calculate late");
        }
    }
    
    
    
}

Another file called "Data.java"

package com.clockreport;


public class Data {
    
    private String name;
    private String date; 
    private String clockIn;
    private String clockOut;
    private String late;

    public Data(String name, String date, String clockIn, String clockOut, String late) {
        this.name = name;
        this.date = date;
        this.clockIn = clockIn;
        this.clockOut = clockOut;
        this.late = late;
    }

    public String getName() {
        return name;
    }

    public String getDate() {
        return date;
    }

    public String getClockIn() {
        return clockIn;
    }
    
    public String getClockOut() {
        return clockOut;
    }

    public String getLate() {
        return late;
    }
    
}

Here's my fxml file called: ClockReportFXML.fxml

<?xml version="1.0" encoding="UTF-8"?>

<?import javafx.scene.control.Button?>
<?import javafx.scene.control.CheckBox?>
<?import javafx.scene.control.Menu?>
<?import javafx.scene.control.MenuBar?>
<?import javafx.scene.control.MenuItem?>
<?import javafx.scene.control.TableColumn?>
<?import javafx.scene.control.TableView?>
<?import javafx.scene.layout.AnchorPane?>
<?import javafx.scene.layout.BorderPane?>


<AnchorPane id="AnchorPane" prefHeight="500.0" prefWidth="600.0" xmlns:fx="http://javafx.com/fxml/1" xmlns="http://javafx.com/javafx/17" fx:controller="com.utupoto.clockreport.ClockReportFXMLController">
   <children>
      <BorderPane prefHeight="31.0" prefWidth="600.0">
         <top>
            <MenuBar prefHeight="28.0" prefWidth="242.0" BorderPane.alignment="CENTER">
              <menus>
                <Menu mnemonicParsing="false" text="File">
                  <items>
                    <MenuItem mnemonicParsing="false" text="Close" />
                  </items>
                </Menu>
                <Menu mnemonicParsing="false" text="Edit">
                  <items>
                    <MenuItem mnemonicParsing="false" text="Delete" />
                  </items>
                </Menu>
                <Menu mnemonicParsing="false" text="Help">
                  <items>
                    <MenuItem mnemonicParsing="false" text="About" />
                  </items>
                </Menu>
              </menus>
            </MenuBar>
         </top>
      </BorderPane>
      <TableView fx:id="tvData" layoutX="13.0" layoutY="75.0" prefHeight="397.0" prefWidth="574.0">
        <columns>
          <TableColumn fx:id="colName" prefWidth="194.0" text="Name" />
          <TableColumn fx:id="colDate" minWidth="0.0" prefWidth="107.0" text="Date" />
            <TableColumn fx:id="colClockIn" prefWidth="100.0" text="Clock In" />
            <TableColumn fx:id="colClockOut" text="Clock Out" />
            <TableColumn fx:id="colLate" prefWidth="91.0" text="Late" />
        </columns>
      </TableView>
      <Button fx:id="btnImportFile" layoutX="14.0" layoutY="40.0" mnemonicParsing="false" onAction="#handleButtonAction" text="Import excel file" />
      <CheckBox fx:id="chkCalculateLate" layoutX="322.0" layoutY="44.0" mnemonicParsing="false" onAction="#handleCheckboxAction" text="Calculate late" />
      <CheckBox fx:id="chkShowLate" layoutX="189.0" layoutY="44.0" mnemonicParsing="false" onAction="#handleCheckboxAction" text="Show late" />
   </children>
</AnchorPane>

Here's the last file called: ClockReport.java

package com.clockreport;

import javafx.application.Application;
import javafx.fxml.FXMLLoader;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.stage.Stage;

/**
 *
 * @author macbookpro
 */
public class ClockReport extends Application{

    @Override
    public void start(Stage stage) throws Exception {
    
        Parent root = FXMLLoader.load(getClass().getResource("ClockReportFXML.fxml"));
        Scene scene = new Scene(root);
        stage.setTitle("Clock In Clock Out Report");
        stage.setScene(scene);
        stage.show();
        
    }
    
    public static void main(String[] args) {
        launch(args);
    }
    
}
mana
  • 1,075
  • 1
  • 24
  • 43
  • 3
    You should look into Apache POI library which you can use to read Excel files. https://poi.apache.org/ – Cheng Thao Dec 19 '21 at 01:37
  • Consider saving the excel format to csv format. Then readi the data using either code you write, or a third party library that reads csv files . It would probably be easier to do that than to read a .xls file. It just depends on whether or not that is possible for you, if not then you can use poi as the prior comment suggests. – jewelsea Dec 19 '21 at 04:24
  • 2
    This can be broken down into basically two steps. 1. Use `Apache POI` to create a `List` of `Data`. 2. Follow any beginner tutorial that shows how to load a `List` of `Objects` into a `TableView`. [Here](https://stackoverflow.com/questions/70302119/show-json-in-tableview/70302970#70302970) is a similar example. It uses a different data format, but the logic on how to load the data into a `TableView` is the same. – SedJ601 Dec 19 '21 at 08:51

0 Answers0