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);
                }
            }
yams
  • 942
  • 6
  • 27
  • 60
  • 1
    Why do you want to convert it? Have you tried adding a listener to the table get the selected index and reference the model based on the selected index? – km1 Sep 17 '12 at 18:30
  • I do have a listselectionevent listener on there. and I use selectedAcData = (String) jtable.getValueAt(selectedRow[i], 0); See when I created the table model I used the string value but I am at a loss to convert it back to the Acronym Object – yams Sep 17 '12 at 18:53

1 Answers1

1

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

km1
  • 2,383
  • 1
  • 22
  • 27
  • I guess I'm assuming too much here. I'm assuming the table is a table of Words with the acronyms. Two columns. One column for the word itself and another column for the acronym. – km1 Sep 17 '12 at 20:17
  • Actually the first is the Acronym which is a string and the second a sentance which is obviuosly a string. But could you give me an example of the Acc table model class. – yams Sep 17 '12 at 21:09
  • @Mark, I post an example. I made the table model final here just for the example. – km1 Sep 17 '12 at 21:46
  • Thanks I am working on it now. I really appreciate the help on this. – yams Sep 18 '12 at 16:20