按行选择编辑jtable内容

时间:2013-02-25 18:19:52

标签: java swing jframe jpanel jtextfield

我有一个具有Editbutton的类并将其设置为actionListener。 我写了我的全班代码。

我有3个班级,

public class UserPage extends JFrame implements ActionListener {

private AllUser userModel;
private JTable uTable;
JButton editButton;

public UserPage() {
    setTitle("All User Information");
    userModel = new AllUser();
    uTable = new JTable(userModel);
    add(new JScrollPane(uTable), BorderLayout.CENTER);
    add(buttonPanels(), BorderLayout.PAGE_START);
    this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    this.setSize(800, 600);
    this.setLocation(300, 60);
}

public final JPanel buttonPanels() {
    JPanel buttonsPanel = new JPanel(new FlowLayout(FlowLayout.LEFT));

    editButton=new JButton("Edit");

    editButton.addActionListener(this);

    buttonsPanel.add(editButton);

    return buttonsPanel;
}

@Override
public void actionPerformed(ActionEvent e) {
    if(e.getSource()== editButton){
        int selectedRow=uTable.getSelectedRow();
        if(selectedRow>0){
            editUser(selectedRow);

        }
        else{
            JOptionPane.showMessageDialog(null, "Select a row");
        }

    }
}

public static void main(String[] args) {
    new UserPage().setVisible(true);
}

public void editUser(int row){
    UserInformation userInf=new UserInformation();
    userModel.getSelectedMember(row);
    NewUserFrame_Edit edit = new NewUserFrame_Edit(userInf);
}
}

其他课程: 其他课程:

public class NewUserFrame_Edit extends javax.swing.JFrame {

public NewUserFrame_Edit( UserInformation userinf) {
    initComponents();
    setLocation(390, 20);

    jTextField1.setText(userinf.getFname());
    jTextField2.setText(userinf.getLname());

    if(userinf.getGender().equals("Male")){    
        jRadioButton1.setSelected(true);
    }
    else{
        jRadioButton2.setSelected(true);
    }

    jTextField3.setText(userinf.getDate());
    setVisible(true);
}

@SuppressWarnings("unchecked")
// <editor-fold defaultstate="collapsed" desc="Generated Code">                          
private void initComponents() {

    jLabel1 = new javax.swing.JLabel();
    jLabel2 = new javax.swing.JLabel();
    jLabel3 = new javax.swing.JLabel();
    jLabel4 = new javax.swing.JLabel();
    jTextField1 = new javax.swing.JTextField();
    jTextField2 = new javax.swing.JTextField();
    jTextField3 = new javax.swing.JTextField();
    jLabel5 = new javax.swing.JLabel();
    jRadioButton1 = new javax.swing.JRadioButton();
    jRadioButton2 = new javax.swing.JRadioButton();
    updateButton = new javax.swing.JButton();
    cancelButton = new javax.swing.JButton();

    setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);

    jLabel1.setFont(new java.awt.Font("Tahoma", 1, 14)); // NOI18N
    jLabel1.setForeground(new java.awt.Color(51, 0, 0));
    jLabel1.setText("Edit User");

    jLabel2.setFont(new java.awt.Font("Tahoma", 0, 14)); // NOI18N
    jLabel2.setText("First Name");

    jLabel3.setFont(new java.awt.Font("Tahoma", 0, 14)); // NOI18N
    jLabel3.setText("Last Name");

    jLabel4.setFont(new java.awt.Font("Tahoma", 0, 14)); // NOI18N
    jLabel4.setText("Date");

    jLabel5.setFont(new java.awt.Font("Tahoma", 0, 14)); // NOI18N
    jLabel5.setText("Gender");

    jRadioButton1.setText("Male");

    jRadioButton2.setText("Female");

    updateButton.setText("Update");
    updateButton.addActionListener(new java.awt.event.ActionListener() {
        public void actionPerformed(java.awt.event.ActionEvent evt) {
            updateButtonActionPerformed(evt);
        }
    });

    cancelButton.setText("Cancel");

    javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
    getContentPane().setLayout(layout);
    layout.setHorizontalGroup(
        layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
        .addGroup(layout.createSequentialGroup()
            .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                .addGroup(layout.createSequentialGroup()
                    .addGap(106, 106, 106)
                    .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                        .addComponent(jLabel2)
                        .addComponent(jLabel3)
                        .addComponent(jLabel5)
                        .addComponent(jLabel4))
                    .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                    .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                            .addComponent(jTextField2)
                            .addComponent(jTextField1, javax.swing.GroupLayout.PREFERRED_SIZE, 189, javax.swing.GroupLayout.PREFERRED_SIZE))
                        .addGroup(layout.createSequentialGroup()
                            .addGap(10, 10, 10)
                            .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                .addComponent(jRadioButton2)
                                .addComponent(jRadioButton1)))
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING, false)
                            .addGroup(layout.createSequentialGroup()
                                .addComponent(updateButton)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                                .addComponent(cancelButton))
                            .addComponent(jTextField3, javax.swing.GroupLayout.PREFERRED_SIZE, 219, javax.swing.GroupLayout.PREFERRED_SIZE))))
                .addGroup(layout.createSequentialGroup()
                    .addGap(292, 292, 292)
                    .addComponent(jLabel1)))
            .addContainerGap(258, Short.MAX_VALUE))
    );
    layout.setVerticalGroup(
        layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
        .addGroup(layout.createSequentialGroup()
            .addGap(21, 21, 21)
            .addComponent(jLabel1)
            .addGap(51, 51, 51)
            .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                .addComponent(jLabel2)
                .addComponent(jTextField1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
            .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
            .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                .addComponent(jLabel3)
                .addComponent(jTextField2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
            .addGap(32, 32, 32)
            .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                .addComponent(jLabel5)
                .addComponent(jRadioButton1))
            .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
            .addComponent(jRadioButton2)
            .addGap(35, 35, 35)
            .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                .addComponent(jLabel4)
                .addComponent(jTextField3, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
            .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 89, Short.MAX_VALUE)
            .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                .addComponent(updateButton)
                .addComponent(cancelButton))
            .addGap(52, 52, 52))
    );

    pack();
}// </editor-fold>                        

private void updateButtonActionPerformed(java.awt.event.ActionEvent evt) {                                             
   //Stil has not code, because first jtextfields should fill with old data

}                                            

public static void main(String args[]) {

    java.awt.EventQueue.invokeLater(new Runnable() {
        @Override
        public void run() {
            new NewUserFrame_Edit(new UserInformation());
        }
    });
}
// Variables declaration - do not modify                     
private javax.swing.JButton cancelButton;
private javax.swing.JLabel jLabel1;
private javax.swing.JLabel jLabel2;
private javax.swing.JLabel jLabel3;
private javax.swing.JLabel jLabel4;
private javax.swing.JLabel jLabel5;
private javax.swing.JRadioButton jRadioButton1;
private javax.swing.JRadioButton jRadioButton2;
private javax.swing.JTextField jTextField1;
private javax.swing.JTextField jTextField2;
private javax.swing.JTextField jTextField3;
private javax.swing.JButton updateButton;
// End of variables declaration                   
}

我喜欢当我使用按钮时,我的NewUserFrame_Edit类文本字段应该由旧数据填充。 但是这个例外很明显:

Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException
at myproject.Library.NewUserFrame_Edit.<init>(NewUserFrame_Edit.java:13)
at myproject.Library.NewUserFrame_Edit$2.run(NewUserFrame_Edit.java:148)
...

我的用户信息类:

public class UserInformation {

private String Fname;
private String Lname;
private String ID;
private String Gender;
private String Date;

public String getFname() {
    return Fname;
}

public void setFname(String fname) {
    this.Fname = fname;
}

public String getLname() {
    return Lname;
}

public void setLname(String lname) {
    this.Lname = lname;
}

public String getID() {
    return ID;
}

public void setID(String i_d) {
    this.ID = i_d;
}

public String getGender() {
    return Gender;
}

public void setGender(String gndr) {
    this.Gender = gndr;
}

public String getDate() {
    return Date;
}

public void setDate(String date) {
    this.Date = date;
}

@Override
public String toString() {
    return ID + "     " + Fname + "     "
            + Lname + "     " + Gender + "     " + Date + "\n";
}
}

2 个答案:

答案 0 :(得分:5)

很难说没有NewUserFrame_Edit类的整个代码,但堆栈跟踪清楚地表明第13行的内容是null

我猜第13行指的是

userinf.getGender().equals("Male")

userinf.getGender()会返回null,因此您无法在其上调用equalsgetGender返回null,因为您传递了一个您没有调用任何setter的对象:

public void editUser(int row){
  UserInformation userInf=new UserInformation();
  userModel.getSelectedMember(row);
  NewUserFrame_Edit edit = new NewUserFrame_Edit(userInf);
}

答案 1 :(得分:3)

正如@Robin所述(对他来说是+1),问题出在这个方法中:

public void editUser(int row)
{
    UserInformation userInf = new UserInformation();
    userModel.getSelectedMember(row);
    NewUserFrame_Edit edit = new NewUserFrame_Edit(userInf);
}

你应该替换:

UserInformation userInf = new UserInformation();

使用:

UserInformation userInf = userModel.getSelectedMember(row);

从所选行的表模型中获取数据。