cancel JTable Cell Editing - Java Swing

Java examples for Swing:JTable Cell

Description

cancel JTable Cell Editing

Demo Code


//package com.java2s;

import javax.swing.JTable;

public class Main {
    public static void cancelCellEditing(JTable table) {
        if (table.isEditing()) {
            int[] selection = table.getSelectedRows();
            table.getCellEditor().cancelCellEditing();
            if (selection.length > 0) {
                selectRows(table, selection);
            }//from  w w w  .  j  av  a2s  .co m
        }
    }

    public static void selectRows(JTable table, int[] rowIndexes) {
        table.getSelectionModel().setValueIsAdjusting(true);
        try {
            table.clearSelection();
            for (int row : rowIndexes) {
                table.addRowSelectionInterval(row, row);
            }
            if (table.getCellSelectionEnabled()) {
                table.setColumnSelectionInterval(0,
                        table.getColumnCount() - 1);
            }
        } finally {
            table.getSelectionModel().setValueIsAdjusting(false);
        }
    }
}

Related Tutorials