• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 394
  • Last Modified:

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
0
prasadtvsv
Asked:
prasadtvsv
  • 6
  • 3
  • 2
  • +2
1 Solution
 
objectsCommented:
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.
0
 
sdinfosysCommented:
Dear Objects


How to do that .



pls help me.
what to do there.


bye
prasad
0
 
objectsCommented:
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.
0
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
ozymandiasCommented:
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);
    }



0
 
Igor BazarnyCommented:
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
0
 
objectsCommented:
I saw that 2, but edittings working so it does not appear to be causing a problem.
Probably cause an editor's not defined for TableColumn.class so it falls back on the default.
0
 
sdinfosysCommented:
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
0
 
objectsCommented:
It's under your prasadtvsv account.
Not your sdinfosys account.
0
 
sdinfosysCommented:
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
0
 
objectsCommented:
mick@objects.com.au

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

ask ee support about it.

0
 
prasadtvsvAuthor Commented:
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
0
 
prasadtvsvAuthor Commented:
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.


0
 
objectsCommented:
Thanks for the points :)
0
 
ozymandiasCommented:
>>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.



0
 
Igor BazarnyCommented:
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.
0

Featured Post

Vote for the Most Valuable Expert

It’s time to recognize experts that go above and beyond with helpful solutions and engagement on site. Choose from the top experts in the Hall of Fame or on the right rail of your favorite topic page. Look for the blue “Nominate” button on their profile to vote.

  • 6
  • 3
  • 2
  • +2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now