从数据库填充JavaFX中的组合框
我有这门课从数据库填充JavaFX中的组合框,java,sqlite,javafx,rmi,fxml,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 JFXTextFi
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());
}
}}
正如您所看到的,它应该用arraylist中的数据填充组合框filierecb,但是当我执行代码时,没有显示任何内容,组合框为空,也没有显示异常。我看到您有一些不好的做法,例如,我不知道为什么组合框是静态的,然后,您必须使用泛型类型的列表来代替ArrayList接口。我认为这个bug来自filiercb=newjfxcombobox;行,因为您有一个.fxml文件,其中包含ComboBox,因此您不必重新实例化ComboBox,另一方面,如果您不必将ObservalArrayList强制转换为ObservalElist,则此代码可以正常工作:
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));
}
}
我看到您正在使用JFXComboBox,但我认为在这个问题上没有任何区别。您不应该使用@FXML private JFXComboBox promocb=new JFXComboBox;:它应该是@FXML private JFXComboBox promobc;。在问题中发布您的FXML文件;strings=service.ConnectServer.getStub.oneColumnQueryselect*from filiere;filierecb.getItems.addAllFXCollections.ObservableArrayListString;
<?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>