Domanda

Sto cercando di popolare un jtable esistente in un jforme esistente.Tutto va bene tranne la formattazione della formattazione del cellrenderer (formati IT per valuta £) non viene mantenuto una volta che la tabella viene trasmessa alla JFORM originale.I dati sono OK ma la formattazione fallisce.

Tuttavia è mantenuto in tutta la routine Populatetable_new2.

Ho la seguente 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);

}
.

... che chiama questo:

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);
            }
        }

    }
}
.

Puoi aiutarmi a garantire che la formattazione delle cellule funzioni ancora una volta che la tabella viene ritornata all'originale JFORM.Grazie

È stato utile?

Soluzione

Ogni volta che si crea e aggiungi un nuovo tablemodel a un jtable è stato creato un nuovo tablecolumnModel per abbinare i nuovi dati, quindi eventuali rendering personalizzati che hai assegnato a un tablecolumn sono persi.

.

Tuttavia è mantenuto in tutta la routine Populatetable_new2.

In questa routine aggiungi i rendering al tablecolumn dopo aver aggiunto il tablemodel alla tabella.

.

Tutto va bene tranne la formattazione della formattazione del cellulare (formati IT per valuta £) non viene mantenuto una volta che la tabella viene trasmessa alla JFORM originale.

Non capire davvero questa affermazione, ma immagino che stai ancora creando aggiungendo un nuovo modello alla tabella ma non assegnano i rendering ai tablecolumns.

O, supponendo che la struttura del tuo modello sia la stessa e solo i dati cambiano, dopo la prima volta che si crea il jtable con un tablemodel è possibile utilizzare:

table.setAutoCreateColumnsFromModel(false);  
.

E il tablecolumnModel non verrà ricreato ogni volta che usi un nuovo modello per la tabella.

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top