I have a JTable that retrieves information from a MySQL database table. The column headers are named just like how they are in the database.

Here is the code to create the JTable:

JScrollPane spBlockViewSchedule = new JScrollPane();
spBlockViewSchedule.setBounds(10, 285, 763, 185);
pnlBlockSched.add(spBlockViewSchedule);

tblBlockViewSchedule = new JTable();
spBlockViewSchedule.setViewportView(tblBlockViewSchedule);

Here is the code that populates the JTable:

private void populateTable(String sql, JTable table) {
        try {
            pst = DbConnection.conn.prepareStatement(sql);
            rs = pst.executeQuery();

        } catch(Exception ex) {
            ex.printStackTrace();
        }

        table.setModel(DbUtils.resultSetToTableModel(rs));
    }

How do I change the column names displayed in the JTable without changing the column names of the database table itself?

有帮助吗?

解决方案

Create an empty DefaultTableModel with code like:

String[] columnNames = {"Course Code", "Subject Code", "Year Level", ...};
DefaultTableModel model = new DefaultTableModel(columnNames, 0);

Then in the code where you read the data from the ResultSet you add the data to the TableMOdel using the addRow(....) method. Something like:

while (rs.next())
{
   Vector row = new Vector();

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

    model.addRow( row );
}

Finaly you create the table using:

JTable table = new JTable( model );

Edit:

Since you are using 3rd party code you either need to change the way you add data to the model. I gave you basic code above. You can see the Table From Database Example source code from Table From Database for a complete example.

Or, you can modify the column headers after the table is created with code like:

table.getColumn("course_code").setHeaderValue("Course Code");
...
table.repaint();

Edit 2:

You can get the TableColumn from the TableColumnModel:

TableColumnModel tcm = table.getTableColumnModel();
tcm.getColumn(0).setHeaderValue("Course Code");
...
table.repaint();

其他提示

I am currently facing the same problem as you did.

This line:

table.setModel(DbUtils.resultSetToTableModel(rs));

provided by r2xml.jar is pretty handy :)

I overcame the problem by setting alias to my sql SELECT statement. For example,

SELECT 
EngagementMethodID AS [ID],
EngagementMethodDescription AS [Engagement Method Description]

 FROM [STUDENT].[EngagementMethod]

This will populate the column header as ID and Engagement Method Description as wanted.

Hope it helps.. Despite the age of this question? haha... Just sharing

try this

       int colCount = 0;
       ResultSetMetaData rsMetaData = null;
        colCount = rsMetaData.getColumnCount();
        for (int k = 1; k <= colCount; k++) {
       { String columnName = null;

        columnName = rsMetaData.getColumnName(k);
        System.out.println(columnName);
        }

use this code simultaneously with your fetching code.

Each JTable has a TableModel. This TableModel defines the columns(Names and data types)

so find your table model and change it accordingly.

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top