File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
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
Author

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


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.
 
Consider Paul's rocket mass heater.
 
subject: TableModel question