gpt4 book ai didi

java - setText()、setSelectedItem()、getValueAt() 和 getSelectedRow() 不起作用

转载 作者:行者123 更新时间:2023-12-02 03:55:13 25 4
gpt4 key购买 nike

我无法理解为什么这段代码不起作用,下面是我尝试调用 JTable 上的单元格值的代码中的一个方法。单击该行时,代码会获取该行并使用该行中的信息,并使用该行中的信息填充 JTextBox。调试器没有帮助,因为它只指向第一个 getValueAt() 并将其称为 NullPointerException。

private void editInfoButtonActionPerformed(java.awt.event.ActionEvent evt) {                                               



System.out.println("Opening 'Edit Info' window.");

java.awt.EventQueue.invokeLater(new Runnable() {
public void run() {
new editPatientDialogBox().setVisible(true);
}


});

editPatientDialogBox.roomNumberField.setText(bedboard.patientInfoTable.getValueAt(bedboard.patientInfoTable.getSelectedRow(),0).toString());
editPatientDialogBox.nameField.setText(bedboard.patientInfoTable.getValueAt(bedboard.patientInfoTable.getSelectedRow(),1).toString());
editPatientDialogBox.genderField.setSelectedItem(bedboard.patientInfoTable.getValueAt(bedboard.patientInfoTable.getSelectedRow(),2).toString());
editPatientDialogBox.dateAdmittedField.setText(bedboard.patientInfoTable.getValueAt(bedboard.patientInfoTable.getSelectedRow(),3).toString());
editPatientDialogBox.ageField.setText(bedboard.patientInfoTable.getValueAt(bedboard.patientInfoTable.getSelectedRow(),4).toString());
editPatientDialogBox.isolationField.setSelectedItem(bedboard.patientInfoTable.getValueAt(bedboard.patientInfoTable.getSelectedRow(),5).toString())

这是它正在调用的类本身

public class editPatientDialogBox extends javax.swing.JFrame {

public editPatientDialogBox() {
initComponents();
}

@SuppressWarnings("unchecked")
private void initComponents() {

roomNumberField = new javax.swing.JTextField();
roomNumberLabel = new javax.swing.JLabel();
nameField = new javax.swing.JTextField();
nameLabel = new javax.swing.JLabel();
genderField = new javax.swing.JComboBox();
genderLabel = new javax.swing.JLabel();
acceptButton = new javax.swing.JButton();
isolationField = new javax.swing.JComboBox();
isolationLabel = new javax.swing.JLabel();
ageField = new javax.swing.JTextField();
dateAdmittedField = new javax.swing.JTextField();
cancelButton = new javax.swing.JButton();
dateAdmittedLabel = new javax.swing.JLabel();
ageLabel = new javax.swing.JLabel();

setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);

roomNumberLabel.setText("Room Number");

nameLabel.setText("Patient Name");

genderField.setMaximumRowCount(2);
genderField.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "Male", "Female" }));

genderLabel.setText("Gender");

acceptButton.setText("Accept");
acceptButton.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
acceptButtonActionPerformed(evt);
}
});

isolationField.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "Yes", "No" }));

isolationLabel.setText("Isolation");

ageField.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
ageFieldActionPerformed(evt);
}
});

dateAdmittedField.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
dateAdmittedFieldActionPerformed(evt);
}
});

cancelButton.setText("Cancel");
cancelButton.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
cancelButtonActionPerformed(evt);
}
});

dateAdmittedLabel.setText("Date Admitted (YYYY/MM/DD)");

ageLabel.setText("Date of Birth (YYYY/MM/DD)");


private void acceptButtonActionPerformed(java.awt.event.ActionEvent evt) {

if(bedboard.patientInfoTable.getValueAt(bedboard.patientInfoTable.getSelectedRow(),bedboard.patientInfoTable.getSelectedRow()) == null)
{
System.out.println("None selected, try again.");
}

else
{
String get = null;

info.setRoom(roomNumberField.getText());
info.setPatientName(nameField.getText());
info.setGender(genderField.getSelectedItem().toString());
info.setDateAd(dateAdmittedField.getText());
info.setAge(ageField.getText());
info.setIso(isolationField.getSelectedItem().toString());

setVisible(false);

System.out.println("*********************************************************************************************");
System.out.println("REVISED PATIENT");
System.out.println("Patient Name: " + nameField.getText());
System.out.println("Room Number: " + roomNumberField.getText());
System.out.println("Gender: " + genderField.getSelectedItem());
System.out.println("Date Admitted: " + dateAdmittedField.getText());
System.out.println("Age: " + ageField.getText());
System.out.println("Isolation: " + isolationField.getSelectedItem());
System.out.println("*********************************************************************************************");


bedboard.setValue(info.getRoom(get),bedboard.patientInfoTable.getSelectedRow(),0);
bedboard.setValue(info.getPatientName(get),bedboard.patientInfoTable.getSelectedRow(),1);
bedboard.setValue(info.getGender(get),bedboard.patientInfoTable.getSelectedRow(),2);
bedboard.setValue(info.getDateAd(get),bedboard.patientInfoTable.getSelectedRow(),3);
bedboard.setValue(info.getAge(get),bedboard.patientInfoTable.getSelectedRow(),4);
bedboard.setValue(info.getIso(get),bedboard.patientInfoTable.getSelectedRow(),5);
}
}

private void ageFieldActionPerformed(java.awt.event.ActionEvent evt) {

}

private void dateAdmittedFieldActionPerformed(java.awt.event.ActionEvent evt) {

}

private void cancelButtonActionPerformed(java.awt.event.ActionEvent evt) {

System.out.println("Revise patient canceled.");
setVisible(false);

}

/**
* @param args the command line arguments
*/
public static void main(String args[]) {
for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
if ("Nimbus".equals(info.getName())) {
javax.swing.UIManager.setLookAndFeel(info.getClassName());
break;
}
}
} catch (ClassNotFoundException ex) {
java.util.logging.Logger.getLogger(editPatientDialogBox.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (InstantiationException ex) {
java.util.logging.Logger.getLogger(editPatientDialogBox.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (IllegalAccessException ex) {
java.util.logging.Logger.getLogger(editPatientDialogBox.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (javax.swing.UnsupportedLookAndFeelException ex) {
java.util.logging.Logger.getLogger(editPatientDialogBox.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
}
//</editor-fold>



java.awt.EventQueue.invokeLater(new Runnable() {
public void run() {
new editPatientDialogBox().setVisible(true);

}
});
}

// Variables declaration - do not modify
private javax.swing.JButton acceptButton;
public static javax.swing.JTextField ageField;
private javax.swing.JLabel ageLabel;
private javax.swing.JButton cancelButton;
public static javax.swing.JTextField dateAdmittedField;
private javax.swing.JLabel dateAdmittedLabel;
public static javax.swing.JComboBox genderField;
private javax.swing.JLabel genderLabel;
public static javax.swing.JComboBox isolationField;
private javax.swing.JLabel isolationLabel;
public static javax.swing.JTextField nameField;
private javax.swing.JLabel nameLabel;
public static javax.swing.JTextField roomNumberField;
private javax.swing.JLabel roomNumberLabel;

// End of variables declaration

}

说实话,我不知道该怎么办。如果您需要更多信息,请评论

最佳答案

为什么使用invokeLater(...)???

该方法将代码添加到事件调度线程的末尾,这意味着它在所有 setText() 语句之后执行。摆脱invokeLater()。

此外,editPatientDialogBox 变量在哪里初始化。您的代码仅调用 new EditPatientDialogBox() 但不会将创建的对象分配给任何变量。

我希望代码应该是:

EditPatientDialogBox editPatiendDialogBox = new EditPatientDialogBox(); // note the capital "E" of the class name.
editPatientDialogBox.roomNumberField.setText(bedboard.patientInfoTable.getValueAt(bedboard.patientInfoTable.getSelectedRow(),0).toString());
...
editPatientDialogBox.setVisible(true);

此外,类名称应以大写字符开头。修正你的类(class)名称。

关于java - setText()、setSelectedItem()、getValueAt() 和 getSelectedRow() 不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35557568/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com