Search code examples
javasqlitejavafxrmifxml

Filling ComboBox in JavaFX From DataBase


i have this class

public class FXMLstudentregisterController implements Initializable {

@FXML
private JFXTextField usernametxt;
@FXML
private JFXTextField nomtxt;
@FXML
private JFXTextField prenomtxt;
@FXML
private JFXTextField emailtxt;
@FXML
private JFXTextField passwordhinttxt;
@FXML
private JFXPasswordField passwordtxt;
@FXML
private static  JFXComboBox<String> filierecb ;
@FXML
private JFXButton registerbtn;
@FXML
private JFXComboBox<Integer> promocb = new JFXComboBox<>();;
@FXML
private AnchorPane rootpane;
AnchorPane pane;
/**
 * Initializes the controller class.
 */
           static  Filiere f;
               static  Promotion p;
           static  Student s;
@FXML
private Label lbl;
@Override
public void initialize(URL url, ResourceBundle rb) {
    fillpromo();
    try {
        filierecb =  new JFXComboBox<>();
         ArrayList ar =       service.ConnectServer.getStub().oneColumnQuery("select * from filiere");
        ObservableList<String> a = (ObservableList<String>)   FXCollections.observableArrayList(ar);
        filierecb.getItems().clear();
        filierecb.setItems(a);
    } catch (RemoteException ex) {
        Logger.getLogger(FXMLstudentregisterController.class.getName()).log(Level.SEVERE, null, ex);
    }
}    

@FXML
private void register(ActionEvent event) throws IOException {
    try {
        String nom = nomtxt.getText();
    String prenom = prenomtxt.getText();
    String username = usernametxt.getText();
    String email = emailtxt.getText();
    String password = passwordtxt.getText();
    String passhint = passwordhinttxt.getText();
    f = new Filiere(filierecb.getSelectionModel().getSelectedItem());
    p = new Promotion(promocb.getSelectionModel().getSelectedItem());
    s = new Student(email, nom, prenom, password, username, passhint);
        if (service.ConnectServer.getStub().registerStudent(s, f, p) != 0 ) {
           pane =  FXMLLoader.load(getClass().getResource("/Login/FXMLLogin.fxml"));
        rootpane.getChildren().setAll(pane);
        }else{
            System.out.println("error");
        }

    } catch (RemoteException ex) {
        Logger.getLogger(FXMLstudentregisterController.class.getName()).log(Level.SEVERE, null, ex);
    }
}


public void fillpromo(){

     DateFormat df  = new SimpleDateFormat("YYYY");
    Date d = new Date();
    String y = df.format(d);
    int yy = Integer.parseInt(y);
    ArrayList<Integer> year = new ArrayList<>();
   year.add(yy);
   year.add(yy-1);

    for (Integer integer : year) {
        Promotion pro =new Promotion(integer);
        promocb.getItems().addAll(pro.getPromotion());
    }

}}

And AS you see it's should fill the combobox " filierecb " with the data from the arraylist but when i execute the code nothing shows up and the combo is empty not exception is shown .


Solution

  • i see you have a few bad practices, for example i don't know why is static the ComboBox, then you have to use instead of ArrayList its interface the List<"Content"> with generic type. The bug i think comes from the filierecb = new JFXComboBox<>(); line, because you have an .fxml file which contains the ComboBox so you don't have to re-instantiate the ComboBox, on the other had you don't have to cast the observableArrayList() to ObservableList for me this code works fine:

    package stackoverflow;
    
    import javafx.collections.FXCollections;
    import javafx.fxml.FXML;
    import javafx.fxml.Initializable;
    import javafx.scene.control.ComboBox;
    
    import java.net.URL;
    import java.util.ArrayList;
    import java.util.List;
    import java.util.ResourceBundle;
    
    
    public class TestController implements Initializable {
    
        @FXML
        private ComboBox<String> comboBox;
    
        @Override public void initialize(URL location, ResourceBundle resources) {
            List<String> strings = new ArrayList<>();
            strings.add("Test1");
            strings.add("Test2");
            comboBox.setItems(FXCollections.observableArrayList(strings));
        }
    
    }
    

    And here is my .fxml file:

    <?xml version="1.0" encoding="UTF-8"?>
    <?import javafx.scene.layout.BorderPane?>
    <?import javafx.scene.control.ComboBox?>
    <BorderPane prefHeight="500.0" prefWidth="700.0" xmlns="http://javafx.com/javafx/8.0.65"
            xmlns:fx="http://javafx.com/fxml/1" fx:controller="stackoverflow.TestController">
    <top>
        <ComboBox fx:id="comboBox"/>
    </top>
    

    I see you are using JFXComboBox, but I don`t think there is any difference regarding to this problem.