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 7444423
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 29, 20262026-05-29T11:30:37+00:00 2026-05-29T11:30:37+00:00

I want to implement a TableModel, that will enable my existing data structure to

  • 0

I want to implement a TableModel, that will enable my existing data structure to be displayed by a JTable.
Say my data structure is a simple List<MyRow>, where MyRow is:

public class MyRow {
        String firstName;
        String secondName;      
}

My idea is that this class “should do its part” in being displayed, so i add it the following method, that simply maps each field to an index:

public Object getValueAt(int columnIndex) {
        switch (columnIndex) {
            case 0: return firstName;
            case 1: return secondName;
            default: return null;
        }
    }

At this point, the TableModel implementation will simply be:

public class MyModel extends AbstractTableModel {

    ArrayList<MyRow> list = new ArrayList<MyRow>();        

    public int getRowCount() {return list.size();}
    public int getColumnCount() {return 2;}
    public Object getValueAt(int rowIndex, int columnIndex) {
        return list.get(rowIndex).getValueAt(columnIndex);
    }
}

Neat, isn’t it? 🙂
Ok, but return 2; annoys me. In this “architecture” i have assigned to MyRow the rexposibility to represent a row, and so it should be MyRow that gives the number of columns. That because it provides the getValueAt(int columnIndex) method that is related to the number of columns: columnsIndex<columnCount.
This piece of information may be asked even if there are no rows in the List, so no instances of MyRow. So the method to add to MyRow should be a static method:

public class MyRow {
   ...
public static int getColumnCount() { return 2; }
   ...

And the getColumnCount() method of MyModel will be simply modified accordingly:

public int getColumnCount() {
    //return 2; //No more this *Encapsulation Violation*
    return MyRow.getColumnCount();
}

Now MyRow contains all the data needed to represent a row. It is easy to show that: adding, removing or changing columns is just question of modifying MyRow.
Clean, isn’t it? 🙂 🙂

The problem is that with static methods, i can’t provide an abstract super-class for MyRow.
This should be obvious, but to be sure:

public abstract class MyAbstractRow {
    public static int getColumnCount() { return 0; }
}

then

public class MyRow extends MyAbstractRow{
    String firstName;
    String secondName;
    public static int getColumnCount() { return 2; }
}

then i implement a “generic” MyData, that uses a MyAbstractRow:

public int getColumnCount() {
    //return 2; //No more this *Encapsulation Violation*
    return MyAbstractRow.getColumnCount();
}

This is resolved on the static type (so returns 0) because the getColumnCount of MyData doesn’t even have a reference to the type i actually want to use as my concrete implementation of MyAbstractRow.

How to solve this problem, mantaining the good level of encapsulation achieved so far?

  • 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-29T11:30:39+00:00Added an answer on May 29, 2026 at 11:30 am

    You are mixing model and view concepts. Your model MyRow should not know how it will be represented and so it should not know how many columns there are, what they contain, etc… This is why you run into this problem (using a static method to know the number of columns).

    One way to do this more cleanly is to have your TableModel return the number of columns and also implement the getValueAt by invoking the members of your MyRow class. Did you take a look at the default implementation (DefaultTableModel).

    Now I don’t understand why you need to override static methods (which is impossible). If you need to have different table models for different objects that inherit each other, then you might consider creating different table models that inherit each other in the same way.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Microsoft has announce that WindowsLiveID become a OpenID provider . I want implement it
I want to implement in Java a class for handling graph data structures. I
I want to implement a simple debug log, which consists of a table into
I want implement a program that can do a a Vision-based Page Segmentation. I
I wrote small service class and I want implement something like that : service
I would like to implement a JTable which will only be edited and updated
I want to implement caching of a list pulled from the db. I've had
I want to implement support of the XMLRPC protocol for my server that is
I want to implement a feature similar to the Related Questions list shown when
I want to bind a database table to a swing JTable, and make that

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.