javafx.JavaFX.java Source code

Java tutorial

Introduction

Here is the source code for javafx.JavaFX.java

Source

/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package javafx;

import java.io.File;
import java.io.FileInputStream;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Iterator;
import java.util.List;
import javafx.application.Application;
import javafx.beans.property.SimpleStringProperty;
import javafx.fxml.FXMLLoader;
import javafx.scene.Parent;
import javafx.scene.Scene;
import org.apache.poi.ss.usermodel.Row;
import javafx.scene.control.Button;
import javafx.scene.control.TableCell;
import javafx.scene.control.TableColumn;
import javafx.scene.control.TreeItem;
import javafx.scene.control.TreeTableCell;
import javafx.scene.control.TreeTableColumn;
import javafx.scene.control.TreeTableColumn.CellDataFeatures;
import javafx.scene.control.TreeTableView;
import javafx.scene.control.cell.ComboBoxTreeTableCell;
import javafx.scene.control.cell.TextFieldTreeTableCell;
import javafx.scene.input.MouseEvent;
import javafx.stage.Stage;
import javafx.util.Callback;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.xssf.usermodel.XSSFSheet;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;

public class JavaFX extends Application {

    public Button but;
    public TreeTableView tree;
    public TreeTableColumn<TRow, String> C1;
    public TreeTableColumn<TRow, String> C2;
    public TreeTableColumn<TRow, String> C3;
    public TreeTableColumn<TRow, String> C4;
    public TreeTableColumn<TRow, String> C5;
    public TreeTableColumn<TRow, String> C6;

    @Override
    public void start(Stage stage) throws Exception {
        Parent root1 = FXMLLoader.load(getClass().getResource("InspektionFXML.fxml"));

        Scene scene = new Scene(root1);
        // stage.setMaximized(true);
        stage.setScene(scene);
        stage.show();
    }

    public void clicked() {
        Object sel = tree.getSelectionModel().getSelectedItem();
        if (sel instanceof TreeItem) {
            TreeItem selItem = (TreeItem) sel;

        }

    }

    public void news() {

    }

    public void initialize() throws Exception {
        tree.setEditable(true);
        tree.setShowRoot(false);
        TreeItem<TRow> root = new TreeItem<>(new TRow("", "", "", "", ""));
        root.setExpanded(true);

        File s = new File("C:\\Users\\Markus\\Desktop\\Prism.xlsx");

        FileInputStream file = new FileInputStream(s);
        XSSFWorkbook workbook = new XSSFWorkbook(file);
        XSSFSheet sheet = workbook.getSheetAt(0);
        Iterator<Row> rowIter = sheet.iterator();
        TreeItem<TRow> level1 = new TreeItem<>();
        TreeItem<TRow> level2 = new TreeItem<>();

        while (rowIter.hasNext()) {
            Row r = rowIter.next();
            String[] values = getValues(r); // 0=Hierarchie, 1=Pruefpunkt, 2=Typ, 3=Einheit, 4=Min, 5=Max

            if (values[0].matches("\\w")) {
                level1 = new TreeItem<>(new TRow(values[1], "", "", "", ""));
                root.getChildren().add(level1);
            }

            if (values[0].matches("\\w[0-9]")) {
                level2 = new TreeItem<>(new TRow(values[1], "", "", "", ""));
                level1.getChildren().add(level2);
            }

            if (values[0].matches("\\w[0-9]\\|[0-9]+")) {
                level2.getChildren().add(new TreeItem<>(
                        new TRow(values[1], "", values[3], formatMinMax(values[4], values[5]), "asd")));
            }

        }

        C1.setCellValueFactory((CellDataFeatures<TRow, String> param) -> new SimpleStringProperty(
                param.getValue().getValue().getPruefpunkt()));

        C2.setCellFactory(TextFieldTreeTableCell.forTreeTableColumn());
        C2.setCellValueFactory((CellDataFeatures<TRow, String> param) -> new SimpleStringProperty(
                param.getValue().getValue().getMesswert()));
        C2.setOnEditCommit((TreeTableColumn.CellEditEvent<TRow, String> evt) -> {
            String nv = evt.getNewValue();
            evt.getRowValue().getValue().setMesswert(nv);
        });

        C3.setCellValueFactory((CellDataFeatures<TRow, String> param) -> new SimpleStringProperty(
                param.getValue().getValue().getEinheit()));
        C4.setCellValueFactory((CellDataFeatures<TRow, String> param) -> new SimpleStringProperty(
                param.getValue().getValue().getMinMax()));

        C4.setCellValueFactory((CellDataFeatures<TRow, String> param) -> new SimpleStringProperty(
                param.getValue().getValue().getMinMax()));

        C5.setCellValueFactory((CellDataFeatures<TRow, String> param) -> new SimpleStringProperty(
                param.getValue().getValue().getStatus()));
        C5.setCellFactory(ComboBoxTreeTableCell.forTreeTableColumn("Pass", "Fail"));
        C5.setOnEditCommit((TreeTableColumn.CellEditEvent<TRow, String> evt) -> {
            String nv = evt.getNewValue();
            evt.getRowValue().getValue().setStatus(nv);
        });
        but.addEventHandler(MouseEvent.MOUSE_CLICKED, (event) -> {
            System.out.println("asdasd231231");
        });
        C6.setCellValueFactory((CellDataFeatures<TRow, String> param) -> new SimpleStringProperty(
                param.getValue().getValue().getBemerkung()));

        Callback<TreeTableColumn<TRow, String>, TreeTableCell<TRow, String>> cellFactory = new Callback<TreeTableColumn<TRow, String>, TreeTableCell<TRow, String>>() {
            public TreeTableCell call(TreeTableColumn p) {
                return new EditTreeTableCell();
            }
        };

        C6.setCellFactory(cellFactory);

        tree.setRoot(root);
    }

    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) {

        launch(args);
    }

    public List<String[]> readTree(TreeItem<TRow> root) {
        return collectData(root, new ArrayList());
    }

    public List<String[]> collectData(TreeItem<TRow> root, List<String[]> collect) {
        for (TreeItem<TRow> node : root.getChildren()) {
            collect.add(node.getValue().getData());
            collectData(node, collect);
        }

        return collect;

    }

    public String formatMinMax(String min, String max) {
        if (min.equals("")) {
            if (max.equals("")) {
                return "";
            }
            return max;
        }

        if (max.equals("")) {
            return min;
        } else {
            return min + " - " + max;
        }

    }

    private String[] getValues(Row r) {
        int n = tree.getColumns().size();

        String[] values = new String[n];

        for (int i = 0; i < n; i++) {
            Cell cell = r.getCell(i);
            if (cell != null) {
                int type = cell.getCellType();

                if (type == Cell.CELL_TYPE_STRING) {
                    values[i] = cell.getStringCellValue();
                } else if (type == Cell.CELL_TYPE_NUMERIC) {
                    values[i] = String.valueOf(cell.getNumericCellValue());
                } else {
                    values[i] = "";
                }

            } else {
                values[i] = "";
            }
        }

        return values;
    }

}