I tried to build a JavaFX application. In the controller some function in initialize method is not invoked.
For example:
@Override
public void initialize(URL location, ResourceBundle resources) {
txt_name.requestFocus();
btn_update.setVisible(false);
btn_delete.setVisible(false);
chk_active.setSelected(true);
clear();
listner_txtbx();
setTable();
Populate_Table();
}
In the above code, CheckBox selection code and Populate_table method are not invoked and all other methods and codes are invoked.
This is the controller file
public class BrandController implements Initializable {
public TextField txt_id;
public TextField txt_name;
public TextField txt_remarks;
public Button btn_save;
public Button btn_update;
public Button btn_delete;
public Button btn_clear;
public CheckBox chk_active;
public TableView tbl_brand;
public TableColumn col_slno;
public TableColumn col_id;
public TableColumn col_name;
public TableColumn col_remarks;
public TableColumn col_status;
public Button btn_Close;
private Stage stage_brand;
ObservableList<obj_Brand> tblData = FXCollections.observableArrayList();
DBMysql db = new DBMysql();
@Override
public void initialize(URL location, ResourceBundle resources) {
txt_name.requestFocus();
btn_update.setVisible(false);
btn_delete.setVisible(false);
chk_active.setSelected(true);
clear();
listner_txtbx();
setTable();
Populate_Table();
}
private void clear(){
txt_name.clear();
txt_remarks.clear();
setCode();
txt_name.requestFocus();
}
private void Populate_Table(){
System.out.println("inside table");
tblData.clear();
int status;
if(chk_active.isSelected()){status=1;}else{status=0;}
try {
Statement st = db.con.createStatement();
System.out.println("select * from " + db.schema + ".tbl_brand where status =" +status+ "");
ResultSet rs = st.executeQuery("select * from " + db.schema + ".tbl_brand where status =" +status+ "");
while (rs.next()) {
tblData.add(new obj_BrandBuilder().setCode(rs.getString("code")).setName(rs.getString("name")).setRemarks(rs.getString("remarks")).setStatus(rs.getString("status")).createObj_Brand());
}
st.close();
} catch (SQLException ex) {
Logger.getLogger(BrandController.class.getName()).log(Level.SEVERE, null, ex);
}
}
I tried the Populate_Table method using a Button On Action method and the code is working, but it's not invoked in the initialize method.
How can I fix this?
Preface
Put system.outs in you method and check If they really are not executed. I think they are executed.
Solution
Initialize is called after the controller creation is finished. Your Scene is not displayed yet.
Put your code in Platform.runLater()
, this will put the execution in the future.
If this does not work, set the size of the stage to "+1" and after that again to "-1" (for width and height). This triggers the renderer.
If this does not work again, post a working minimal example of your code.