无法用结果填充SummaryTable [英] Unable to populate SummaryTable with results

查看:99
本文介绍了无法用结果填充SummaryTable的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

    private void myProfileTabStateChanged(javax.swing.event.ChangeEvent evt) {                                          
    if (myProfileTab.getSelectedComponent() == EditProfile) {
        editProfile();
    } else if (SearchAcademic == myProfileTab.getSelectedComponent()) {
        AcademicDAO aDao = new AcademicDAO();
        try {
            List<AcademicDTO> listAll = aDao.listAll(AcademicDTO.class);
            searchTable.setData(listAll);
        } catch (DBException ex) {
            Logger.getLogger(MainMenu.class.getName()).log(Level.SEVERE, null, ex);
        }
    }
}  





public class ListDataUI<T extends BaseDTO> extends javax.swing.JPanel {

    public ListDataUI() {
        this.summaryColumnsAndTheirViewNames = Collections.emptyMap();
        this.dtoSummaryFields = Collections.emptyList();
        this.summaryTableModel = new SummaryTableModel();
        initComponents();
        this.summaryTable.setModel(summaryTableModel);
        initListeners();
    }

    /**
     * Creates new form ListDataUI
     */
    public ListDataUI(LinkedHashMap<String, String> summaryColumnsAndTheirViewNames) {
        this.summaryColumnsAndTheirViewNames = summaryColumnsAndTheirViewNames;
        this.dtoSummaryFields = new ArrayList<String>(summaryColumnsAndTheirViewNames.keySet());
        this.summaryTableModel = new SummaryTableModel();
        initComponents();
        this.summaryTable.setModel(summaryTableModel);
        initListeners();
    }

    public ListDataUI(List<String> dtoSummaryFields) {
        this.summaryColumnsAndTheirViewNames = Collections.emptyMap();
        this.dtoSummaryFields = new ArrayList<String>(dtoSummaryFields);
        this.summaryTableModel = new SummaryTableModel();
        initComponents();
        this.summaryTable.setModel(summaryTableModel);
        initListeners();
    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">                          
    private void initComponents() {

        tableSp = new javax.swing.JScrollPane();
        summaryTable = new javax.swing.JTable();

        setLayout(new java.awt.BorderLayout());

        summaryTable.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null}
            },
            new String [] {
                "Title 1", "Title 2", "Title 3", "Title 4"
            }
        ));
        tableSp.setViewportView(summaryTable);

        add(tableSp, java.awt.BorderLayout.CENTER);
    }// </editor-fold>                        
    // Variables declaration - do not modify                     
    private javax.swing.JTable summaryTable;
    private javax.swing.JScrollPane tableSp;
    // End of variables declaration                   
    private List<T> data;
    private Map<String, String> summaryColumnsAndTheirViewNames;
    private List<String> dtoSummaryFields;
    private SummaryTableModel summaryTableModel;

    public List<T> getData() {
        return data;
    }

    public void removeSelectedDataRow() {
        final int selectedRow = summaryTable.getSelectedRow();
        if (selectedRow != -1) {
            final int modelIndex = summaryTable.convertRowIndexToModel(selectedRow);
            data.remove(modelIndex);
            summaryTableModel.fireTableRowsDeleted(modelIndex, modelIndex);
        }
    }

    public void setData(List<T> data) {
        this.data = data;
        summaryTableModel.fireTableDataChanged();
        if (data.size() > 0) {
            summaryTable.getSelectionModel().setSelectionInterval(0, 0);
        }
    }

我正在尝试在表searchTable中显示数据库中的行数据,并向其中调用setData()方法.我在"searchTable.setData(listAll);"行设置了一个断点,listAll具有数据库中的所有数据,但不会显示在searchTable上.

I am trying to show the row data from database in the table searchTable , to which I invoke the setData() method. I set a breakpoint at the line `searchTable.setData(listAll);', listAll has all the data from database, but doesnt showup on searchTable.

推荐答案

问题不是在瞬间将JTable的模型连接到数据.因此,您的JTable及其TableModel完全不了解该List<T> data.

The problem is at not moment is the model of your JTable connected to your data. So your JTable and its TableModel have absolutely no knowledge of that List<T> data.

因此,基本上,您需要一个TableModel来接收您的列表,然后触发一个适当的TableModelEvent. TableModel应该使用很少的基本方法来指示如何访问数据以及显示哪些数据.

So basically you need to have a TableModel that receives your list and then fires an appropriate TableModelEvent. The TableModel should implement very few basic methods that indicate how to access the data and which data to display.

在下面找到这种实现的非常基本的示例(此处基于PersonList显示名字和姓氏).使其适应您的情况应该很简单:

Find below a very basic example of such implementation (here it is based on a List of Person to display first name and last name). It should be pretty straightforward to adapt this to your case:

import java.util.ArrayList;
import java.util.List;

import javax.swing.JFrame;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.SwingUtilities;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;
import javax.swing.event.TableModelEvent;
import javax.swing.table.AbstractTableModel;

public class TestListTableModel {

    class MyTableModel extends AbstractTableModel {

        private List<Person> baseModel;

        public MyTableModel() {
            baseModel = new ArrayList<TestListTableModel.Person>();
        }

        public MyTableModel(List<Person> baseModel) {
            super();
            this.baseModel = new ArrayList<Person>(baseModel);
        }

        @Override
        public int getRowCount() {
            return baseModel.size();
        }

        @Override
        public String getColumnName(int column) {
            switch (column) {
            case 0:
                return "First Name";
            case 1:
                return "Last Name";
            }
            return null;
        }

        @Override
        public int getColumnCount() {
            return 2;
        }

        @Override
        public Object getValueAt(int rowIndex, int columnIndex) {
            switch (columnIndex) {
            case 0:
                return getPersonAtIndex(rowIndex).getFirstName();
            case 1:
                return getPersonAtIndex(rowIndex).getLastName();
            }
            return null;
        }

        public Person getPersonAtIndex(int rowIndex) {
            return baseModel.get(rowIndex);
        }

        public int getIndexOfPerson(Person person) {
            return baseModel.indexOf(person);
        }

        public void addPerson(Person person) {
            baseModel.add(person);
            fireTableRowsInserted(baseModel.size() - 1, baseModel.size() - 1);
        }

        public void removePerson(Person person) {
            int removed = baseModel.indexOf(person);
            if (removed > -1) {
                baseModel.remove(removed);
                fireTableRowsDeleted(removed, removed);
            }
        }

        public void setBaseModel(List<Person> baseModel) {
            this.baseModel = baseModel;
            fireTableChanged(new TableModelEvent(this));
        }

    }

    protected void initUI() {
        List<Person> personModel = new ArrayList<TestListTableModel.Person>();
        personModel.add(new Person("John", "Smith"));
        personModel.add(new Person("Peter", "Donoghan"));
        personModel.add(new Person("Amy", "Peterson"));
        personModel.add(new Person("David", "Anderson"));
        JFrame frame = new JFrame(TestListTableModel.class.getSimpleName());
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        MyTableModel tableModel = new MyTableModel();
        JTable table = new JTable(tableModel);
        frame.add(new JScrollPane(table));
        frame.pack();
        frame.setVisible(true);
        tableModel.setBaseModel(personModel);
    }

    public class Person {
        private final String firstName;
        private final String lastName;

        public Person(String firstName, String lastName) {
            this.firstName = firstName;
            this.lastName = lastName;
        }

        public String getFirstName() {
            return firstName;
        }

        public String getLastName() {
            return lastName;
        }
    }

    public static void main(String[] args) throws ClassNotFoundException, InstantiationException, IllegalAccessException,
            UnsupportedLookAndFeelException {
        UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
        SwingUtilities.invokeLater(new Runnable() {

            @Override
            public void run() {
                new TestListTableModel().initUI();
            }
        });
    }

}

这篇关于无法用结果填充SummaryTable的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆