Link to home
Start Free TrialLog in
Avatar of prasadtvsv
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.red);
table.setRowHeight(table.getRowHeight()*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().getColumn(0);
column.setPreferredWidth(200);
column = table.getColumnModel().getColumn(1);
column.setPreferredWidth(100);
table.setAutoResizeMode(1);
muster2.add(jsp = new JScrollPane(table));
jsp.setBorder(new BevelBorder(BevelBorder.LOWERED));
jsp.setPreferredSize(new Dimension(1600,700));
jsp.createHorizontalScrollBar();




*****************
public class ResultSetTableModel implements TableModel
 {
   private ResultSetMetaData md;
   private Vector dataset;
   private Vector cols;
   private int rowCount;

   public ResultSetTableModel(ResultSet 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.TableColumn.class;  }

   public int getColumnCount()
    {
      try{ return md.getColumnCount(); }
      catch(Exception e){ e.printStackTrace(); return 0; }
    }

   public String getColumnName(int columnIndex)
    {
      try{ return md.getColumnName(columnIndex + 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(columnIndex) + "";
      }
     catch(Exception e)
      {
        System.out.println("Request Invalid: rowIndex = " + rowIndex + ", colIndex = " + columnIndex);
        return "";
      }
    }

 public boolean isCellEditable(int rowIndex, int columnIndex)
    { return true; }
   
   public void removeTableModelListener(TableModelListener l) { }
   public void addTableModelListener(TableModelListener 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(key))
       {
       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).toString(); }

   public String getFieldValueAt(int index)
    { return getFieldValue(getFieldNameAt(index)); }

 }



Pls help me.

Thanxs in advance.

bye
prasad
Avatar of Mick Barry
Mick Barry
Flag of Australia image

You need to implement setValueAt in your model.
Without this the cell can be editted, but the underlying data is never updated so the table reverts to the original value.
Avatar of sdinfosys
sdinfosys

Dear Objects


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.
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(columnIndex, aValue);
    }



Hi,

Check out this (in addition to setValueAt() implementation):

 public Class getColumnClass(int columnIndex)
 {  return javax.swing.table.TableColumn.class;  }

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
Avatar of Mick Barry
Mick Barry
Flag of Australia image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
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
It's under your prasadtvsv 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
mick@objects.com.au

> I dont know how this question is missing???.

ask ee support about it.

Avatar of prasadtvsv

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
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.TableColumn.class;  }

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.TableColumn.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.



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.