从数据库在 JavaFX 中填充 ComboBox
Filling ComboBox in JavaFX From DataBase
我有这个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());
}
}}
如您所见,它应该用数组列表中的数据填充组合框“filierecb”,但是当我执行代码时,什么也没有显示,组合为空,没有显示异常。
我看到你有一些不好的做法,例如我不知道为什么 static
是 ComboBox
,那么你必须使用而不是 ArrayList
它的界面List<"Content">
具有通用类型。我认为这个错误来自 filierecb = new JFXComboBox<>();
行,因为你有一个包含 ComboBox
的 .fxml
文件,所以你不必重新实例化 ComboBox
,另一个是你不必将 observableArrayList()
转换为 ObservableList
对我来说这段代码工作正常:
package Whosebug;
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="Whosebug.TestController">
<top>
<ComboBox fx:id="comboBox"/>
</top>
我看到你在使用 JFXComboBox
,但我认为这个问题没有任何区别。
我有这个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());
}
}}
如您所见,它应该用数组列表中的数据填充组合框“filierecb”,但是当我执行代码时,什么也没有显示,组合为空,没有显示异常。
我看到你有一些不好的做法,例如我不知道为什么 static
是 ComboBox
,那么你必须使用而不是 ArrayList
它的界面List<"Content">
具有通用类型。我认为这个错误来自 filierecb = new JFXComboBox<>();
行,因为你有一个包含 ComboBox
的 .fxml
文件,所以你不必重新实例化 ComboBox
,另一个是你不必将 observableArrayList()
转换为 ObservableList
对我来说这段代码工作正常:
package Whosebug;
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="Whosebug.TestController">
<top>
<ComboBox fx:id="comboBox"/>
</top>
我看到你在使用 JFXComboBox
,但我认为这个问题没有任何区别。