• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

making a selected cell editable in JTable and removing a selected row from JTable

 
Ranch Hand
Posts: 67
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
hi all,
guys i'm having a problem when i want to remove a row from a JTable.i did get the selected rowIndex and passed it to the method remove(int index) of my ArrayList of type array os strings but remove() returned false so i did change the Array List to a Linked List so i do have the ability to remove in the middle of it, anyway it didn't work too.i did implement a method for removing row/s:here is my implementation:

public void removeRows(int[] rowIndexes)
{
for(Integer i: rowIndexes)
{
String caseNum = (String)getValueAt(i, 0);
Boolean b = resultSets.remove(i);
fireTableRowsDeleted(i, i);
}
}

resultSets = new LinkedList<String[]>();//it represents rows of my
// table and each array os strings represents a row in my table.

there is another question how can i make a cell editable and save changes to my DB?
i tryed to implement the method isCellEditable that returns always true as.

All my changes are done in my model.
please advice.
 
Mahmoud Hadad
Ranch Hand
Posts: 67
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
guys i have made a method in my table's model that takes an array os strings which represents the indexes for the selected rows if the user multi selected rows or one row if the user selected one row only.here is my method:
the problem is that when i try to remove the array of strings which represents a row in my table from an LinkedList object which reprensents the whole table ,the remove() returns false and nothing is removed:


public void removeRows(int[] rowIndexes)
{
for(Integer i: rowIndexes)
{
Boolean b = resultSets.remove(i);

fireTableRowsDeleted(i, i);
}
}


as for my edit method is simply does not save my fields after editing to my DB ,so i used a SQL statement to update my DB but sometimes it duplicates that field for the next field in the same column .here is my code :


public void setValueAt(Object aValue, int rowIndex, int columnIndex) {
String[] row = resultSets.get(rowIndex);
row[columnIndex] = (String) aValue;
fireTableCellUpdated(rowIndex, columnIndex);

try
{
Statement stmt = con.createStatement();
String oldValue = (String) getValue();
String columnName = getColumnName(columnIndex);
int result = stmt.executeUpdate("UPDATE GeneralTabel "+
"SET ["+columnName+"]"+ "=" + "'"+aValue + "'" +
"WHERE [" + columnName + "] =" + "'"+ oldValue + "'");
if(result == 0)
{
System.out.println("Please re edit fields");
}

}


catch(SQLException e)
{
System.out.println("SQLException : "+e.getMessage());
}
}

pplease advice what am i doing wrong?
 
Mahmoud Hadad
Ranch Hand
Posts: 67
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
i'm replying myself so if someone else faces the same problem can find his answear in this post.as for making a cell editable i have created a boolean value and implemented a setter and getter for it.anyway the current behavior of isCellEditing method is set to false until the user chooses to edit so my boolean value are set to true.
my implementation is mentioned below:

public boolean isCellEditable()
{
return cellEditing; //cellEditing is a boolean value that indicates
// if the cell is editable or not
}
and my implementation for the setValueAt method is i used the current behavior of editing cells so as when u edit a cell in the default behavior ur updated cells keeps the same as the view only changes but nothing changes in the model so i just used a SQL statement to update my fields using my table's primary key.

as for my remove method i don't know why i can not remove an array of strings that is stored in a LinkedList object using an index it keeps returning false, although i retrieved those rows using the same index?Any ideas?
 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic