How do i update ListView items when i added a new data. Here is my code for displaying data to my listview:
public class ChapterController implements Initializable {
@FXML
private ListView<String> listview;
ObservableList<String> view = FXCollections.observableArrayList();
@FXML
private TextField des;
@FXML
private TextField no;
Connection connection = null;
PreparedStatement preparedStatement = null;
ResultSet resultSet = null;
public ChapterController() {
connection = Connect.ConnectDb();
}
@Override
public void initialize(URL url, ResourceBundle rb) {
listview.setItems(view);
ListView()
}
//display data to the listview
private void ListView(){
String sql = "SELECT * FROM chapter ORDER BY id ASC";
try{
preparedStatement = connection.prepareStatement(sql);
resultSet = preparedStatement.executeQuery();
while(resultSet.next()){
view.add("Chapter" +" "+ resultSet.getString(1) +" "+ "-" +" "+ resultSet.getString(2));
}
}catch(Exception e){
}
}
And here is my code for adding item on the list view, and I want to automatically updated the listview when I save the data. Where should I put my code for updating the listview.
public void buttonAction(ActionEvent event){
connection = Connect.ConnectDb();
try{
String sql = "INSERT INTO chapter"
+"(id, description)"
+"VALUES(?,?)";
preparedStatement = (PreparedStatement) connection.prepareStatement(sql);
preparedStatement.setString(1, no.getText());
preparedStatement.setString(2, des.getText());
preparedStatement.executeUpdate();
ListView(); //I am using to call the ListView but it is not good at all
}catch(Exception e){
}
}