File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Swing / AWT / SWT and the fly likes TableModel question Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Swing / AWT / SWT
Bookmark "TableModel question" Watch "TableModel question" New topic

TableModel question

Alex Kravets
Ranch Hand

Joined: Jan 24, 2001
Posts: 476
I have a DynamicTableModel that I use for my JTable. In JTable I have two columns, one contains integers and other doubles. If I enter integer values for integer column everything is fine. But if I enter double values like 2.2 or 3.1 into double column, the edited cell gets highlighted in red and I can't edit any other cell until I enter and integer value into double column. Once I do that everything is fine. What's wrong here?
Here is the code:
import javax.swing.table.AbstractTableModel;
import java.util.*;
import javax.swing.table.*;
import java.text.*;
public class DynamicTableModel extends AbstractTableModel{
private Object[][] data;
private String[] columnNames;
private int[] editcol;
private DefaultTableModel tablemodel;
private int FIXED_NUM;
public DynamicTableModel(Object[][] data,String[] columnNames,int[] editcol, int FIXED_NUM){
this.editcol=new int[editcol.length];
for(int i=0;i<editcol.length;i++)
tablemodel=new DefaultTableModel(data,columnNames);
public Class getColumnClass( int column )
return Integer.class;
public int getColumnCount()
return columnNames.length;
public String getColumnName( int column )
return columnNames[column];
public int getRowCount()
return data.length - FIXED_NUM;
public boolean isCellEditable(int r,int c){
int pos=Arrays.binarySearch(editcol,c);
return true;
return false;
public Object getValueAt(int row, int column) {
return data[row][column];

public void setValueAt(Object value,int row,int col) {
if (value instanceof Double){
DecimalFormat twodigits=new DecimalFormat("0.00");
data[row][col] = twodigits.format(Double.parseDouble(value.toString()));
else if (value instanceof String)
data[row][col] = new Integer((String)value);
data[row][col] = new Integer((String)value.toString());
fireTableCellUpdated(row, col);
public void fillACell(Object value,int[] row,int col) {
for (int i=0;i<row.length;i++)
public void fillACell(double value,int row,int col) {
public void fillAColumn(Object value,int col) {
for (int i=0;i<getRowCount();i++)
public void fillAColumn(double value,int col) {
for (int i=0;i<getRowCount();i++)

All right brain, you don't like me and I don't like you, but let's just do this one thing so I can get back to killing you with beer.<br /> <br />- Homer Simpson
Michael Zalewski
Ranch Hand

Joined: Apr 23, 2002
Posts: 168

makes both columns to be type Integer.
I agree. Here's the link:
subject: TableModel question
It's not a secret anymore!