This week's book giveaway is in the Mac OS forum.
We're giving away four copies of a choice of "Take Control of Upgrading to Yosemite" or "Take Control of Automating Your Mac" and have Joe Kissell on-line!
See this thread for details.
The moose likes Swing / AWT / SWT and the fly likes making a selected cell editable in JTable and removing a selected row from JTable Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


JavaRanch » Java Forums » Java » Swing / AWT / SWT
Bookmark "making a selected cell editable in JTable and removing a selected row from JTable " Watch "making a selected cell editable in JTable and removing a selected row from JTable " New topic
Author

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

Mahmoud Hadad
Ranch Hand

Joined: Jan 09, 2006
Posts: 67
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

Joined: Jan 09, 2006
Posts: 67
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

Joined: Jan 09, 2006
Posts: 67
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.
 
subject: making a selected cell editable in JTable and removing a selected row from JTable