JTable setValueAt()>>>?????

How to use JTable setValueAt() coz i tried to use it but it doesnt work...

E.g.

jtbl.setValueAt(value1,row1,1);
jtbl.setValueAt(value2,row2,1);
mattjuliusAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Mick BarryJava DeveloperCommented:
what table mode are you using?
mattjuliusAuthor Commented:
AbstractTableModel...
Mick BarryJava DeveloperCommented:
if you are subclassing it then you need to implement setValueAt() method in the model
OWASP: Threats Fundamentals

Learn the top ten threats that are present in modern web-application development and how to protect your business from them.

mattjuliusAuthor Commented:
how?? can you help out>....
Mick BarryJava DeveloperCommented:
post your model class
mattjuliusAuthor Commented:
public class ResultSetTableModel extends AbstractTableModel  {
      /**
       *
       */
   private static final long serialVersionUID = 1L;
   private Connection connection;
   public Statement statement;
   public ResultSet resultSet;
   //private Connection connection2;
   //public Statement statement2;
   //public ResultSet resultSet2;
   private ResultSetMetaData metaData;
   private int numberOfRows;

   // keep track of database connection status
   private boolean connectedToDatabase = false;
   
   // constructor initializes resultSet and obtains its meta data object;
   // determines number of rows
   public ResultSetTableModel( String query )
      throws SQLException, ClassNotFoundException
   {        
      // connect to database
      //connection = cnn;
      //connection2 = DriverManager.getConnection( url, username, password );
     
      // create Statement to query database
      statement = DBConnect.connection.createStatement(
         ResultSet.TYPE_SCROLL_SENSITIVE,
         ResultSet.CONCUR_READ_ONLY );
      // update database connection status
      connectedToDatabase = true;

      // set query and execute it
      setQuery( query );
   } // end constructor ResultSetTableModel

   /*// get class that represents column type
   public Class getColumnClass( int column ) throws IllegalStateException
   {
      // ensure database connection is available
      if ( !connectedToDatabase )
         throw new IllegalStateException( "Not Connected to Database" );

      // determine Java class of column
      try
      {
         String className = metaData.getColumnClassName( column + 1 );
         
         // return Class object that represents className
         return Class.forName( className );
      } // end try
      catch ( Exception exception )
      {
         exception.printStackTrace();
      } // end catch
     
      return Object.class; // if problems occur above, assume type Object
   } // end method getColumnClass*/

   // get number of columns in ResultSet
   public int getColumnCount() throws IllegalStateException
   {  
      // ensure database connection is available
      if ( !connectedToDatabase )
         throw new IllegalStateException( "Not Connected to Database" );

      // determine number of columns
      try
      {
         return metaData.getColumnCount();
      } // end try
      catch ( SQLException sqlException )
      {
            System.out.println(sqlException.getMessage() + " line 94");
      } // end catch
     
      return 0; // if problems occur above, return 0 for number of columns
   } // end method getColumnCount

   // get name of a particular column in ResultSet
   public String getColumnName( int column ) throws IllegalStateException
   {    
      // ensure database connection is available
      if ( !connectedToDatabase )
         throw new IllegalStateException( "Not Connected to Database" );

      // determine column name
      try
      {
         return metaData.getColumnName( column + 1 );  
      } // end try
      catch ( SQLException sqlException )
      {
            System.out.println(sqlException.getMessage() + " line 114");
      } // end catch
     
      return ""; // if problems, return empty string for column name
   } // end method getColumnName

   // return number of rows in ResultSet
   public int getRowCount() throws IllegalStateException
   {      
      // ensure database connection is available
      if ( !connectedToDatabase )
         throw new IllegalStateException( "Not Connected to Database" );
 
      return numberOfRows;
   } // end method getRowCount

   // obtain value in particular row and column
   public Object getValueAt( int row, int column )
      throws IllegalStateException
   {
      // ensure database connection is available
      if ( !connectedToDatabase )
         throw new IllegalStateException( "Not Connected to Database" );

      // obtain a value at specified ResultSet row and column
      try
      {
         resultSet.absolute( row + 1 );
         return resultSet.getObject( column + 1 );
      } // end try
      catch ( SQLException sqlException )
      {
            //JOptionPane.showMessageDialog(null,"Record locked....","Information",JOptionPane.INFORMATION_MESSAGE);
      } // end catch
     
      return ""; // if problems, return empty string object
   } // end method getValueAt
   
   public void again(String q)throws SQLException, IllegalStateException {
         try {
            statement.setQueryTimeout(1000);
            statement.executeQuery(q);
      } catch (SQLException e) {
            System.out.println("Hi");
      }
   }
   
   // set new database query string
   public void setQuery( String query )
      throws SQLException, IllegalStateException
   {
      // ensure database connection is available
      if ( !connectedToDatabase )
         throw new IllegalStateException( "Not Connected to Database" );

      try {
            // specify query and execute it
            resultSet = statement.executeQuery( query );
            //resultSet = statement.execute( query );
     
     
            // obtain meta data for ResultSet
            metaData = resultSet.getMetaData();
     
     
            // determine number of rows in ResultSet
            resultSet.last();                   // move to last row
            numberOfRows = resultSet.getRow();  // get row number      
     
            // notify JTable that model has changed
            fireTableDataChanged();
            
      }catch(SQLException sqlE){
            
      }
   } // end method setQuery
   

   
   public void setInsert( String query1 )
   throws SQLException, IllegalStateException
{
   // ensure database connection is available
   if ( !connectedToDatabase )
      throw new IllegalStateException( "Not Connected to Database" );
      statement.execute(query1);
   
   // notify JTable that model has changed
   fireTableStructureChanged();
} // end method setQuery
   
   // close Statement and Connection              
   public void disconnectFromDatabase()            
   {              
      if ( !connectedToDatabase )                  
         return;

      // close Statement and Connection            
      try                                          
      {                                            
         statement.close();                        
         connection.close();                      
      } // end try                                
      catch ( SQLException sqlException )          
      {                                            
            System.out.println(sqlException.getMessage() + " line 210");          
      } // end catch                              
      finally  // update database connection status
      {                                            
         connectedToDatabase = false;              
      } // end finally                            
   } // end method disconnectFromDatabase    
   
   public void setValueAt(Object value, int row, int col) {
       
        
   }

   
}
Mick BarryJava DeveloperCommented:
By the looks setValueAt() is going to need:

- close the open result set
- perform necessary update statement
- reopen the result set
- fire required event indication cell has changed

mattjuliusAuthor Commented:
what do you mean
???
Mick BarryJava DeveloperCommented:
thats what you'll need to do in your setValueAt() method.
which bit don't u understand?
mattjuliusAuthor Commented:
public void setValueAt(Object value, int row, int col) {
       
        
         fireTableCellUpdated(row,col);
 }

where can i put the variable value
Mick BarryJava DeveloperCommented:
yopu need to update the database, and rerun your query

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Java

From novice to tech pro — start learning today.