我试过这个:
public void removeSelectedFromTable(JTable from)
{
int[] rows = from.getSelectedRows();
TableModel tm= from.getModel();
while(rows.length>0)
{
((DefaultTableModel)tm).removeRow(from.convertRowIndexToModel(rows[0]));
rows = from.getSelectedRows();
}
from.clearSelection();
}
但是,它有时会留下一个。可能是什么问题?
它不起作用,这是更好的:
public void removeSelectedRows(JTable table){
DefaultTableModel model = (DefaultTableModel) this.table.getModel();
int[] rows = table.getSelectedRows();
for(int i=0;i<rows.length;i++){
model.removeRow(rows[i]-i);
}
}
试试这个:
int selectedIndex = table_.getSelectedRow();
while (selectedIndex >= 0) {
int modelIndex = table_.convertRowIndexToModel(selectedIndex);
tableModel_.removeRow(modelIndex);
selectedIndex = table_.getSelectedRow();
}
它适用于我。输入代码在这里
public void removeSelectedFromTable(JTable from)
{
int[] rows = from.getSelectedRows();
DefaultTableModel tm = (DefaultTableModel) from.getModel();
for (int i = rows.length-1; i >= 0; i--) {
tm.removeRow(rows[i]);
}
}
以下代码将起作用:
do {
((DefaultTableModel) jXTable1.getModel()).removeRow(jXTable1.getSelectedRows()[0]);
} while (jXTable1.getSelectedRowCount() > 0);
这对我行得通:
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)]));
}
}
上面的流解决方案没有考虑排序,它可以这样修复:
IntStream.of(table.getSelectedRows())
.boxed().map(i -> table.convertRowIndexToModel(i))
.sorted(Collections.reverseOrder())
.forEach(((DefaultTableModel)table.getModel())::removeRow);
我查看了所有解决方案,但也有一些例外,所以我想补充一下这个答案。
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();
}
}
该解决方案适用于标题排序和表自动更新(即:不需要重新编译它)。
“Collecions.sort以相反的顺序”可以避免IndexOutOfBondException。
private void deleteSelectedRows() {
int[] selectedRows = table.getSelectedRows();
table.clearSelection();
// get model rows
List<Integer> selectedModelRows = new LinkedList<Integer>();
for (int i =`enter code here` 0; i < selectedRows.length; i++) {
selectedModelRows.add(table.convertRowIndexToModel(selectedRows[i]));
}
Collections.sort(selectedModelRows, Collections.reverseOrder());
for (int selectedModelRow : selectedModelRows) {
tableModel.removeRow(selectedModelRow);
tableModel.fireTableRowsDeleted(selectedModelRow, selectedModelRow);
}
}
private void jButton2ActionPerformed(java.awt.event.ActionEvent evt){ DefaultTableModel defaultModel =(DefaultTableModel)jTable1.getModel();矢量newRow = new Vector(); defaultModel.removeRow(jTable1.getSelectedRow()); i = i - 1; }
int[] selectedRows = getTable().getSelectedRows();
if (selectedRows.length > 0) {
for (int i = selectedRows.length - 1; i >= 0; i--) {
tableModel.removeRow(selectedRows[i]);
}
}
这对我有用
int numRows = table.getSelectedRows().length;
for(int i=0; i<numRows ; i++ ) {
m_tableModel.removeRow(table.getSelectedRow());
}
为什么不用这个,
public void removeSelectedFromTable(JTable from)
{
int[] rows = from.getSelectedRows();
TableModel tm = (DefaultTableModel) from.getModel();
for (int row : rows) {
tm.removeRow(from.convertRowIndexToModel(row));
}
from.clearSelection();
}
removeRows: {
int[] lines = t.getSelectedRows();
if(lines.length==0)
break removeRows;
for(int i=0; i<lines.length; i++) {
lines[i]=t.convertRowIndexToModel(lines[i]);
}
Arrays.sort(lines);
for(int i=lines.length-1; i>=0; i--) {
dtm.removeRow(lines[i]);
}
}
使用排序,移动列,多选等。
Java 8流媒体解决方案:
IntStream.of(table.getSelectedRows())
.boxed()
.sorted(Collections.reverseOrder())
.forEach(model::removeRow);
JTable选项卡; DefaultTableModel模型;
int[] selectedRows = table.getSelectedRows();
List<Object> selectedObjects = new ArrayList<Object>();
for (int row : selectedRows) {
selectedObjects.add(tableModel.getObject(row));
}
for (Object obj : selectedObjects) {
tableModel.removeObject(obj);
}
//我的TableModel是这个的子类:
public abstract class ListTableModel<T> extends AbstractTableModel {
private List<T> data;
public void setData(List<T> data) {
this.data = data;
}
public T getObject(int row) {
return data.get(row);
}
@Override
public int getRowCount() {
return data.size();
}
}
这段代码对我有用!
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();
}
}
试试这个
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);
}
}