I have been trying to query the data from my database and pass it through my JFXComboBox
as value
I tried wrapping it to a while loop
and add every item to the JFXComboBox
but it still does not show to value from my data.
@FXML
JFXComboBox<String> combobox;
@Override
public void initialize(URL url, ResourceBundle resourceBundle) {
String sql = "Select * from transaction_type from transactions";
try(Connection conn = SQLConnection.getConnection(); /*Singleton Class DB Connection*/
Statement statement = conn.createStatement();
ResultSet rs = statement.executeQuery(sql))
{
while(rs.next()){
combobox.getItems.add(rs.getString(2));
}
}
catch(SQLException e)
{
serr(e.getNessage());
}
}
I expect the items in the JFXComboBox
to be the value found in the transaction
The correct way is to create a model class, however, I think this can help you.
Create a method:
public static void fillListFromDataBase (Connection conn, ObservableList<String> list) {
String sql = "SELECT fieldName FROM transactions";
try {
Statement statement = conn.createStatement();
ResultSet rs = statement.executeQuery(sql))
while(rs.next()) {
list.add(rs.getString("fieldName"));
}
} catch(SQLException e) {
e.printStackTrace();
}
}
Then call the method to load the data in the list and bind the list to Combobox:
JFXComboBox<String> combobox;
private ObservableList<String> list = FXCollections.observableArrayList();
@Override
public void initialize(URL url, ResourceBundle resourceBundle) {
Connection conn = SQLConnection.getConnection();
fillListFromDataBase (conn, list);
combobox.setItems(list);
}