-
Notifications
You must be signed in to change notification settings - Fork 0
Open
Labels
bugSomething isn't workingSomething isn't workinghelp wantedExtra attention is neededExtra attention is needed
Description
Background
After refactoring all of the database access code to DBController.java the TableView no longer displays correctly. Initially I had a connect() inside Controller.java which connected to database, ran several queries, and formatted the Tabs/TableViews. Here is the code for that method.
private void connect() {
StringBuilder dbUrl = new StringBuilder("jdbc:sqlite:");
if (dbUrlTxt.getText().isEmpty()) {
new Alert(Alert.AlertType.ERROR, "Database url must be specified.").showAndWait();
} else {
try {
connection = getConnection(dbUrl.append(dbUrlTxt.getText()).toString());
} catch (SQLException e) {
e.printStackTrace();
}
String tableQuery = "SELECT * FROM sqlite_master WHERE type='table' ORDER BY name";
try (PreparedStatement tableQueryPS = connection.prepareStatement(tableQuery)) {
ResultSet tableNames = tableQueryPS.executeQuery();
while (tableNames.next()) {
Tab tab = new Tab(tableNames.getString("name"));
tabPane.getTabs().add(tab);
TableView<ObservableList> tableView = new TableView<>();
tab.setContent(tableView);
String dataQuery = "SELECT * from " + tableNames.getString("name");
ResultSet tableValues = connection.createStatement().executeQuery(dataQuery);
for (int i = 0; i < tableValues.getMetaData().getColumnCount(); i++) {
final int j = i;
int dataValue = tableValues.getMetaData().getColumnType(i + 1);
// I need to use some generics here on the TableColumn to get rid of the Unchecked call to setCellValueFactory()
TableColumn tableColumn = new TableColumn(tableValues.getMetaData().getColumnName(i + 1));
if (dataValue == 4) {
tableColumn.setCellValueFactory((Callback<TableColumn.CellDataFeatures<ObservableList, Integer>, ObservableValue<String>>) param ->
new SimpleStringProperty(param.getValue().get(j).toString()));
} else if (dataValue == 7) {
tableColumn.setCellValueFactory((Callback<TableColumn.CellDataFeatures<ObservableList, Double>, ObservableValue<String>>) param ->
new SimpleStringProperty(param.getValue().get(j).toString()));
} else if (dataValue == 12) {
tableColumn.setCellValueFactory((Callback<TableColumn.CellDataFeatures<ObservableList, String>, ObservableValue<String>>) param ->
new SimpleStringProperty(param.getValue().get(j).toString()));
}
tableView.getColumns().addAll(tableColumn);
}
ObservableList<ObservableList> data = FXCollections.observableArrayList();
while (tableValues.next()) {
ObservableList<String> row = FXCollections.observableArrayList();
for (int i = 1; i <= tableValues.getMetaData().getColumnCount(); i++) {
row.add(tableValues.getString(i));
}
data.add(row);
}
tableValues.close();
tableView.getItems().addAll(data);
}
tableNames.close();
} catch (SQLException tableQueryException) {
System.err.println(tableQueryException.toString());
}
connectBtn.setDisable(true);
disconnectBtn.setDisable(false);
addBtn.setDisable(false);
updateBtn.setDisable(false);
deleteBtn.setDisable(false);
saveBtn.setDisable(false);
}
}This method generated the expected results.
Expected Results
After refactoring, I get these results
Results
I have been stuck on this for several weeks now. Any help would be immensely appreciated.
Reactions are currently unavailable
Metadata
Metadata
Assignees
Labels
bugSomething isn't workingSomething isn't workinghelp wantedExtra attention is neededExtra attention is needed