• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
  • Campbell Ritchie
  • Bear Bibeault
  • Devaka Cooray
  • Liutauras Vilda
  • Jeanne Boyarsky
  • Knute Snortum
  • Junilu Lacar
  • paul wheaton
Saloon Keepers:
  • Ganesh Patekar
  • Frits Walraven
  • Tim Moores
  • Ron McLeod
  • Carey Brown
  • Stephan van Hulst
  • salvin francis
  • Tim Holloway

TableModel question  RSS feed

Ranch Hand
Posts: 476
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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++)
Ranch Hand
Posts: 168
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

makes both columns to be type Integer.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!