从DataBase填充JavaFX中的ComboBox

时间:2017-06-07 10:47:34

标签: java sqlite javafx rmi fxml

我有这个课程

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());
    }

}}

如你所见,它应该填充组合框&#34; filierecb&#34;使用来自arraylist的数据,但是当我执行代码时,没有显示任何内容,并且组合为空,不显示异常。

1 个答案:

答案 0 :(得分:1)

我发现你有一些不好的做法,例如我不知道为什么staticComboBox,那么你必须使用ArrayList其接口List<"Content"> 1}}具有泛型类型。我认为该错误来自filierecb = new JFXComboBox<>();行,因为您有一个包含.fxml的{​​{1}}文件,因此您无需重新实例化ComboBox,另一个你没有必要为我ComboBox投射observableArrayList()这个代码工作正常:

ObservableList

这是我的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)); } } 文件:

.fxml

我发现你正在使用<?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> ,但我认为这个问题没有任何区别。