I want to use the variable data's value from the following method in another class and this method handles the run button in one window and the other class handles the buttons which are located in another window. I have tried a few ways to do that but none of them worked, so I would be grateful if anyone could help me with it.
public class Test{
@FXML
private TextField sqlTextField;
@FXML
private TableView table;
@FXML
private Button save;
@FXML
private Button run;
@FXML
private Button delete;
public ObservableList<ObservableList> data = FXCollections.observableArrayList();
public void handleRunButton() throws SQLException {
run.setDisable(true);
try {
ConnectionClass connectionClass = new ConnectionClass();
Connection connection = connectionClass.getConnection();
String sql = sqlTextField.getText() + ";";
Statement statement = connection.createStatement();
ResultSet resultSet = statement.executeQuery(sql);
for (int i = 0; i < resultSet.getMetaData().getColumnCount(); i++) {
final int j = i;
TableColumn col = new TableColumn(resultSet.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());
}
});
table.getColumns().addAll(col);
System.out.println("Column [" + i + "] ");
}
/********************************
* Data added to ObservableList *
********************************/
while (resultSet.next()) {
//Iterate Row
ObservableList<String> row = FXCollections.observableArrayList();
for (int i = 1; i <= resultSet.getMetaData().getColumnCount(); i++) {
row.add(resultSet.getString(i));
}
data.add(row);
System.out.println("Row [1] added " + row);
}
System.out.println(data);
//FINALLY ADDED TO TableView
table.setItems(data);
System.out.println("data =" + data);
} catch (Exception e) {
e.printStackTrace();
System.out.println("Error on Building Data");
}
ObservableList tableText = table.getItems();
System.out.println("tableText=" + tableText);
boolean disableButton2;
if (tableText.size() > 0) {
disableButton2 = false;
} else {
disableButton2 = true;
}
save.setDisable(disableButton2);
System.out.println(disableButton2);
delete.setDisable(disableButton2);
System.out.println("data==>" + data);
}