我有这门课
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());
}
}}
正如您所看到的,它应该用数组列表中的数据填充组合框“filierecb ”,但是当我执行代码时,没有任何显示,并且组合为空,不显示异常。
最佳答案
我发现你有一些不好的做法,例如我不知道为什么是 static
ComboBox
,那么你必须使用 ArrayList
来代替其界面List<"Content">
与泛型类型。我认为该错误来自 filierecb = new JFXComboBox<>();
行,因为你有 .fxml
文件包含 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>
我看到您正在使用JFXComboBox
,但我认为这个问题没有任何区别。
关于java - 从数据库填充 JavaFX 中的组合框,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44410528/