jTextField不根据jradiobutton选择更新值

时间:2017-03-12 11:49:22

标签: java sql jtextfield jradiobutton

我有2个jRadioButtons,jRadiobutton2和jRadioButton3。 我添加了对两者执行的操作以填充jTextfield

如果选择了jRadioButton2,则函数从数据库中获取最后一个ProfessorID,并将值+ 1设置为文本字段,例如,如果最后一个教授id = 1,则新值为2。

如果选择了jRadioButton3,则函数从数据库中获取最后一个StudentID,并将值+ 1设置为文本字段

对于两个jRadioButtons

,它都是相同的文本字段

我有2个函数LastStudentID()和LastProfessorID,每个函数都获取相应表的最后一个ID。

问题是它只在选择jRadioButton2时设置文本。如果选择了jRadioButton3,它没有,我试图打印它正确的新值,但它没有在texfield中设置它。

我的代码如下:

    public int LastStudentID() {
 String query = "SELECT IdStudent FROM student ORDER BY IdStudent DESC LIMIT 1";
 try {
  Statement stmt = conn.createStatement();
  ResultSet rs = stmt.executeQuery(query);
  while (rs.next()) {
   idstudent = (rs.getInt("IdStudent"));
  }
 } catch (Exception e) {
  System.out.println(e);
 }
 return idstudent;
}

public int LastProfessorID() {
 String query = "SELECT IdProfessor FROM professor ORDER BY IdProfessor DESC LIMIT 1";
 try {
  Statement stmt = conn.createStatement();
  ResultSet rs = stmt.executeQuery(query);
  while (rs.next()) {
   idprofessor = (rs.getInt("IdProfessor"));
  }
 } catch (Exception e) {
  System.out.println(e);
 }
 return idprofessor;
}

// ums is an instance of a class containing the functions
idstudent = ums.LastStudentID();
idprofessor = ums.LastProfessorID();

private void jRadioButton2ActionPerformed(java.awt.event.ActionEvent evt) {
 // TODO add your handling code here:
 if (jRadioButton2.isSelected()) {
  ID.setText(idprofessor + 1 + "");
 }
}

private void jRadioButton3ActionPerformed(java.awt.event.ActionEvent evt) {                                              
        // TODO add your handling code here:
        if(jRadioButton3.isSelected()){
            ID.setText(idstudent+1+"");
ID.setText(idstudent+1+"");
        }
}

This image shows the result of jRadionbutton2 selection (left part) and jRadioButton3 Selection (right Part). As you can see, the textfield is updated in when professor is selected, and not updated when the student is selected, although it's visible in the console the value 5

0 个答案:

没有答案