prasadtvsv
asked on
How to edit a JTable and retrieve the values
Dear Friends
I want the JTable to be editable and want to capture each data cell value.
i have written the following code. I am getting the cell as editable true but it is not allowing to edit anything. I am getting the grid editable but the values are
not being editable.
I have written the following code
getData(); // gets the result set which is rss
model = new ResultSetTableModel(rss); // RE
table = new JTable(model);
table.setGridColor(Color.r ed);
table.setRowHeight(table.g etRowHeigh t()*2);
boolean b = table.isCellEditable(1,1);
System.out.println(b); //o/p - true
Object data = table.getValueAt(1,1);
System.out.println(data);
TableColumn column = null;
column = table.getColumnModel().get Column(0);
column.setPreferredWidth(2 00);
column = table.getColumnModel().get Column(1);
column.setPreferredWidth(1 00);
table.setAutoResizeMode(1) ;
muster2.add(jsp = new JScrollPane(table));
jsp.setBorder(new BevelBorder(BevelBorder.LO WERED));
jsp.setPreferredSize(new Dimension(1600,700));
jsp.createHorizontalScroll Bar();
*****************
public class ResultSetTableModel implements TableModel
{
private ResultSetMetaData md;
private Vector dataset;
private Vector cols;
private int rowCount;
public ResultSetTableModel(Result Set data) throws Exception
{
md = data.getMetaData();
dataset = new Vector();
String col;
String val;
String val1;
String val2;
while(data.next())
{
Record r = new Record();
for(int i=1; i<=md.getColumnCount(); i++)
{
col = md.getColumnName(i) + "";
val = data.getString(i) + "";
r.put(col, val);
}
dataset.addElement(r);
}
}
public Class getColumnClass(int columnIndex)
{ return javax.swing.table.TableCol umn.class; }
public int getColumnCount()
{
try{ return md.getColumnCount(); }
catch(Exception e){ e.printStackTrace(); return 0; }
}
public String getColumnName(int columnIndex)
{
try{ return md.getColumnName(columnInd ex + 1); }
catch(Exception e){ e.printStackTrace(); return null; }
}
public int getRowCount()
{ return dataset.size(); }
public Object getValueAt(int rowIndex, int columnIndex)
{
try
{
Record r = ((Record)dataset.elementAt (rowIndex) );
return r.getFieldValueAt(columnIn dex) + "";
}
catch(Exception e)
{
System.out.println("Reques t Invalid: rowIndex = " + rowIndex + ", colIndex = " + columnIndex);
return "";
}
}
public boolean isCellEditable(int rowIndex, int columnIndex)
{ return true; }
public void removeTableModelListener(T ableModelL istener l) { }
public void addTableModelListener(Tabl eModelList ener l) { }
public void setValueAt(Object aValue, int rowIndex, int columnIndex) {}
}
**********************
Record.java
*********************
public class Record extends Hashtable
{
private Vector fieldNames = new Vector();
public Object put(Object key, Object value)
{
if(!fieldNames.contains(ke y))
{
fieldNames.addElement(key) ;
}
return super.put(key, value);
}
public int getFieldCount()
{ return fieldNames.size(); }
public String getFieldNameAt(int index)
{ return fieldNames.elementAt(index ).toString (); }
public String getFieldValue(String fieldName)
{ return super.get(fieldName).toStr ing(); }
public String getFieldValueAt(int index)
{ return getFieldValue(getFieldName At(index)) ; }
}
Pls help me.
Thanxs in advance.
bye
prasad
I want the JTable to be editable and want to capture each data cell value.
i have written the following code. I am getting the cell as editable true but it is not allowing to edit anything. I am getting the grid editable but the values are
not being editable.
I have written the following code
getData(); // gets the result set which is rss
model = new ResultSetTableModel(rss); // RE
table = new JTable(model);
table.setGridColor(Color.r
table.setRowHeight(table.g
boolean b = table.isCellEditable(1,1);
System.out.println(b); //o/p - true
Object data = table.getValueAt(1,1);
System.out.println(data);
TableColumn column = null;
column = table.getColumnModel().get
column.setPreferredWidth(2
column = table.getColumnModel().get
column.setPreferredWidth(1
table.setAutoResizeMode(1)
muster2.add(jsp = new JScrollPane(table));
jsp.setBorder(new BevelBorder(BevelBorder.LO
jsp.setPreferredSize(new Dimension(1600,700));
jsp.createHorizontalScroll
*****************
public class ResultSetTableModel implements TableModel
{
private ResultSetMetaData md;
private Vector dataset;
private Vector cols;
private int rowCount;
public ResultSetTableModel(Result
{
md = data.getMetaData();
dataset = new Vector();
String col;
String val;
String val1;
String val2;
while(data.next())
{
Record r = new Record();
for(int i=1; i<=md.getColumnCount(); i++)
{
col = md.getColumnName(i) + "";
val = data.getString(i) + "";
r.put(col, val);
}
dataset.addElement(r);
}
}
public Class getColumnClass(int columnIndex)
{ return javax.swing.table.TableCol
public int getColumnCount()
{
try{ return md.getColumnCount(); }
catch(Exception e){ e.printStackTrace(); return 0; }
}
public String getColumnName(int columnIndex)
{
try{ return md.getColumnName(columnInd
catch(Exception e){ e.printStackTrace(); return null; }
}
public int getRowCount()
{ return dataset.size(); }
public Object getValueAt(int rowIndex, int columnIndex)
{
try
{
Record r = ((Record)dataset.elementAt
return r.getFieldValueAt(columnIn
}
catch(Exception e)
{
System.out.println("Reques
return "";
}
}
public boolean isCellEditable(int rowIndex, int columnIndex)
{ return true; }
public void removeTableModelListener(T
public void addTableModelListener(Tabl
public void setValueAt(Object aValue, int rowIndex, int columnIndex) {}
}
**********************
Record.java
*********************
public class Record extends Hashtable
{
private Vector fieldNames = new Vector();
public Object put(Object key, Object value)
{
if(!fieldNames.contains(ke
{
fieldNames.addElement(key)
}
return super.put(key, value);
}
public int getFieldCount()
{ return fieldNames.size(); }
public String getFieldNameAt(int index)
{ return fieldNames.elementAt(index
public String getFieldValue(String fieldName)
{ return super.get(fieldName).toStr
public String getFieldValueAt(int index)
{ return getFieldValue(getFieldName
}
Pls help me.
Thanxs in advance.
bye
prasad
Dear Objects
How to do that .
pls help me.
what to do there.
bye
prasad
How to do that .
pls help me.
what to do there.
bye
prasad
The setValueAt method is called when a data cell is modified, passing you the new value and the row, column of the cell (see javadoc for details).
What you need to do is update your data accordingly, ensuring that the next time getValueAt() is called for that cell that the correct value is returned.
What you need to do is update your data accordingly, ensuring that the next time getValueAt() is called for that cell that the correct value is returned.
You have a blank implmentation of setValueAt(), in the last line of your TableModel Class.
public void setValueAt(Object aValue, int rowIndex, int columnIndex){}
You need to fill this in.
Your Record class could do with a
public void setFieldValueAt(int index, Object value)
method, which updates the hashtable.
You can then call this from your TableModels setValue at method like so :
public void setValueAt(Object aValue, int rowIndex, int columnIndex){
Record r = ((Record)dataset.elementAt (rowIndex) );
r.setFieldValueAt(columnIn dex, aValue);
}
public void setValueAt(Object aValue, int rowIndex, int columnIndex){}
You need to fill this in.
Your Record class could do with a
public void setFieldValueAt(int index, Object value)
method, which updates the hashtable.
You can then call this from your TableModels setValue at method like so :
public void setValueAt(Object aValue, int rowIndex, int columnIndex){
Record r = ((Record)dataset.elementAt
r.setFieldValueAt(columnIn
}
Hi,
Check out this (in addition to setValueAt() implementation):
public Class getColumnClass(int columnIndex)
{ return javax.swing.table.TableCol umn.class; }
If I remeber correctly, it should be String.class, not TableColumn.class.
Regards,
Igor Bazarny,
Brainbench MVP for Java 1
www.brainbench.com
Check out this (in addition to setValueAt() implementation):
public Class getColumnClass(int columnIndex)
{ return javax.swing.table.TableCol
If I remeber correctly, it should be String.class, not TableColumn.class.
Regards,
Igor Bazarny,
Brainbench MVP for Java 1
www.brainbench.com
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Dear Friends
The ques what i have asked is not present in my account of Questions Asked . Now how can i give points pls tell me.
bye
prasad
The ques what i have asked is not present in my account of Questions Asked . Now how can i give points pls tell me.
bye
prasad
It's under your prasadtvsv account.
Not your sdinfosys account.
Not your sdinfosys account.
Dear Objects
I had checked both the accounts after that only i posed that doubt. Can u give me ur personal mail ID or any messenger Id so that i can solve my problems.
I dont know how this question is missing???.
bye
prasad
I had checked both the accounts after that only i posed that doubt. Can u give me ur personal mail ID or any messenger Id so that i can solve my problems.
I dont know how this question is missing???.
bye
prasad
mick@objects.com.au
> I dont know how this question is missing???.
ask ee support about it.
> I dont know how this question is missing???.
ask ee support about it.
ASKER
Thanxs objects for ur support.
The SetValueAt is to be implemented for editinf a cell. i was missing that.
Even i was wrong at returning String.class.
bye
prasad
The SetValueAt is to be implemented for editinf a cell. i was missing that.
Even i was wrong at returning String.class.
bye
prasad
ASKER
Hey i want bazarny to be given some points how can i give points to different memebers fro the same question ..................
bazarny in addition to serValueAt methd implementation i was wrong at
public Class getColumnClass(int columnIndex)
{ return javax.swing.table.TableCol umn.class; }
If I remeber correctly, it should be String.class, not TableColumn.class.
bazarny in addition to serValueAt methd implementation i was wrong at
public Class getColumnClass(int columnIndex)
{ return javax.swing.table.TableCol
If I remeber correctly, it should be String.class, not TableColumn.class.
Thanks for the points :)
>>public Class getColumnClass(int columnIndex)
>>{ return javax.swing.table.TableCol umn.class; }
>>
>>If I remeber correctly, it should be String.class, not
>>TableColumn.class.
It sould not be any definite class. The class maybe different in each TableColumn and will not necessarily be a String. The method should look like this :
public Class getColumnClass(int column){
return getValueAt(0, column).getClass();
}
It simply returns the class of whatever is at position 0 in that column.
>>{ return javax.swing.table.TableCol
>>
>>If I remeber correctly, it should be String.class, not
>>TableColumn.class.
It sould not be any definite class. The class maybe different in each TableColumn and will not necessarily be a String. The method should look like this :
public Class getColumnClass(int column){
return getValueAt(0, column).getClass();
}
It simply returns the class of whatever is at position 0 in that column.
Hi,
prasadtvsv,
> Hey i want bazarny to be given some points how can i give points to different memebers fro the same
> question ..................
Thanks. You can ask ee support to split points (well, I guess _before_ accepting some answer, but ask them)--they will decrease points assigned to this question and file another question with remaining points. Or you can submit empty question yourself with additional points for me.
ozymandias,
>>If I remeber correctly, it should be String.class, not
>>TableColumn.class.
> It sould not be any definite class.
[...]
You are right, but in initial code all values are Strings, so there is no need for more complex solution. Besides, your code will fail if model doesn't contain data. I'm not sure that getColumnClass() will not be called for empty model by JTable code.
Regards,
Igor Bazarny.
prasadtvsv,
> Hey i want bazarny to be given some points how can i give points to different memebers fro the same
> question ..................
Thanks. You can ask ee support to split points (well, I guess _before_ accepting some answer, but ask them)--they will decrease points assigned to this question and file another question with remaining points. Or you can submit empty question yourself with additional points for me.
ozymandias,
>>If I remeber correctly, it should be String.class, not
>>TableColumn.class.
> It sould not be any definite class.
[...]
You are right, but in initial code all values are Strings, so there is no need for more complex solution. Besides, your code will fail if model doesn't contain data. I'm not sure that getColumnClass() will not be called for empty model by JTable code.
Regards,
Igor Bazarny.
Without this the cell can be editted, but the underlying data is never updated so the table reverts to the original value.