0

我有我创建的组件,它们被放入具有如下两列的表模型中。

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

    }
}

我需要的是当用户在表模型上选择一个项目时,它会将项目转换回我的首字母缩写词对象。我正在使用 Listselectionevent 侦听器。

这是 valueChanged 选择事件``

            @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);
                }
            }
4

1 回答 1

1

您可能希望创建一个为首字母缩略词实现 TableModel 接口的类。它可能被调用AcronymTableModel并由List<Acronym>首字母缩略词列表支持。然后把这个模型放到你的桌子上。

accTable.getValueAt(selectedRow[i], 0);然后,在您的方法中调用valueChanged将返回 Acronym 的一个实例。

这是一个简单的例子。

    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) {
        }
    }

Java 教程总是很好并且有很好的例子。 http://docs.oracle.com/javase/tutorial/uiswing/events/listselectionlistener.html

于 2012-09-17T20:09:34.660 回答