Question

I am trying to populate an existing JTable in an existing JForm. All is well except the CellRenderer formatting (it formats to £currency) is not maintained once the table is passed back to the original JForm. The data is OK but the formatting fails.

However it is maintained throughout the populateTable_new2 routine.

I have the following routine:

public static void startFormPopulateResourcesTable2() throws
        SQLException, ParseException {
    String SQL;

    SQL = "SELECT tbl_deals.ID, tbl_stocks.stock_name AS Stock, ";
    SQL = SQL + "tbl_deals.purchase_date AS `Purchase Date`, ";
    SQL = SQL + "tbl_premium_types.premium_type AS Type, ";
    SQL = SQL + "tbl_deals.share_quantity AS Qty, ";
    SQL = SQL + "tbl_deals.total_cost_of_purchase AS `Total ";
    SQL = SQL + "Purchase Cost`, tbl_deals.sell_date AS ";
    SQL = SQL + "`Sale Date`, tbl_stocks.ID, ";
    SQL = SQL + "tbl_deals.purchase_share_price AS `Original ";
    SQL = SQL + "Share Price`, tbl_deals.target_share_price_today ";
    SQL = SQL + "AS `Target Price Today`, ";
    SQL = SQL + "tbl_deals.target_share_price_30_days AS `Target ";
    SQL = SQL + "Price 30 Days`, tbl_stocks.web_site_link, ";
    SQL = SQL + "tbl_stocks.forum_web_site_address FROM ";
    SQL = SQL + "tbl_premium_types INNER JOIN (tbl_stocks INNER ";
    SQL = SQL + "JOIN tbl_deals ON tbl_stocks.ID = ";
    SQL = SQL + "tbl_deals.stock_id) ON tbl_premium_types.ID = ";
    SQL = SQL + "tbl_stocks.premium_type_id ORDER BY ";
    SQL = SQL + "tbl_stocks.stock_name";
    theModules.FormMessages.setStart_forumWebSiteAddressColumnNumber(12);
    theModules.FormMessages.setStart_stockIDColumnNumber(7);
    theModules.FormMessages.setStart_WebSiteAddressListBoxColumn(11);

    int[] visibleColumnNumbers;
    visibleColumnNumbers = new int[5];
    visibleColumnNumbers[0] = 0;
    visibleColumnNumbers[1] = 6;
    visibleColumnNumbers[2] = 7;
    visibleColumnNumbers[3] = 11;
    visibleColumnNumbers[4] = 12;

    int[] currencyColumnNumbers;
    currencyColumnNumbers = new int[4];
    currencyColumnNumbers[0] = 5;
    currencyColumnNumbers[1] = 8;
    currencyColumnNumbers[2] = 9;
    currencyColumnNumbers[3] = 10;

    Statement stmt = DBase.Connect1.DoConnect();
    Admin.FormControllers.populateTable_new2(SQL,
            theModules.FormMessages.getStart_tblDeals(),
            visibleColumnNumbers, stmt, currencyColumnNumbers);

}

...which calls this one:

public static void populateTable_new2(String SQL, javax.swing.JTable thisTable, int[] invisibleColumnNumbers, Statement stmt, int[] currencyColumnNumbers) throws SQLException, ParseException {
    Statement thisSTMT;
    thisSTMT = DBase.Connect1.DoConnect();
    ResultSet rst;
    rst = thisSTMT.executeQuery(SQL);
    int theRowCount;
    DefaultTableModel thisDefaultTableModel;
    ResultSetMetaData meta = rst.getMetaData();
    int numberOfColumns = meta.getColumnCount();
    theRowCount
            = DBase.DatabaseValidation.countOfRecordsetRecordsWithUndefinedTable(SQL,
                    stmt);

    String[] theColumnName = new String[numberOfColumns];
    for (int n = 0; n < numberOfColumns; n++) {
        theColumnName[n] = meta.getColumnLabel(n + 1);
    }

    Object[][] theTableData = new Object[theRowCount][numberOfColumns];
    int n = 0;
    while (rst.next()) {

        for (int nn = 0; nn < numberOfColumns; nn++) {
            theTableData[n][nn] = rst.getString(nn + 1);
        }
        n = n + 1;
    }

    thisDefaultTableModel = new DefaultTableModel(theTableData,
            theColumnName);

    thisTable.setModel(thisDefaultTableModel);

    for (n = 0; n < thisTable.getColumnCount(); n++) {
        for (int nn = 0; nn < currencyColumnNumbers.length; nn++) {
            if (currencyColumnNumbers[nn] == n) {
                TableColumn thisColumn;
                thisColumn = thisTable.getColumnModel().getColumn(n);
                TableCellRenderer renderer = new FormatRenderer(NumberFormat.getCurrencyInstance());
                thisColumn.setCellRenderer(renderer);
            }
        }

        for (int nn = 0; nn < invisibleColumnNumbers.length; nn++) {
            if (invisibleColumnNumbers[nn] == n) {
                TableColumn thisColumn;
                thisColumn = thisTable.getColumnModel().getColumn(n);
                thisColumn.setMinWidth(0);
                thisColumn.setMaxWidth(0);
                thisColumn.setWidth(0);
                thisColumn.setPreferredWidth(0);
            }
        }

    }
}

Can you help me to ensure that the cell formatting still works once the table is passed back to the original JForm. Thanks

Was it helpful?

Solution

Whenever you create and add a new TableModel to a JTable a new TableColumnModel is created to match the new data, so any custom renderers that you have assigned to a TableColumn are lost.

However it is maintained throughout the populateTable_new2 routine.

In this routine you add the renderers to the TableColumn after add the TableModel to the table.

All is well except the CellRenderer formatting (it formats to £currency) is not maintained once the table is passed back to the original JForm.

Don't really understand this statement but I would guess you are once again creating adding a new model to the table but are not assigning the renderers to the TableColumns.

Or, assuming that the structure of you model is the same and only the data changes, then after the first time that you create the JTable with a TableModel you can use:

table.setAutoCreateColumnsFromModel(false);  

and the TableColumnModel will not be recreated every time you use a new model for the table.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top