0

我正在使用 Netbeans、SWING 将数据从 mysql 表获取到 jTable。我做到了,但是表格的标题。标题与 mysql 表中的相同。从数据库获取数据后如何更改jTable的标题?

附言

默认标题在此问题中不起作用。

这是自动生成的代码:

@SuppressWarnings("unchecked") // private void initComponents() { bindingGroup = new org.jdesktop.beansbinding.BindingGroup();

progressjournalPUEntityManager = java.beans.Beans.isDesignTime() ? null : javax.persistence.Persistence.createEntityManagerFactory("progressjournalPU").createEntityManager();
pupilsQuery = java.beans.Beans.isDesignTime() ? null : progressjournalPUEntityManager.createQuery("SELECT p FROM Pupils p");
pupilsList = java.beans.Beans.isDesignTime() ? java.util.Collections.emptyList() : pupilsQuery.getResultList();
jScrollPane1 = new javax.swing.JScrollPane();
jTable1 = new javax.swing.JTable();

setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
setTitle("Журнал успеваемости");
setResizable(false);

jTable1.setModel(new javax.swing.table.DefaultTableModel(
    new Object [][] {

    },
    new String [] {
        "#", "ФИО", "Курс", "Предмет", "Оценка"
    }
) {
    Class[] types = new Class [] {
        java.lang.Integer.class, java.lang.String.class, java.lang.String.class, java.lang.String.class, java.lang.String.class
    };

    public Class getColumnClass(int columnIndex) {
        return types [columnIndex];
    }
});

org.jdesktop.swingbinding.JTableBinding jTableBinding = org.jdesktop.swingbinding.SwingBindings.createJTableBinding(org.jdesktop.beansbinding.AutoBinding.UpdateStrategy.READ_WRITE, pupilsList, jTable1);
org.jdesktop.swingbinding.JTableBinding.ColumnBinding columnBinding = jTableBinding.addColumnBinding(org.jdesktop.beansbinding.ELProperty.create("${pupilMark}"));
columnBinding.setColumnName("Pupil Mark");
columnBinding.setColumnClass(String.class);
columnBinding = jTableBinding.addColumnBinding(org.jdesktop.beansbinding.ELProperty.create("${pupilSubject}"));
columnBinding.setColumnName("Pupil Subject");
columnBinding.setColumnClass(String.class);
columnBinding = jTableBinding.addColumnBinding(org.jdesktop.beansbinding.ELProperty.create("${pupilCourse}"));
columnBinding.setColumnName("Pupil Course");
columnBinding.setColumnClass(String.class);
columnBinding = jTableBinding.addColumnBinding(org.jdesktop.beansbinding.ELProperty.create("${pupilName}"));
columnBinding.setColumnName("Pupil Name");
columnBinding.setColumnClass(String.class);
columnBinding = jTableBinding.addColumnBinding(org.jdesktop.beansbinding.ELProperty.create("${pupilId}"));
columnBinding.setColumnName("Pupil Id");
columnBinding.setColumnClass(Integer.class);
bindingGroup.addBinding(jTableBinding);
jTableBinding.bind();
jScrollPane1.setViewportView(jTable1);

javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
getContentPane().setLayout(layout);
layout.setHorizontalGroup(
    layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
    .addGroup(layout.createSequentialGroup()
        .addGap(15, 15, 15)
        .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 616, Short.MAX_VALUE)
        .addContainerGap())
);
layout.setVerticalGroup(
    layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
    .addGroup(layout.createSequentialGroup()
        .addContainerGap()
        .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 275, javax.swing.GroupLayout.PREFERRED_SIZE)
        .addContainerGap(14, Short.MAX_VALUE))
);

bindingGroup.bind();

pack();

}//

并且此代码已锁定以进行编辑。

4

2 回答 2

1

调用getMetaData()你的 sql ResultSetResultSetMetaData包含列名。列标题名称(如行数据)从TableModel.

于 2012-04-26T09:33:45.453 回答
1

表的标题由 确定TableModel#getColumnName,正如该方法的 javadoc 中明确说明的那样:

返回 columnIndex 处的列名。这用于初始化表的列标题名称

于 2012-04-26T09:34:39.803 回答