hi friends
as i am new to swings and Jtable pls help me if u can.
in the code copied below when i start putting value into the Double column
a ( .0 ) is appended at the end which is undesirable it should be like that only when the value is
something like 22.99 and also i want to limit the double value's fractional part to be limited to 2 digits only.
if this can be done in my Table model itself so that it will work everywhere i use this model
it will be really great !!!!!
if anyone can send a sample code for achieving this it would be of great help
import java.awt.*;
import javax.swing.*;
import javax.swing.event.*;
import javax.swing.table.*;
import java.text.*;
public class TableProcessing extends JFrame implements TableModelListener
{
JTable table;
public TableProcessing()
{
String[] columnNames = {"Item", "Quantity", "Price", "Cost"};
Object[][] data =
{
{"Bread", new Integer(1), new Double(1.11), new Double(1.11)},
{"Milk", new Integer(1), new Double(2.22), new Double(2.22)},
{"Tea", new Integer(1), new Double(3.33), new Double(3.33)},
{"Cofee", new Integer(1), new Double(4.44), new Double(4.44)}
};
DefaultTableModel model = new DefaultTableModel(data, columnNames);
model.addTableModelListener( this );
//
table = new JTable( model )
{
// Returning the Class of each column will allow different
// renderers to be used based on Class
public Class getColumnClass(int column)
{
return getValueAt(0, column).getClass();
}
// The Cost is not editable
public boolean isCellEditable(int row, int column)
{
if (column == 3)
return false;
else
return true;
}
};
table.setPreferredScrollableViewportSize(table.getPreferredSize());
//
JScrollPane scrollPane = new JScrollPane( table );
getContentPane().add( scrollPane );
}
/*
* The cost is recalculated whenever the quantity or price is changed
*/
public void tableChanged(TableModelEvent e)
{
if (e.getType() == TableModelEvent.UPDATE)
{
int row = e.getFirstRow();
int column = e.getColumn();
if (column == 1 || column == 2)
{
int quantity = ((Integer)table.getValueAt(row, 1)).intValue();
double price = ((Double)table.getValueAt(row, 2)).doubleValue();
Double value = new Double(quantity * price);
table.setValueAt(value, row, 3);
}
}
}
public static void main(String[] args)
{
TableProcessing frame = new TableProcessing();
frame.setDefaultCloseOperation( EXIT_ON_CLOSE );
frame.pack();
frame.setLocationRelativeTo( null );
frame.setVisible(true);
}
}
thank in advance
jags