0

I have 6 columns and a maximum of about 20 rows in the database. I like to make a general program in Java that populates the JTable with these values. I am very much conversant with using dynamic arrays, but not familiar with JTable attributes and model in Swing.

Can anyone guide me here on tho requirement? Thanks

3

3 Answers 3

2

You have to create your own table model. See the API documentation here: http://docs.oracle.com/javase/7/docs/api/javax/swing/table/TableModel.html

Basically, getColumnCount() would return your number of columns, getRowCount() the number of rows in your database, getColumnName(int columnIndex) some name for every column (the column name from the database or an arbitrary name, maybe from a constant string array). getColumnClass(int columnIndex) can, in the simple case, return String.class for every column. Then you have to convert every value to string.

getValueAt(int rowIndex, int columnIndex) has to return the value from the database for the given row and column. You should probably pre-load all these values in a 2D array or something like this (but that would be the answer to another question ;) ).

You can ignore the other methods for now, they are for editable tables.

Your code could look something like this:

String[][] tableData = readTableDataFromDatabase();
String columnNames = initColumnNames(); //From DB or constants
TableModel model = new DbTableModel(tableData, columnNames);

class DbTableModel extends AbstractTableModel {
    private String[][] tableData;
    private String[] columnNames;

    public DbTableModel(String[][] tableData, columnNames) {
        this.tableData = tableData;
        this.columnNames = columnNames;
    }
    @Override
    public Object getValueAt(int rowIndex, int columnIndex) {
        return tableData[rowIndex][columnIndex];
    }
    @Override
    public int getRowCount() {
        return tableData.length;
    }
    @Override
    public int getColumnCount() {
        if(tableData.length == 0)
            return 0;
        return tableData[0].length;
    }
    @Override
    public String getColumnName(int column) {
        return columnNames[column]
    }
}

This example assumes that you read the data from the database as a 2D array. If you use an O/R-Mapper (like the Java Persistence API - JPA), which I highly recommend, you would probably load a list of Entities. Each entity would contain the data for a table row. Anyway, the table model would not change much. Instead of accessing array values, you would get an entity object from the list and call "get"-Methods on the Entity.

There's more information in the Java tutorial: http://docs.oracle.com/javase/tutorial/uiswing/components/table.html#data

Sign up to request clarification or add additional context in comments.

3 Comments

Some nice advice, be nice if you can back it up with a simple example. It'd be would also be nice if you could reference the Java 7 API ;)
Please link to the current Javadoc version.
I added an example and linked to the recent JavaDoc. Thanks for the hints!
0

Nice but i hope readTableDataFromDatabase(); return resultset object

Comments

0

Have a look at the tutorial here: http://docs.oracle.com/javase/tutorial/uiswing/components/table.html#data

Here's a sample of a List-based read-only TableModel I wrote some time ago:

https://sourceforge.net/p/puces-samples/code/HEAD/tree/tags/sessionstate-1.0/sessionstate-suite/sessionstate-sample/src/blogspot/puce/sessionstate/sample/ParticipantTableModel.java

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.