The Java Developers Almanac 1.4


Order this book from Amazon.

   
Home > List of Packages > javax.swing.table  [62 examples] > Editing  [8 examples]

e958. Disabling User Edits in a JTable Component

User editing can be disabled in two places. By disabling edits in the model, no table component using that model will allow editing by the user. By disabling edits in the table component, only that component disallows edits; other table components using the same model will allow editing.

Disabling edits requires subclassing either the JTable or TableModel class.

Note: Although user editing is disabled, programmatically changing values in the model is still possible.

    // Create a JTable that disallow edits
    JTable table1 = new JTable() {
        public boolean isCellEditable(int rowIndex, int vColIndex) {
            return false;
        }
    };
    
    // Create a JTable based on the same model as table1 but allows edits
    JTable table2 = new JTable(table1.getModel());
    
    // Create a model that disallows edits; JTable's using this model will not allow edits
    TableModel model = new DefaultTableModel() {
        public boolean isCellEditable(int rowIndex, int mColIndex) {
            return false;
        }
    };
    

 Related Examples
e953. Creating a Custom Table Cell Editor in a JTable Component
e954. Preventing Invalid Values in a Cell in a JTable Component
e955. Setting the Activation Click Count for a Table Cell Editor in a JTable Component
e956. Programmatically Starting and Stopping Cell Editing in a JTable Component
e957. Creating a Text Field That Mirrors the Value in the Anchor Cell in a JTable Component
e959. Using a JComboBox in a Cell in a JTable Component
e960. Using a List JSpinner as a Cell Editor in a JTable Component

See also: Cells    Column Heads    Columns    Events    Layout    Rows    Scrolling    Selection    Sorting    Table Model    Tool Tips   


© 2002 Addison-Wesley.