简体   繁体   中英

Column Currency Format on DefaultTableModel

I'm trying to use a DefaultTableModel and place in a column format, but not make changes. How I can put Currency format in a column in a DefaultTableModel ?

String iSql = "SELECT cod_pro, name_pro, price_pro,stock_pro  from product";
String[] r = {"ID","Product","Price","Stock"};
String [] Data = new String[4];
model = new DefaultTableModel(null,r);
try{
    Statement jSt = jcn.Con().createStatement();
    ResultSet jRst = jSt.executeQuery(iSql);
    while(jRst.next()){
        Data[0] = jRst.getString("cod_pro");
        Data[1]= jRst.getString("name_pro");
        Data[2]= jRst.getString("price_pro");
        Data[3]= jRst.getString("stock_pro");
        modelo.addRow(Data);
    }
    tblProduct.setModel(model);
    TableColumnModel m = tblProduct.getColumnModel();
    m.getColumn(2).setCellRenderer(NumberRenderer.getCurrencyRenderer()); 
}
catch(SQLException ex)
{
    JOptionPane.showMessageDialog(null, "ERROR " + ex);
}

I really don`t know a method to do what you want but you could do a trick like this

Data[2]= "Currency"+jRst.getString("price_pro");

but when your retrieving data you have to split it or substring it

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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