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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T13:17:32+00:00 2026-05-13T13:17:32+00:00

I have developed a basic custom JTableModel as follows public class CustomTableModel extends DefaultTableModel

  • 0

I have developed a basic custom JTableModel as follows

public class CustomTableModel extends DefaultTableModel {
  List<MyClass> data;
  public CustomTableModel(List<MyClass> data) {
    this.data = data;
  }

  public Class<?> getColumnClass(int columnIndex) {
    return MyClass.class;
  }

  public MyClass getValueAt(int rowIndex, int columnIndex) {
    return data.get(rowIndex);
  }

  // ...
}

I then use a basic custom JTableCellRenderer as follows

public class CustomTableCellRenderer extends JLabel implements TableCellRenderer {

  public Component getTableCellRendererComponent(JTable table, Object value,
        boolean isSelected, boolean hasFocus, int row, int column) {

    MyClass myClass = (MyClass)value;

    lbl.setText(myClass.getString());

    return this;
  }
}

I also have a custom JPanel that displays various information as follows

public class MyPanel extends JPanel {
  private MyClass myClass;

  public MyPanel(MyClass myClass) {
    // initialize components
  }

  public setMyClass(MyClass myClass) {
    this.myClass = myClass;
    updateFields();
  }

  private void updateFields() {
    this.fieldString.setText(myClass == null ? "" : myClass.getString());
    // ...
  }
}

Finally, I use a table to list my data and the custom panel to display the details of the selected data.

public class JCustomFrame extends JFrame {
  public JCustomFrame(List<MyClass> data) {
    // ...
    JTable table = new JTable(new CustomTableModel(data));
    table.setDefaultRenderer(MyClass.class, new CustomTableCellRenderer());

  }
}

What I am trying to accomplish is get the selected MyClass from the table regardless of sorting.

I tried ListSelectionListener but the methods do not return anything other than the selected indexes. Even if I have the index, if the table is sorted, my model is not so sophisticated and will return the wrong object.

  • 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-13T13:17:33+00:00Added an answer on May 13, 2026 at 1:17 pm

    …Even if I have the index, if the table is sorted, my model is not so sophisticated and will return the wrong object…

    You have to use:

    JTable.convertRowIndexToModel( int viewIndex )

    Maps the index of the row in terms of the view to the underlying TableModel. If the contents of the model are not sorted the model and view indices are the same.

    With that index you can access your table model and see what’s the object you need.

    Note Table sorting along with this method was introduced in Java 1.6

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

Sidebar

Ask A Question

Stats

  • Questions 433k
  • Answers 433k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer Try: UpdateModel(viewmodel.Recipient, new[] { "MobileNumber" }); Your problem is your… May 15, 2026 at 3:03 pm
  • Editorial Team
    Editorial Team added an answer Are you not able to just make use of normal… May 15, 2026 at 3:03 pm
  • Editorial Team
    Editorial Team added an answer You need Tomcat (or jetty, or any servlet container -… May 15, 2026 at 3:03 pm

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

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.