Java – how to set a custom object in the JTable line

I should first tell it that this is not about rendering table cells

This is the tablemodel I'm building using a 2D array based on user objects in my database

List<User> userList = userManagerService.getAllUsers();

    /* String[] col_user = {"Username","Name","Phone",.... } */
    String[][] data = new String[userList.size()][col_user.length];
    int i = 0;
    for (User user : userList) {
        String[] userdata = new String[col_user.length];
        userdata[0] = user.getUserUsername();
        userdata[1] = user.getUserName();
        userdata[2] = user.getUserPhone();
        userdata[3] = user.getUserNic();
        userdata[4] = user.getUserAddress();
        userdata[5] = user.getUserEmail();

        data[i++] = userdata;
    }

    VstTableItemModel tiModel = new VstTableItemModel(data,col_user);
    dataTable.setModel(tiModel);

My question is how to use the selected row in the table to return the user object Note that I cannot create a new user object and populate it with row data I have to get the user object of the query (the object in userlist) So, what is their method of setting objects with table rows?

This is my vsttableitemmodel class

public class VstTableItemModel extends AbstractTableModel {

    ArrayList<Object[]> data;
    String[] header;

    public VstTableItemModel(Object[][] obj,String[] header) {
        this.header = header;
        data = new ArrayList<Object[]>();

        for (int i = 0; i < obj.length; ++i) {
            data.add(obj[i]);
        }
    }

    @Override
    public int getRowCount() {
        return data.size();
    }

    @Override
    public int getColumnCount() {
        return header.length;
    }

    @Override
    public Object getValueAt(int rowIndex,int columnIndex) {
        return data.get(rowIndex)[columnIndex];
    }

    @Override
    public String getColumnName(int index) {
        return header[index];
    }

}

Solution

Before creating the model, do not split the user object, but add it directly to the model and allow the model to work for you

for example

public class VstTableItemModel extends AbstractTableModel {

    private List<User> users;

    public VstTableItemModel(List<User> users) {

        this.users = new ArrayList<User>(users);

    }

    @Override
    public int getRowCount() {
        return users.size();
    }

    @Override
    public int getColumnCount() {
        return 6;
    }

    @Override
    public Object getValueAt(int rowIndex,int columnIndex) {

        Object value = "??";
        User user = users.get(rowIndex);
        switch (columnIndex) {
            case 0:
                value = user.getUserUsername();
                break;
            case 1:
                value = user.getUserName();
                break;
            case 2:
                value = user.getUserPhone();
                break;
            case 3:
                value = user.getUserNic();
                break;
            case 4:
                value = user.getUserAddress();
                break;
            case 5:
                value = user.getUserEmail();
                break;
        }

        return value;

    }

    @Override
    public Class<?> getColumnClass(int columnIndex) {
        return // Return the class that best represents the column...
    }

    /* Override this if you want the values to be editable...
    @Override
    public void setValueAt(Object aValue,int rowIndex,int columnIndex) {
        //....
    }
    */

    /**
     * This will return the user at the specified row...
     * @param row
     * @return 
     */
    public User getUserAt(int row) {
        return users.get(row);
    }

}

In this way, you should be able to do something like

List<User> userList = userManagerService.getAllUsers();
VstTableItemModel tiModel = new VstTableItemModel(userList);

Now, when you need... You can get the user representing a specific row

User user = tiModel.getUserAt(rowIndex);
The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>