如何将ArrayList数据从数据库绑定到JTable中的JComboBox?

时间:2018-11-11 17:10:37

标签: java database swing jtable jcombobox

我已经成功地从数据库中获取数据到JTable中。但是我在填充JComboBox并将其添加到JTable时遇到了一些麻烦。就像我已经将ArrayList以String格式存储在Documents Column中一样。现在,我希望使用JTable中的ArrayList在Documents Column的每一行中都有一个JComboBox。

基本上,用该Array项将ArrayList字符串替换为JComboBox。

在“文档”列中,数据采用数组字符串的形式:

https://i.stack.imgur.com/GsyNN.png

  

Jtable代码:

private static final DatabaseConnections databaseConnections = new DatabaseConnections();
private Thread thread; 

Vector<Object> columnNames = new Vector<Object>();
Vector<Object> data = new Vector<Object>();

public ConnectionRecordsPanel() {
    initComponents();
    SetData();
}

private void SetData(){
    columnNames.addElement("Customer Id");
    columnNames.addElement("Name");
    columnNames.addElement("DOB");
    columnNames.addElement("Gender");
    columnNames.addElement("Current Address");
    columnNames.addElement("Permanent Address");
    columnNames.addElement("Mobile Number");
    columnNames.addElement("Email Id");
    columnNames.addElement("Documents");
    columnNames.addElement("Gas Type");
    columnNames.addElement("Status");


    thread = new Thread(new Runnable() {
        @Override
        public void run() {
            try {
                System.out.println("Inside run");
                data = databaseConnections.getData(data);
                System.out.println("Inside run");
                DefaultTableModel model = new DefaultTableModel(data, columnNames)
                {
                    @Override
                    public Class getColumnClass(int column)
                    {
                        for (int row = 0; row < getRowCount(); row++)
                        {
                            Object o = getValueAt(row, column);

                            if (o != null)
                            {
                                return o.getClass();
                            }
                        }
                        return Object.class;
                    }
                };
                jTable1.setModel(model);

                JComboBox jcb = new JComboBox();
                jTable1.getColumnModel().getColumn(9).setCellEditor(new DefaultCellEditor(jcb));
            } catch (ParseException ex) {
                Logger.getLogger(ConnectionRecordsPanel.class.getName()).log(Level.SEVERE, null, ex);
            }
        }
    });
    thread.start();
}
  

从数据库读取

public Vector getData(Vector data) throws ParseException{
    try {
        resultSet = statement.executeQuery("select * from connection_records");
        while (resultSet.next()){
            Vector<Object> row = new Vector<Object>();

            for (int i = 1; i <= 11 ; i++)
            {
                row.addElement(resultSet.getString(i));
            }
            data.addElement( row );
        }
    } catch (SQLException ex) {
        Logger.getLogger(DatabaseConnections.class.getName()).log(Level.SEVERE, null, ex);
    }
    return data;
}

0 个答案:

没有答案