Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public class ThirdWindowController implements Initializable {
- Database database = new Database();
- private ObservableList<ObservableList> data;
- @FXML
- MenuItem createCategories = new MenuItem();
- @FXML
- private TableView tableOfProp;
- @FXML
- private TableView nameAndCount;
- @FXML
- Button addCategories = new Button();
- @Override
- public void initialize(URL location, ResourceBundle resources) {
- try {
- database.setConnection();
- } catch (ClassNotFoundException e) {
- e.printStackTrace();
- } catch (SQLException e) {
- e.printStackTrace();
- }
- Connection connection = null;
- Statement statement;
- data = FXCollections.observableArrayList();
- try {
- Class.forName("org.sqlite.JDBC");
- connection = DriverManager.getConnection("jdbc:sqlite:Database.db");
- statement = connection.createStatement();
- ResultSet rs = statement.executeQuery("select category from categories");
- for (int i = 0; i < rs.getMetaData().getColumnCount(); i++) {
- //We are using non property style for making dynamic table
- final int j = i;
- TableColumn col = new TableColumn(rs.getMetaData().getColumnName(i + 1));
- col.setCellValueFactory(new Callback<TableColumn.CellDataFeatures<ObservableList, String>, ObservableValue<String>>() {
- public ObservableValue<String> call(TableColumn.CellDataFeatures<ObservableList, String> param) {
- return new SimpleStringProperty(param.getValue().get(j).toString());
- }
- });
- tableOfProp.getColumns().addAll(col);
- System.out.println("Column [" + i + "] ");
- }
- /********************************
- * Data added to ObservableList *
- ********************************/
- while (rs.next()) {
- //Iterate Row
- ObservableList<String> row = FXCollections.observableArrayList();
- for (int i = 1; i <= rs.getMetaData().getColumnCount(); i++) {
- //Iterate Column
- row.add(rs.getString(i));
- }
- System.out.println("Row [1] added " + row);
- data.add(row);
- }
- //FINALLY ADDED TO TableView
- tableOfProp.setItems(data)
- // insert(nameAndCount,"select name,amount from ingredients");
- } catch (Exception e) {
- e.printStackTrace();
- System.out.println("Error on Building Data");
- }
- createCategories.setOnAction((event) -> {
- Parent root;
- try {
- FXMLLoader loader = new FXMLLoader(getClass().getResource("../fxFiles/FourthWindowFX.fxml"));
- root = loader.load();
- Stage stage = new Stage();
- stage.setTitle("Second window");
- stage.initModality(Modality.WINDOW_MODAL);
- Scene scene = new Scene(root);
- stage.setScene(scene);
- stage.initOwner(addCategories.getScene().getWindow());
- stage.showAndWait();
- } catch (IOException e) {
- e.printStackTrace();
- }
- });
- // todo for button
- }
- public void createWindow() {
- Parent root;
- try {
- FXMLLoader loader = new FXMLLoader(getClass().getResource("../fxFiles/ThirdWindowFX.fxml"));
- root = loader.load();
- Stage stage = new Stage();
- stage.setTitle("Second window");
- stage.initModality(Modality.WINDOW_MODAL);
- Scene scene = new Scene(root);
- stage.setScene(scene);
- stage.showAndWait();
- database.closeConnection();
- } catch (Exception exc) {
- exc.printStackTrace();
- }
- }
- public void insert(TableView tableView, String sqlStatement) throws
- SQLException, ClassNotFoundException {
- Connection connection = null;
- Statement statement;
- data = FXCollections.observableArrayList();
- try {
- Class.forName("org.sqlite.JDBC");
- connection = DriverManager.getConnection("jdbc:sqlite:Database.db");
- statement = connection.createStatement();
- ResultSet rs = statement.executeQuery(sqlStatement);
- for (int i = 0; i < rs.getMetaData().getColumnCount(); i++) {
- //We are using non property style for making dynamic table
- final int j = i;
- TableColumn col = new TableColumn(rs.getMetaData().getColumnName(i + 1));
- col.setCellValueFactory(new Callback<TableColumn.CellDataFeatures<ObservableList, String>, ObservableValue<String>>() {
- public ObservableValue<String> call(TableColumn.CellDataFeatures<ObservableList, String> param) {
- return new SimpleStringProperty(param.getValue().get(j).toString());
- }
- });
- tableView.getColumns().addAll(col);
- System.out.println("Column [" + i + "] ");
- }
- /********************************
- * Data added to ObservableList *
- ********************************/
- while (rs.next()) {
- //Iterate Row
- ObservableList<String> row = FXCollections.observableArrayList();
- for (int i = 1; i <= rs.getMetaData().getColumnCount(); i++) {
- //Iterate Column
- row.add(rs.getString(i));
- }
- System.out.println("Row [1] added " + row);
- data.add(row);
- }
- //FINALLY ADDED TO TableView
- tableView.setItems(data);
- } catch (Exception e) {
- e.printStackTrace();
- System.out.println("Error on Building Data");
- }
- }
- }
Add Comment
Please, Sign In to add comment