Solved

How to edit a JTable and retrieve the values

Posted on 2001-07-11
15
355 Views
Last Modified: 2007-12-19
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
Comment
Question by:prasadtvsv
  • 6
  • 3
  • 2
  • +2
15 Comments
 
LVL 92

Expert Comment

by:objects
ID: 6275756
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
 

Expert Comment

by:sdinfosys
ID: 6276309
Dear Objects


How to do that .



pls help me.
what to do there.


bye
prasad
0
 
LVL 92

Expert Comment

by:objects
ID: 6276326
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
 
LVL 15

Expert Comment

by:ozymandias
ID: 6276395
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
 
LVL 7

Expert Comment

by:Igor Bazarny
ID: 6278323
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
 
LVL 92

Accepted Solution

by:
objects earned 60 total points
ID: 6279084
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
 

Expert Comment

by:sdinfosys
ID: 6284589
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
How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

 
LVL 92

Expert Comment

by:objects
ID: 6284594
It's under your prasadtvsv account.
Not your sdinfosys account.
0
 

Expert Comment

by:sdinfosys
ID: 6284604
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
 
LVL 92

Expert Comment

by:objects
ID: 6284622
mick@objects.com.au

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

ask ee support about it.

0
 

Author Comment

by:prasadtvsv
ID: 6284710
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
 

Author Comment

by:prasadtvsv
ID: 6284719
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
 
LVL 92

Expert Comment

by:objects
ID: 6284735
Thanks for the points :)
0
 
LVL 15

Expert Comment

by:ozymandias
ID: 6284842
>>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
 
LVL 7

Expert Comment

by:Igor Bazarny
ID: 6287086
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

Maximize Your Threat Intelligence Reporting

Reporting is one of the most important and least talked about aspects of a world-class threat intelligence program. Here’s how to do it right.

Join & Write a Comment

For customizing the look of your lightweight component and making it look lucid like it was made of glass. Or: how to make your component more Apple-ish ;) This tip assumes your component to be of rectangular shape and completely opaque. (COD…
After being asked a question last year, I went into one of my moods where I did some research and code just for the fun and learning of it all.  Subsequently, from this journey, I put together this article on "Range Searching Using Visual Basic.NET …
The viewer will learn how to implement Singleton Design Pattern in Java.
This tutorial explains how to use the VisualVM tool for the Java platform application. This video goes into detail on the Threads, Sampler, and Profiler tabs.

743 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

13 Experts available now in Live!

Get 1:1 Help Now