1
votes

I'm noob in javafx and scene builder. I want to populate tableview by selecting one item from combobox. It is possible?

i try with String val = combobox.getValue() and i put the string in SQL query in preparedStatement for directly sort but app stops at the null string value and tableview is not updated.

Thank you guys!

1
Is it possible? Yes. You probably get the value before the scene is even shown/the user has a chance to interact with the GUI. - fabian

1 Answers

0
votes

It is possible that the String is being initialized with the ComboBox value even before the ComboBox gets an input. In that case, the ComboBox will return a null value.

You should add an onAction event for the ComboBox which will update the string. You can use the following code segment to do that

comboBox.setOnAction((event) -> {
  val = comboBox.getValue();
//Any other action you want to carry out when an item of the combo box is selected
});

Or if you are using an FXML file and want to add the onAction event in the controller, you can use this.

public void comboBoxEvent(ActionEvent event){
  val = comboBox.getValue();
} // Use this code when working with FXML files 

Both these examples assume that the String var was defined globally. Just to be on the safer side, when you are comparing var to another value or storing it somewhere else, you should put it under an if condition

if(var != null)
  //Code segment here