stop JTable Cell Editing - Java Swing

Java examples for Swing:JTable Cell

Description

stop JTable Cell Editing

Demo Code


//package com.java2s;

import javax.swing.JTable;

public class Main {
    public static void stopCellEditing(JTable table) {
        if (table.isEditing()) {
            int[] selection = table.getSelectedRows();
            table.getCellEditor().stopCellEditing();
            if (selection.length > 0) {
                selectRows(table, selection);
            }/*from ww  w.  j  av a 2  s.com*/
        }
    }

    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