Java从DB Netbeans获取数据后更改表头

时间:2012-04-26 09:22:51

标签: java swing jtable

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

P.S。

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

这是自动生成的代码:

@SuppressWarnings( “未登记”) // 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();

} //

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

2 个答案:

答案 0 :(得分:1)

在您的sql getMetaData()上调用ResultSetResultSetMetaData包含列名称。列标题名称(如行数据)从TableModel读取。

答案 1 :(得分:1)

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

  

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