Mam dodane 2 comboboxy, które mają być od siebie zależne. ComboBox1 wczytuje z bazy listę przedmiotów, a ComboBox2 ma wczytywać listę studentów po numerze indeksu przypisanych do danego studenta. Nie mam pojęcia jak mam odwołać się do ComboBoxa1.
Wyświetla nazwe przedmiotu w comboboxie
private void chooseSubject() {
String sqlSubjectName = "select nazwa from przedmioty";
Connection myConn = null;
try {
myConn = ConnectionManager.getConnection();
PreparedStatement ps = myConn.prepareStatement(sqlSubjectName);
ResultSet rs = ps.executeQuery(sqlSubjectName);
while (rs.next()) {
if (!subjectList.contains(rs.getString("nazwa"))) {
subjectList.add(rs.getString("nazwa"));
}
}
rs.close();
ps.close();
myConn.close();
} catch (SQLException ex) {
System.err.println("SQL ERR" + ex);
}
ComboBox1.setItems(subjectList);
}
wyświetla wszystkich studentów w comboboxie2 niezależnie od przedmiotu
private void chooseStudent() {
String sqlStudentName = "SELECT student.nr_indeksu FROM `przedmioty_studenci` Inner join student on przedmioty_studenci.id_studenta=student.id_studenta";
Connection myConn = null;
try {
myConn = ConnectionManager.getConnection();
PreparedStatement ps = myConn.prepareStatement(sqlStudentName);
ResultSet rs = ps.executeQuery(sqlStudentName);
while (rs.next()) {
if (!studentList.contains(rs.getString("nr_indeksu"))) {
studentList.add(rs.getString("nr_indeksu"));
}
}
rs.close();
ps.close();
myConn.close();
} catch (SQLException ex) {
System.err.println("SQL ERR" + ex);
}
ComboBox2.setItems(studentList);
}
Inicjalizacja
public void initialize(URL url, ResourceBundle rb) {
chooseSubject();
chooseStudent();
}