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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T04:26:23+00:00 2026-06-17T04:26:23+00:00

I have three files, TopicData, TopicView, TopicTableModel. My program displays a table using values

  • 0

I have three files, TopicData, TopicView, TopicTableModel. My program displays a table using values from a database. Right now, when I click on a row, the index of the row is printed. I want to modify the code such that the topicID from my database is printed instead. The value of topicID is stored in the ArrayList but not displayed in the table, so I can’t use JTable.getValueAt().

Please advise on how to modify my codes. Thanks in advance.

More information:

  1. TopicData takes the data from the database and stores it in an ArrayList.

  2. The ArrayList is then passed to TopicTableModel where the data is made suitable to be displayed in JTable.

  3. TopicView creates a JTable and takes in the TopicTableModel to generate the JTable.

TopicData.java

public class TopicData {
int id;
String name; 
String date; 
String category;
String user;

public TopicData(){
}

public TopicData(int id, String name, String date, String category, String user) {
    this.id = id;
    this.name = name;
    this.date = date;
    this.category = category;
    this.user = user;
}



public TopicData(String name, String date, String category, String user) {
    this.name = name;
    this.date = date;
    this.category = category;
    this.user = user;
}

public int getId() {
    return id;
}

public void setId(int id) {
    this.id = id;
}

public String getName() {
    return name;
}

public void setName(String name) {
    this.name = name;
}

public String getDate() {
    return date;
}

public void setDate(String date) {
    this.date = date;
}

public String getCategory() {
    return category;
}

public void setCategory(String category) {
    this.category = category;
}

public String getUser() {
    return user;
}

public void setUser(String user) {
    this.user = user;
}


public ArrayList<TopicData> getTopicList(){
    ArrayList<TopicData> topicList = new ArrayList<TopicData>();
    ResultSet rs = null;
    DBController db = new DBController();
    db.setUp("myDatabase");
    String dbQuery = "SELECT topicID, topicName, topicDate, topicCategory, topicUser FROM topicTable ORDER BY topicDate";

    rs = db.readRequest(dbQuery);

    try{
        while(rs.next()){
            int id = rs.getInt("topicID");
            String name = rs.getString("topicName"); 
            String date = rs.getString("topicDate") ; 
            String category = rs.getString("topicCategory");
            String user = rs.getString("topicUser");

            TopicData topic = new TopicData (id, name, date, category, user);
            topicList.add(topic);
        }
    } catch (SQLException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
    db.terminate();
    return topicList;
}

TopicTableModel.java

public class TopicTableModel extends AbstractTableModel {

private static final long serialVersionUID = 1L;
private int rowCount, colCount;
private String[] columnNames = {"Name", "Date", "User"};
private Object [][] data;

public TopicTableModel(ArrayList<TopicData> listOfObjects) {
    rowCount = listOfObjects.size();
    colCount = columnNames.length;
    data = new Object[rowCount][colCount];

    for (int i = 0; i < rowCount; i++) {
        //Copy an ArrayList element to an instance of MyObject
        TopicData topic = (listOfObjects.get(i)); 
        data[i][0] = topic.getName();            
        data[i][1] = topic.getDate();
        data[i][2] = topic.getUser();
    }              
} 

@Override
public int getColumnCount() {
    // TODO Auto-generated method stub
    return colCount;
}

@Override
public int getRowCount() {
    // TODO Auto-generated method stub
    return rowCount;
}

@Override
public String getColumnName(int col) {
    return columnNames[col];
}

@Override
public Object getValueAt(int rowIndex, int columnIndex) {
    // TODO Auto-generated method stub
    return data[rowIndex][columnIndex];
}

@Override
public boolean isCellEditable(int rowIndex, int colIndex) {
    return false; //Disallow the editing of any cell
}

}

TopicView.java

private JTable getTable() {
    if (table == null) {
        TopicData topic= new TopicData();
        TopicTableModel tableModel = new TopicTableModel(topic.getTopicList());
        table = new JTable(tableModel);

        table.setShowGrid(false);
        table.setFillsViewportHeight(true);
        table.setBounds(173, 87, 456, 263);
        table.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
        table.getTableHeader().setReorderingAllowed(false);
        table.getTableHeader().setResizingAllowed(false);
        table.getColumnModel().getColumn(0).setPreferredWidth(500);

        ListSelectionModel rowSM = table.getSelectionModel();
        rowSM.addListSelectionListener(new ListSelectionListener() {
            public void valueChanged(ListSelectionEvent e) {
                //Ignore extra messages.
                if (e.getValueIsAdjusting()) return;

                ListSelectionModel lsm = (ListSelectionModel)e.getSource();
                if (lsm.isSelectionEmpty()) {
                    System.out.println("No rows are selected.");
                } 
                else {
                    int selectedRow = lsm.getMinSelectionIndex();
                    System.out.println("Row " + selectedRow + " is now selected.");
                }
            }
        });
    }
    return table;
}
  • 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-17T04:26:24+00:00Added an answer on June 17, 2026 at 4:26 am

    You actually have everything right under your nose.

    All you need to do is change your TableModel so that it keeps the ArrayList instead of transforming that list into an Object[][].

    Something like this (may have some typo issues):

    public class TopicTableModel extends AbstractTableModel {
    
    private static final long serialVersionUID = 1L;
    private int rowCount, colCount;
    private String[] columnNames = {"ID", "Name", "Date", "User"};
    private List<TopicData> listOfObjects;
    public TopicTableModel(ArrayList<TopicData> listOfObjects) {
        this.listOfObjects = listOfObjects;
    } 
    
    @Override
    public int getColumnCount() {
        return columnNames.length;
    }
    
    @Override
    public int getRowCount() {
        return listOfObjects.size();
    }
    
    @Override
    public String getColumnName(int col) {
        return columnNames[col];
    }
    
    @Override
    public Object getValueAt(int rowIndex, int columnIndex) {
        TopicData data = listOfObjects.get(rowIndex);
        switch(columnIndex) {
            case 0:
                return data.getId();
            case 1:
                return data.getName();
            case 2:
                return data.getDate();
            case 3:
                return data.getUser();
        }
        return null;
    }
    
    @Override
    public boolean isCellEditable(int rowIndex, int colIndex) {
        return false; //Disallow the editing of any cell
    }
    
    }
    

    Side effect: you will need to make your TopicData Serializable if you use Java serialization (or other technologies that take advantage of Serializable)

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

Sidebar

Related Questions

Suppose now I have three source files: ClassA.hpp, ClassB.hpp, and ClassC.hpp. ClassB inherits from
I have three files: movie.h (header); movie.cpp (implementation file); lab9.cpp (driver file using movie
I have three files: program.c , program.h and headers.h . program.c includes program.h and
Now, while I am developing my project I have three files, main.css, about.css and
I have three files, with import statements done in the following way: main.py from
I have three files; index.php, searchbar.php and search.php now when i have search.php show
I have three files. header.php index.php footer.php The header file contains from <html> to
I have three python files one.py , two.py , three.py one.py from one.py I
I have three .csv files that are output from saving a query in MS
I have three files: lib.c lib.h => They should be built as a .so

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.