• 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 Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

TableModel question

 
Ranch Hand
Posts: 476
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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){
Arrays.sort(editcol);
this.data=data;
this.columnNames=columnNames;
this.editcol=new int[editcol.length];
for(int i=0;i<editcol.length;i++)
this.editcol[i]=editcol[i];
tablemodel=new DefaultTableModel(data,columnNames);
this.FIXED_NUM = FIXED_NUM;
}
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);
if(pos>=0)
//if(c==9)
return true;
else
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);
else
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++)
setValueAt(value,row[i],col);
}
public void fillACell(double value,int row,int col) {
setValueAt(Double.valueOf(Double.toString(value)),row,col);
}
public void fillAColumn(Object value,int col) {
for (int i=0;i<getRowCount();i++)
setValueAt(value,i,col);
}
public void fillAColumn(double value,int col) {
for (int i=0;i<getRowCount();i++)
setValueAt(Double.valueOf(Double.toString(value)),i,col);
}
}
thanks,
Alex
 
Ranch Hand
Posts: 168
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

makes both columns to be type Integer.
 
Well THAT's new! Comfort me, reliable tiny ad:
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic