简体   繁体   English

JTable,Java中的列标题名称

[英]Column header name in JTable, Java

I am filling a JTable with data from a database. 我用数据库中的数据填充JTable。 I am have subclassed AbstractTableModel and passed the reference to the JTable. 我已经将AbstractTableModel子类化并传递了对JTable的引用。 However, how do I manage to extract the column names in the database and set them as "headers" in the JTable? 但是,如何在数据库中提取列名并在JTable中将它们设置为“标题”?

Thanks in advance. 提前致谢。

I already have this, in the console the header names display but I do not have a column in the GUI. 我已经有了这个,在控制台中显示了头名,但我在GUI中没有列。 I have attached the JTable to a tab in a tabbed pane. 我已将JTable附加到选项卡式窗格中的选项卡。

@Override
public String getColumnName(int column) {
    try {
        System.out.println(dbhandler.getMetaData().getColumnName(column + 1));
        return dbhandler.getMetaData().getColumnName(column + 1);
    } catch (SQLException e) {
        e.printStackTrace();
        return "";
    }
}

However, how do I manage to extract the column names in the database and set them as "headers" in the JTable? 但是,如何在数据库中提取列名并在JTable中将它们设置为“标题”?

Your table model needs to implement the getColumnName() method. 您的表模型需要实现getColumnName()方法。 Then you need to create your table model with data first. 然后,您需要首先使用数据创建表模型。 Then you create your JTable using the table model and the table will build the columns for you. 然后使用表模型创建JTable,表将为您构建列。

See Table from Database for code that provides a reusable table model as well as a method to populate the table for you automatically. 有关提供可重用表模型的代码以及自动为表填充表的方法,请参阅数据库中的表。

I do not have any columns at all, it is all rows... 我根本没有任何列,它是所有行...

You need to add the table to a JScrollPane (not a JPanel) and the headers will appear as the column header view of the scroll pane: 您需要将表添加到JScrollPane(而不是JPanel),标题将显示为滚动窗格的列标题视图:

JScrollPane scrollPane = new JScrollPane( table );

你可以通过覆盖AbstractTableModel的getColumnName(int i)方法来实现它,并将你的“头”字符串放在这个方法中。

How do you get your data? 你如何获得数据? With a "SELECT * FROM foo"? 使用“SELECT * FROM foo”?

Interface: java.sql.DatabaseMetaData 接口:java.sql.DatabaseMetaData

getColumns(String catalog, String schemaPattern, String tableNamePattern, String columnNamePattern) Retrieves a description of table columns available in the specified catalog. getColumns(String catalog,String schemaPattern,String tableNamePattern,String columnNamePattern)检索指定目录中可用的表列的说明。

你是否也覆盖了getColumnCount?

private void getColumnsFromDB (Connection connection, String tname) throws SQLException
{
    String query = "SELECT * FROM " + tname;
    Statement stmt = connection.createStatement ();
    ResultSet res = stmt.executeQuery (query);
    ResultSetMetaData rsmd = res.getMetaData ();
    int numberOfColumns = rsmd.getColumnCount ();
    boolean searchable = rsmd.isSearchable (1);
    if (searchable)
    {
        for (int j = 1; j <= numberOfColumns; ++j)
        {
            Column col = new Column (tname, rsmd, j);
            // do something with Column (col);
        }
    }
}

Class Column, Ctor: 类列,Ctor:

public Column (String t, ResultSetMetaData rsmd, int j) throws SQLException
{
    table = t;
    name = rsmd.getColumnName (j);
    schema = rsmd.getSchemaName (j);
    precision = -1;
    try
    {
        precision = rsmd.getPrecision (j);
    }
    catch (NumberFormatException nfe)
    {
        System.err.println ("nfe[gtd]: " + nfe + " " + t.getName () + "." + name);
    }
    scale = rsmd.getScale (j);
    catName = rsmd.getCatalogName (j);
    coltype = rsmd.getColumnType (j);
    coltypeName = rsmd.getColumnTypeName (j);
    int nulling = rsmd.isNullable (j);
    nullable = NULLTYP [nulling];
}

I hope it works, because this code is part of a much larger Codebase, and it's long ago I worked with it. 我希望它有效,因为这段代码是更大代码库的一部分,而且很久以前我就用它了。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM