JTable Cell Editing!!
Hi,
I have a JTable having 5 columns. I have made all cells editable (except the last one) in the last column. The first column holds string data whereas rest of the columns hold float data. Now what I want is, when I enter some number in any of the last column's cell, the other cells in that row should automatically display some value. For e.g. if I enter '4' in 4th row of last column's cell then see the table below:
Col 1Col2Col3Col 4Col5
abc 202.5 0.125 4
.
.
.
.
--
Total500Sum(col3)Sum(Col4)Sum(Col5)
--
formula for col2 is (500*4)/100
formula for col3 is col2/8
formula for col4 is col2/20
The sums also should be calculated down below as shown.
[The values in cols 2,3,4 & sums should get calculated somewhere and displayed in the respective column's cell.]
The cells in column no. 2 to 4 display the value on the basis of some formula. For each column formula is as shown above and the all of these formulae take last column's corresponding cell value as input value.
Problem is where do I do the calculations [coz JTable does not have any direct listener] and show the results?
Any Takers?
THANKS IN ADVANCE
maheshmrd....
[1278 byte] By [
maheshmrd] at [2007-9-26 2:07:06]

hope this will work
import java.util.*;
import java.awt.*;
import java.awt.event.*;
import java.text.*;
import javax.swing.*;
import javax.swing.table.*;
import javax.swing.event.*;
class DecimalRenderer extends DefaultTableCellRenderer {
DecimalFormat formatter;
DecimalRenderer(String pattern) {
this(new DecimalFormat(pattern));
}
DecimalRenderer(DecimalFormat formatter) {
this.formatter = formatter;
setHorizontalAlignment(JLabel.RIGHT);
}
public void setValue(Object value) {
setText((value == null) ? ""
: formatter.format(((Double)value).doubleValue()));
}
}
public class TotalRowExample extends JFrame {
final private int TOTAL_ROW= 3;
final private int TOTAL_COLUMN = 1;
TotalRowExample() {
super( "Total Row Example" );
final DecimalFormat formatter = new DecimalFormat("###,##0.00");
DefaultTableModel dm = new DefaultTableModel() {
public void setValueAt(Object value, int row, int col) {
Vector rowVector = (Vector)dataVector.elementAt(row);
if (col == TOTAL_COLUMN) {
Double d = null;
if (value instanceof Double) {
d = (Double)value;
} else {
try {
d = new Double(
((Number)formatter.parse((String)value)).doubleValue());
} catch (ParseException ex) {
d = new Double(0.0);
}
}
rowVector.setElementAt(d, col);
} else {
rowVector.setElementAt(value, col);
}
}
public boolean isCellEditable(int row, int col) {
if (row == TOTAL_ROW) return false;
return true;
}
public Class getColumnClass(int col) {
if (col == TOTAL_COLUMN) return Number.class;
return String.class;
}
};
dm.setDataVector(
new Object[][]{
{"coffee",new Double(0.0)},
{"tea",new Double(0.0)},
{"cocoa" ,new Double(0.0)},
{"total" ,new Double(0.0)}},
new Object[]{"Item","Price"});
JTable table = new JTable( dm ) {
public void editingStopped(ChangeEvent e) {
super.editingStopped(e);
reCalcurate(getModel());
repaint();
}
};
table.getColumn("Price").setCellRenderer(
new DecimalRenderer(formatter));
JScrollPane scroll = new JScrollPane(table);
Container content = getContentPane();
content.add(scroll);
setSize( 300, 120 );
setVisible(true);
}
private void reCalcurate(TableModel ml) {
if (ml == null) return;
double total = 0.0;
for (int i=0;i<TOTAL_ROW;i++) {
total += ((Double)ml.getValueAt(i,TOTAL_COLUMN)).doubleValue();
}
ml.setValueAt(new Double(total),TOTAL_ROW,TOTAL_COLUMN);
}
public static void main(String[] args) {
TotalRowExample frame = new TotalRowExample();
frame.addWindowListener( new WindowAdapter() {
public void windowClosing( WindowEvent e ) {
System.exit(0);
}
});
}
}
>