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

  • Home
  • SEARCH
  • 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 7411005
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 29, 20262026-05-29T06:19:12+00:00 2026-05-29T06:19:12+00:00

I have a JTable populated with a custom DataModel (pasted below) and when I

  • 0

I have a JTable populated with a custom DataModel (pasted below) and when I call the populate() method, it appears to populate the table with duplicate data – each row is filled with the same value over and over again. However, on closer inspection (by simply println()ing the ‘data’ field), the data model isn’t at fault – it holds correct data, in the format I expect. What gives?

import java.util.ArrayList;    
import javax.swing.table.AbstractTableModel;

@SuppressWarnings("serial") // we don't expect this app to ever use serialized classes.  EVER.
public class CollectionDataModel extends AbstractTableModel {
    private ArrayList<ArrayList<String>> data;

    public CollectionDataModel() {
        data = new ArrayList<ArrayList<String>>();
    }

    @Override
    public int getColumnCount() {
        if(data.isEmpty()) return 0;
        return data.get(0).size();
    }

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

    @Override
    public Object getValueAt(int rowIndex, int columnIndex) {
        if(rowIndex > getRowCount()) return null;
        if(columnIndex > getColumnCount()) return null;
        return data.get(rowIndex).get(columnIndex);
    }

    public void populate(Collection c) {
        data.clear();
        for(Item i : c.getItems()) {
            ArrayList<String> row = new ArrayList<String>();
            for(Property p : i.getProperties().values()) {
                row.add(p.toString());
            }
            data.add(row);
        }
        fireTableDataChanged();
    }

}
  • 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-05-29T06:19:13+00:00Added an answer on May 29, 2026 at 6:19 am

    Here’s a complete example that may prove helpful. As the sample Map is unmodifiable, I refer you to @mKorbel’s example on how to override isCellEditable() and setValueAt().

    import java.awt.EventQueue;
    import java.awt.GridLayout;
    import java.util.Map;
    import javax.swing.JFrame;
    import javax.swing.JPanel;
    import javax.swing.JScrollPane;
    import javax.swing.JTable;
    import javax.swing.table.AbstractTableModel;
    
    /** @see https://stackoverflow.com/questions/9132987 */
    public class EnvTableTest extends JPanel {
    
        public EnvTableTest() {
            this.setLayout(new GridLayout());
            this.add(new JScrollPane(new JTable(new EnvDataModel())));
        }
    
        private static class EnvDataModel extends AbstractTableModel {
    
            private Map<String, String> data = System.getenv();
            private String[] keys;
    
            public EnvDataModel() {
                keys = data.keySet().toArray(new String[data.size()]);
            }
    
            @Override
            public String getColumnName(int col) {
                if (col == 0) {
                    return "Key";
                } else {
                    return "Value";
                }
            }
    
            @Override
            public int getColumnCount() {
                return 2;
            }
    
            @Override
            public int getRowCount() {
                return data.size();
            }
    
            @Override
            public Object getValueAt(int row, int col) {
                if (col == 0) {
                    return keys[row];
                } else {
                    return data.get(keys[row]);
                }
            }
        }
    
        private void display() {
            JFrame f = new JFrame("EnvTableTest");
            f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            f.add(this);
            f.pack();
            f.setLocationRelativeTo(null);
            f.setVisible(true);
        }
    
        public static void main(String[] args) {
            EventQueue.invokeLater(new Runnable() {
    
                @Override
                public void run() {
                    new EnvTableTest().display();
                }
            });
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a JTable that is populated using a custom TableModel I created. I
I have a jtable, everything works fine if I set the table data as
I have a JTable which is loaded from a data-structure using table model.The data-structure
I have a JTable with a custom TableModel called DataTableModel . I initialized the
I have a JTable inside of a JScrollPane . I am creating a custom
I have a JTable that I want to use to display some data (a
I have a jTable and a jButton. When clicked, the button's actionPerformed method calls
I have a Jtable on which I called the method table1.setAutoCreateRowSorter(true); . So this
Suppose you have a JTable and for each cell you want to display three
The problem is quite basic. I have a JTable showing cached data from a

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.