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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T11:34:49+00:00 2026-06-11T11:34:49+00:00

I have Components I have created that are being put into a table model

  • 0

I have Components I have created that are being put into a table model with two columns as below.

if (!newAcList.isEmpty()) {
    for (Acronym acc : newAcList) {
        tableModel.addRow(new String[]{acc.getName(), acc.getDefinition()});

    }
}

What I need is when the user selects an item on the table model it converts the item back to my Acronym Object. I am using a Listselectionevent Listener.

Here is valueChanged selection event“

            @Override
            public void valueChanged(ListSelectionEvent e) {
                String selectedAcData = null;
                String selectDefData = null;

                int[] selectedRow = accTable.getSelectedRows();
                int[] selectedColumns = accTable.getSelectedColumns();

                for (int i = 0; i < selectedRow.length; i++) {
//                    for (int j = 0; j < selectedColumns.length; j++) {
                    selectedAcData = (String) accTable.getValueAt(selectedRow[i], 0);
                }
            }
  • 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-11T11:34:50+00:00Added an answer on June 11, 2026 at 11:34 am

    You might want to create a class that implements the TableModel interface for the acronyms. It might be called AcronymTableModel and is backed by a List<Acronym> list of Acronyms. Then give this model to your table.

    The call to accTable.getValueAt(selectedRow[i], 0); in your valueChanged method will then return an instance of an Acronym.

    Here’s a quick example.

        public class Example {
    
            public static void main(String [] a) {
                JFrame f = new JFrame();
    
                JPanel p = new JPanel();
    
                List<Acronym> acronyms = new ArrayList<Acronym>();
                acronyms.add(new Acronym("FBI", "Federal Bureau of Investigation"));
                acronyms.add(new Acronym("CIA", "Central Intelligence Agency"));
    
                final TableModel tModel = new AcronymTableModel(acronyms);
    
                JTable t = new JTable(tModel);
                t.getSelectionModel().addListSelectionListener(new ListSelectionListener() {
                    @Override
                    public void valueChanged(ListSelectionEvent e) {
                        Acronym a = (Acronym)tModel.getValueAt(e.getFirstIndex(), 0);
                        System.out.println(a.acronym + ": " + a.definition);
                    }});
    
                p.add(t);
    
                f.getContentPane().add(p);
    
                f.pack();
    
                f.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
    
                f.setVisible(true);
            }
    
    
        }
        class Acronym {
            String acronym;
            String definition;
    
            public Acronym(String a, String d) {
                acronym = a;
                definition = d;
            }
        }
        class AcronymTableModel implements TableModel {
    
            private List<Acronym> acronyms;
    
            public AcronymTableModel(List<Acronym> acs) {
                this.acronyms = new ArrayList<Acronym>(acs);
            }
    
            @Override
            public int getRowCount() {
                return this.acronyms.size();
            }
    
            @Override
            public int getColumnCount() {
                return 2;
            }
    
            @Override
            public String getColumnName(int columnIndex) {
                switch(columnIndex) {
                case 0:
                    return "Acronym";
                case 1:
                    return "Definition";
                }
    
                return null;
    
            }
    
            @Override
            public Class<?> getColumnClass(int columnIndex) {
                return String.class; // Since both columns are simply
            }
    
            @Override
            public boolean isCellEditable(int rowIndex, int columnIndex) {
                return false;
            }
    
            @Override
            public Object getValueAt(int rowIndex, int columnIndex) {
                return acronyms.get(rowIndex);
            }
    
            @Override
            public void setValueAt(Object aValue, int rowIndex, int columnIndex) {
            }
    
            @Override
            public void addTableModelListener(TableModelListener l) {
            }
    
            @Override
            public void removeTableModelListener(TableModelListener l) {
            }
        }
    

    The Java tutorials are always good and have good examples.
    http://docs.oracle.com/javase/tutorial/uiswing/events/listselectionlistener.html

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

Sidebar

Related Questions

I have created a form on which two components are present, button and progressbar
I have an application that consist of multiple components, each compiled into single DLL,
I have created a Hexagon component that extends JPanel . It draws a hexagon
I have created a plugin in that i included one component.. So in that
We have few components like libraries dlls When initially created I ran the following
I have created JTextpane and inserted components inside textpane (components like Jtextarea). (vertical scrollbar
On an embedded type system, I have created a Small Object Allocator that piggy
Using VB6, I have created an Outlook plugin, that has a property page. The
i have JavaScript components, that has following architecture: var MyComponent = function(params) { setup(params);
I have two components, a base Entity component: <cfcomponent persistent=true> <cfproperty name=Id fieldtype=id generator=native>

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.