This week's book giveaway is in the Android forum.
We're giving away four copies of Head First Android and have Dawn & David Griffiths on-line!
See this thread for details.
The moose likes Swing / AWT / SWT and the fly likes Jtable double with 2 decimal and data validation Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

Win a copy of Head First Android this week in the Android forum!
JavaRanch » Java Forums » Java » Swing / AWT / SWT
Bookmark "Jtable double with 2 decimal and data validation" Watch "Jtable double with 2 decimal and data validation" New topic

Jtable double with 2 decimal and data validation

iuliana iulia

Joined: Jul 20, 2010
Posts: 1
Hello, i tired to search how to solve my problem.
i want to can put only double numbers with two decimals in cells.

my code is :

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;

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

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) {

new DecimalRenderer(formatter));

JScrollPane scroll = new JScrollPane(table);
Container content = getContentPane();
setSize( 300, 120 );

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 ) {

Rob Camick
Ranch Hand

Joined: Jun 13, 2009
Posts: 2357
1) Edit your posting and delete all the code. Then add the code back in using the "Code tags" so the code retains its formatting and is readable.

i want to can put only double numbers with two decimals in cells.

a) first the getColumnClass() method should return Double.class, it you want to store a Double in the column. There is not need to override the setValueAt() method the editor will create the Double for you

b) Check out Table Format Renderers for some reusable renderers. The create a double renderer the code would be something like:

I agree. Here's the link:
subject: Jtable double with 2 decimal and data validation
It's not a secret anymore!