Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8575887
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T19:47:51+00:00 2026-06-11T19:47:51+00:00

I should tell this first, this is NOT about Rendering a Table cell. Here

  • 0

I should tell this first, this is NOT about Rendering a Table cell.

Here is the TableModel that i’m building using a 2D array based on a User object in my DB.

    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 problem is how can i get a User object back, using the selected row in the Table. Note that i can’t make a new User object and populate it with the row data. I must get the queried User object(objects in userList). So, is their any way to set a Object with a table row ?

Here 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];
    }

}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-11T19:47:52+00:00Added an answer on June 11, 2026 at 7:47 pm

    Instead of splitting the User object up before you create the model, add it directly to the model and allow the model to do the 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);
        }
    
    }
    

    This way, you should be able to do something like…

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

    Now when you need to…you can grab a the user that is represent at a specific row…

    User user = tiModel.getUserAt(rowIndex);
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Note: Not sure if this is the right stack, please tell if I should
First of all this is not just about redirecting. I'm trying to understand the
As far as I can tell, this code is fine, and should display some
So either I go back and tell someone that they should fix their JSON,
Is there a way to tell a JTable's row filter that it should update
I had this idea and my first reaction after having it was That's a
this my first question on here. I hope someone can help. It is to
Will you guys tell me whether or not this solution will work before I
This is my very first question so I am a bit nervous about it
Since this is answered I put the simplest answer here. (Thanks to Bergi) first

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.