简体   繁体   中英

How to get the stored object from JTable

I have a JTable in which I can add users with several attributes like age, name, etc. This works and the users are added to my arraylist and JTable. Now what I want is when I choose the JTable row, to be able to get the object stored in the user's arrayList so that I can modify or delete them.

Here is the example of my code when I add users to the JTable:

private void jButtonAddAUserActionPerformed(java.awt.event.ActionEvent evt) {                                                
   User obj=new User();
   obj.setName(jTextFieldName.getText());
   obj.setAdress(jTextFieldAdress.getText());
   obj.setNumCC(Integer.parseInt(jTextFieldNumCC.getText()));
   obj.setTele(Integer.parseInt(jTextFieldTele.getText()));
   obj.setUserName(jTextFieldUserName.getText());
   obj.setPassword(jTextFieldPassword.getText());

   DefaultTableModel model=(DefaultTableModel) jTableUsers.getModel();
   model.addRow(new Object[]{
       jTextFieldName.getText(),
       jTextFieldAdress.getText(),
       jTextFieldTele.getText(),
       jTextFieldNumCC.getText(),
       obj.isAdmin
   });

   usersList.add(obj);
   JOptionPane.showMessageDialog(null,"Data inserted correctly.");
   jTextFieldName.setText("");
   jTextFieldAdress.setText("");
   jTextFieldNumCC.setText("");
   jTextFieldTele.setText("");
   jTextFieldPassword.setText("");
   jTextFieldUserName.setText("");
}   

Edit: Here is the code for removing users already working:

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

    DefaultTableModel model = (DefaultTableModel) jTableInvestidores.getModel();

    User u = userList.get(jTableUsers.getSelectedRow()); 
    userList.remove(u);
    model.removeRow(jTableUsers.getSelectedRow());
    JOptionPane.showMessageDialog(null,"Data removed.");

}  

And here is the code for updating user that is still not working, im trying to update it from the jTextFields:

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

    DefaultTableModel model = (DefaultTableModel) jTableUsers.getModel();
    userList.get(jTableUsers.getSelectedRow());
    model.setValueAt(jTextFieldName.getText(), jTableUsers.getSelectedRow(),0);
    model.setValueAt(jTextFieldAdress.getText(), jTableUsers.getSelectedRow(),1);
    model.setValueAt(jTextFieldPhone.getText(), jTableUsers.getSelectedRow(),2);
    model.setValueAt(jTextFieldNumCC.getText(), jTableUsers.getSelectedRow(),3);
    User u =userList.get(jTableUsers.getSelectedRow());

    JOptionPane.showMessageDialog(null,"Data updated.");

}   

Can anyone please give me some help on this? Thanks!

you could use something similar to this. sadly you didn't specify how you want to edit the user.

User u=userList.get(table.getSelectedRow());  //get user for editing
int location=table.getSelectedRow();  //get location in list to maintain order
userList.remove(u);  //remove selected user to edit variables
//modify user u
userList.add(location,u);  //insert user at previous location in list
model.setRowCount(0);  //reset table model
for (int i = 0; i < userList.size(); i++) {  //refill table model 
    User u = userList.get(i);  /7get user
    Vector<Object> vhelp = new Vector<>(); //create vector to store the values of the variables from user
    vhelp.add(/*your data*/);  // 1 add per variable that should be displayed in table
    model.addRow(vhelp); //add the data to the table model (fills the table with data)
}

your method should look like this:

    DefaultTableModel model = (DefaultTableModel) jTableUsers.getModel();
    User u = userList.get(jTableUsers.getSelectedRow());
    int location=jTableUsers.getSelectedRow();
    userList.remove(u);
    u.setName(jTextFieldName.getText());
    u.setAdress(jTextFieldAdress.getText());
    u.setNumCC(Integer.parseInt(jTextFieldNumCC.getText()));
    u.setTele(Integer.parseInt(jTextFieldTele.getText()));
    //u.isAdmin can't tell what this has to be
    userlist.add(location,u);

    model.setRowCount(0);  
    for (int i = 0; i < userList.size(); i++) {  
        User u = userList.get(i);  
        Vector<Object> vhelp = new Vector<>(); 
        vhelp.add(u.getName()); 
        vhelp.add(u.getAddress());
        vhelp.add(u.getTele());
        vhelp.add(u.getNumCC());
        vhelp.add(u.isAdmin);
        model.addRow(vhelp);
    }
    JOptionPane.showMessageDialog(null, "Data updated.");

the users are added to my arraylist and JTable.

Don't store the data in two separate places. The data should only be stored in the TableModel of the JTable .

So you can create a custom "User" object to contain the data about each user. Then you can create a custom TableModel to hold "User" object which can be displayed and access by the JTable .

Now what I want is when I choose the JTable row, to be able to get the object stored in the user's arrayList so that I can modify or delete them.

Check out Table Row Model for a step by step approach on create the custom TableModel . It contains all the methods you need to dynamically add, access and delete objects from the TableModel .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM