【问题标题】:JTable Row Header's Name Is The Name Of The Database Table Column [closed]JTable行标题的名称是数据库表列的名称[关闭]
【发布时间】:2015-12-29 03:06:42
【问题描述】:

当我制作表格时,(未硬编码的)Jtable 的行标题设置为

| Performance Title | Start Date | End Date | Start Time | End Time |

但是从数据库中获取数据后,变成了

| perf_title | perf_sdate | perf_edate | perf_stime | perf_etime |

这是来自数据库的列名..

这是我的代码:

    try{
       String qwe = "SELECT perf_title, perf_sdate, perf_edate, perf_stime, perf_etime FROM performances";
       rs=st.executeQuery(qwe);
       jTable3.setModel(DbUtils.resultSetToTableModel(rs));
    }

    catch (SQLException ex) {
        Logger.getLogger(MainFrame.class.getName()).log(Level.SEVERE, null, ex);
    }

帮助?

【问题讨论】:

  • 不要使用DbUtils 来构造TableModel,而是使用您已经创建的模型(使用您想要的列名)并填充它。更多详情请见How to Use Tables

标签: java mysql swing jtable


【解决方案1】:

基本代码如下:

//  Create an empty TableModel with just the column names

String[] columnNames = {"Performance Title", "Start Date", "...", "..."};
DefaultTableModel model = new DefaultTableModel(columnNames, 0)
{
    @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;
    }
};

//  Get each row of data from the ResultSet and add it to the TableModel

while (rs.next())
{
    Vector<Object> row = new Vector<Object>(columns);

    for (int i = 1; i <= columns; i++)
    {
        row.addElement( rs.getObject(i) );
    }

    model.addRow( row );
}

JTable table = new JTable( model );

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2019-04-28
    • 2011-08-19
    • 1970-01-01
    • 2010-10-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多