I developed a simple demo JavaFx app with Sqlite since command line(terminal) with two tabs (tab1-Info and tab2-Register), in tab1-Info there is a tableview "myTable" that shows only id, name and lastname and in tab2-Register shows the textfields to register name and lastname as seen in the following figures.
In the archives i use tab1.fxml and tab2.fxml which tab1 calls Controller.java and tab2.fxml calls Tab2Controller.java. Actually the simple demo JavaFx app with Sqlite is working perfectly, but i do not know how to refresh tableview "myTable" when i pressed the button "save" which is in another controller tab2.fxml (Tab2Controller.java) different from where tab1-info TableView "myTable" is located. I wish that when i pressed the button "save", automatically "myTable" updates/refresh the new content data, any advice in order to solve this issue?
Here are the java program archives.
principal.java
import javafx.application.Application;
import javafx.fxml.FXMLLoader;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.stage.Stage;
public class principal extends Application {
public static void main(String[] args) {
Application.launch(principal.class, args);
}
@Override
public void start(Stage stage) throws Exception {
Parent root = FXMLLoader.load(getClass().getResource("scheme.fxml"));
stage.setTitle("Demo tab version");
stage.setScene(new Scene(root, 300, 400));
stage.show();
}
}
scheme.fxml
<?xml version="1.0" encoding="UTF-8"?>
<?import java.net.*?>
<?import javafx.geometry.*?>
<?import javafx.scene.control.*?>
<?import javafx.scene.layout.*?>
<?import javafx.scene.text.*?>
<GridPane xmlns:fx="http://javafx.com/fxml" hgap="20" vgap="20" styleClass="root" gridLinesVisible="false">
<padding><Insets top="25" right="2" bottom="20" left="2" /></padding>
<Text id="welcome-text" text="Demo tab" GridPane.columnIndex="0" GridPane.rowIndex="0" GridPane.columnSpan="6" GridPane.rowSpan="1"/>
<TabPane fx:id="tabPane" xmlns:fx="http://javafx.com/fxml" prefHeight="500.0" prefWidth="300.0" GridPane.columnIndex="0" GridPane.rowIndex="1" GridPane.columnSpan="6" tabClosingPolicy="UNAVAILABLE">
<tabs>
<Tab fx:id="tab1" text="Info" >
<fx:include source="Tab1.fxml"/>
</Tab>
<Tab fx:id="tab2" text="Register" >
<fx:include source="Tab2.fxml"/>
</Tab>
</tabs>
</TabPane>
<stylesheets>
<URL value="@Login.css" />
</stylesheets>
Tab1.fxml
<?xml version="1.0" encoding="UTF-8"?>
<?import java.net.*?>
<?import javafx.geometry.*?>
<?import javafx.scene.control.*?>
<?import javafx.scene.layout.*?>
<?import javafx.scene.text.*?>
<?import javafx.scene.control.TabPane?>
<?import javafx.scene.control.Tab?>
<?import javafx.scene.control.ComboBox?>
<?import javafx.scene.control.cell.*?>
<GridPane fx:controller="Controller" xmlns:fx="http://javafx.com/fxml" hgap="20" vgap="20" >
<!-- <ScrollPane visible="true" GridPane.columnIndex="0" GridPane.rowIndex="0" GridPane.columnSpan="1" GridPane.rowSpan="5"> -->
<VBox layoutX="15.0" layoutY="76.0" prefHeight="528.0" prefWidth="1200.0">
<children>
<TableView fx:id="myTable" prefHeight="512.0" prefWidth="500.0" />
</children>
</VBox>
</GridPane>
Tab2.fxml
<?xml version="1.0" encoding="UTF-8"?>
<?import java.net.*?>
<?import javafx.geometry.*?>
<?import javafx.scene.control.*?>
<?import javafx.scene.layout.*?>
<?import javafx.scene.text.*?>
<?import javafx.scene.control.TabPane?>
<?import javafx.scene.control.Tab?>
<?import javafx.scene.control.Label?>
<GridPane xmlns:fx="http://javafx.com/fxml" fx:controller="Tab2Controller" hgap="10" vgap="5">
<Text id="informacion" text="Personal Information" GridPane.columnIndex="1" GridPane.rowIndex="0" style="-fx-fill:white" />
<Text text="Name" GridPane.columnIndex="1" GridPane.rowIndex="2" style="-fx-fill:white"/>
<TextField fx:id="name" GridPane.columnIndex="1" GridPane.rowIndex="3" prefWidth="150.0"/>
<Text text="Lastname" GridPane.columnIndex="1" GridPane.rowIndex="4" style="-fx-fill:white" />
<TextField fx:id="lastname" GridPane.columnIndex="1" GridPane.rowIndex="5" prefWidth="300.0" />
<Button text="Save" GridPane.columnIndex="1" GridPane.rowIndex="7" prefHeight="20" prefWidth="90" GridPane.halignment="RIGHT" onAction="#handleSubmitButtonAction"/>
</GridPane>
Controller.java
import java.sql.*;
import javafx.application.Application;
import javafx.scene.Scene;
import javafx.scene.control.TableColumn;
import javafx.scene.control.TableView;
import javafx.scene.control.cell.PropertyValueFactory;
import javafx.scene.layout.VBox;
import javafx.stage.Stage;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.net.URL;
import java.util.ResourceBundle;
import javafx.collections.FXCollections;
import javafx.collections.ObservableList;
import javafx.event.ActionEvent;
import javafx.fxml.FXML;
import javafx.fxml.Initializable;
import javafx.scene.control.Label;
public class Controller implements Initializable{
@FXML
public TableView myTable;
@Override
public void initialize(URL url, ResourceBundle rb) {
TableColumn<Integer, Person> column1 = new TableColumn<>("Id");
column1.setCellValueFactory(new PropertyValueFactory<>("id"));
TableColumn<String, Person> column2 = new TableColumn<>("Name");
column2.setCellValueFactory(new PropertyValueFactory<>("nomCom"));
TableColumn<String, Person> column3 = new TableColumn<>("LastName");
column3.setCellValueFactory(new PropertyValueFactory<>("ApellPatern"));
myTable.getColumns().add(column1);
myTable.getColumns().add(column2);
myTable.getColumns().add(column3);
String sql = "SELECT id, nomCom, ApellPatern FROM warehouses";
try (Connection conn = this.connect();
Statement stmt = conn.createStatement();
ResultSet rs = stmt.executeQuery(sql)){
System.out.println("Opened database successfully");
while (rs.next()) {
System.out.println(rs.getInt("id")+ "\t" + rs.getString("nomCom") + "\t" + rs.getString("ApellPatern"));
myTable.getItems().add(new Person(rs.getInt("id"),rs.getString("nomCom"),rs.getString("ApellPatern")));
}/*Aqui finaliza el while */
/*Aqui finaliza el executequery*/
} catch (SQLException e) {
System.out.println(e.getMessage());
}
}/* End of initialize */
public class Person {
private Integer id=null;
private String nomCom=null;
private String ApellPatern=null;
public Person(Integer id, String nomCom, String ApellPatern){
this.id=id;
this.nomCom=nomCom;
this.ApellPatern=ApellPatern;
}
public Integer getId(){
return id;
}
public String getNomCom() {
return nomCom;
}
public String getApellPatern(){
return ApellPatern;
}
}
private Connection connect() {
String url ="jdbc:sqlite:test.db";
Connection conn = null;
try {
conn = DriverManager.getConnection(url);
} catch (SQLException e) {
System.out.println(e.getMessage());
}
return conn;
}
}
Tab2Controller.java
import java.sql.*;
import javafx.application.Application;
import javafx.scene.Scene;
import javafx.scene.control.TableColumn;
import javafx.scene.control.TableView;
import javafx.scene.control.TextField;
import javafx.scene.control.cell.PropertyValueFactory;
import javafx.scene.layout.VBox;
import javafx.stage.Stage;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.net.URL;
import java.util.ResourceBundle;
import javafx.collections.FXCollections;
import javafx.collections.ObservableList;
import javafx.event.ActionEvent;
import javafx.fxml.FXML;
import javafx.fxml.Initializable;
import javafx.scene.control.Label;
import java.sql.SQLException;
import javafx.scene.control.Alert.AlertType;
import javafx.scene.control.Alert;
public class Tab2Controller implements Initializable{
/*@FXML private fx:id="name" */
@FXML private TextField name;
/*@FXML private fx:id="lastname" ;*/
@FXML private TextField lastname;
@Override
public void initialize(URL url, ResourceBundle rb) {
/* No information*/
}
@FXML protected void handleSubmitButtonAction(ActionEvent event) {
String nomCom=name.getText();
String ApellPatern=lastname.getText();
String sql = "INSERT INTO warehouses(nomCom,ApellPatern) VALUES(?,?)";
try (Connection conn = this.connect();
PreparedStatement pstmt = conn.prepareStatement(sql)) {
pstmt.setString(1,nomCom);
pstmt.setString(2, ApellPatern);
pstmt.executeUpdate();
} catch (SQLException e) {
System.out.println(e.getMessage());
}
Alert alert = new Alert(AlertType.INFORMATION);
alert.setTitle("Information Dialog");
alert.setHeaderText(null);
alert.setContentText("The information has been saved succesfully");
alert.showAndWait();
}
private Connection connect() {
// SQLite connection string
// String url = "jdbc:sqlite:C://sqlite/db/test.db";
String url ="jdbc:sqlite:test.db";
Connection conn = null;
try {
conn = DriverManager.getConnection(url);
} catch (SQLException e) {
System.out.println(e.getMessage());
}
return conn;
}
}
Any help is appreciated. Thank you in advance