I\'ve tried this:
public void removeSelectedFromTable(JTable from)
{
int[] rows = from.getSelectedRows();
TableModel tm= from.getModel();
while(
Try this:
int selectedIndex = table_.getSelectedRow();
while (selectedIndex >= 0) {
int modelIndex = table_.convertRowIndexToModel(selectedIndex);
tableModel_.removeRow(modelIndex);
selectedIndex = table_.getSelectedRow();
}
This code work for me!
DefaultTableModel model = (DefaultTableModel) Table.getModel();
int nbrrow=Table.getRowCount();
if(nbrrow>=1){
int[] rows = Table.getSelectedRows();
if(rows.length>=1){
if(Table.getModel().isCellEditable(Table.getSelectedRow(),Table.getSelectedColumn())
&&Table.isEditing()==true)
{
Table.getCellEditor().stopCellEditing();
for(int i=0;i<rows.length;i++){
model.removeRow(rows[i]-i);
}
}else{
for(int i=0;i<rows.length;i++){
model.removeRow(rows[i]-i);
}
}
for(int i=0;i<Table.getRowCount();i++){
model.setValueAt(i, i, 0);
}
Table.revalidate();
Table.repaint();
}
}
Try this
public void removeSelectedFromTable(JTable table) {
DefaultTableModel model = (DefaultTableModel) table.getModel();
int indexes[] = table.getSelectedRows();
int res = 0;
for(int i = 0; i < indexes.length; i++) {
res += (i>0)?(indexes[i]-indexes[i-1]-1):0;
int index = table.convertRowIndexToModel(indexes[0]+res)
model.removeRow(index);
}
}
I looked at all the solutions, but there are some exceptions, so I would like to add this answer.
for (int i = 0; i < raws.length; i++) {
System.out.println("I : " + i);
for (int j = 1; j <= 1; j++) {
defaultTableModel.removeRow(0);
jTStockJournal.clearSelection();
defaultTableModel.fireTableDataChanged();
}
}
private void jButton2ActionPerformed(java.awt.event.ActionEvent evt) {
DefaultTableModel defaultModel = (DefaultTableModel) jTable1.getModel();
Vector newRow = new Vector();
defaultModel.removeRow(jTable1.getSelectedRow());
i = i - 1;
}
That works for me:
if(tablaNotif.getSelectedRows().length>0){
int[] rows = tablaNotif.getSelectedRows();
if(tablaNotif.getSelectedRows().length<2) {
modelo.remove(tablaNotif.convertRowIndexToModel(rows[0]));
} else{
for(int i = 0; i<tablaNotif.getSelectedRows().length;i++) {
modelo.remove(tablaNotif.convertRowIndexToModel(rows[i]));
}
modelo.remove(tablaNotif.convertRowIndexToModel(rows[(tablaNotif.getSelectedRows().length-1)]));
}
}