简体   繁体   中英

How to make individual Cell of a JTable Uneditable

I am using the following cell Model for my JTable:

this.setModel(new DefaultTableModel
    (
    new Object [][] {
    {"Item ID", ""},
    {"Radius", 0},
    {"Center", 0,0},
    {"Mass", 0}
    },
    new String [] 
    {
        "Property", "Value"
    }
    ) 
{
    Class[] types = new Class [] 
    {
        String.class, Object.class
    };
    boolean[] canEdit = new boolean [] 
    {
        false, true
    };

    @Override
    public Class getColumnClass(int columnIndex) 
    {
        return types [columnIndex];
    }

    @Override
    public boolean isCellEditable(int rowIndex, int columnIndex) 
    {
        return canEdit [columnIndex];
    }
});

but this sets the whole row and column editable/uneditable. How can i set the individual cell say (1,1) as uneditable?

How can i set the individual cell say (1,1) as uneditable?

By simply using the passed row and column index

@Override
public boolean isCellEditable(int rowIndex, int columnIndex) {
  return !( rowIndex == 1 && columnIndex == 1 );
}

The problem is, you isCellEditable method is using a single dimension array (which is okay to do if that's what you want it to do)

    @Override
    public boolean isCellEditable(int rowIndex, int columnIndex) 
    {
        return canEdit [columnIndex];
    }

This basically says, that for all the cells in a given column that they should be editable or not.

If you want make an individual cell editable/uneditable, you need to determine if the combination of row and column makes the cell editable/uneditable, not just the column

Updated with simple example

This is a SIMPLE example of the concept. Personally, I wouldn't use a 2D array for this because it becomes to difficult to manage with dynamically table models...

在此处输入图片说明

import java.awt.EventQueue;
import javax.swing.JFrame;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;
import javax.swing.table.DefaultTableModel;

public class TestTable {

    public static void main(String[] args) {
        new TestTable();
    }

    public TestTable() {
        EventQueue.invokeLater(new Runnable() {
            @Override
            public void run() {
                try {
                    UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
                } catch (ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException ex) {
                    ex.printStackTrace();
                }
                MyTableModel model = new MyTableModel();
                JTable table = new JTable(model);

                JFrame frame = new JFrame("Testing");
                frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                frame.add(new JScrollPane(table));
                frame.pack();
                frame.setLocationRelativeTo(null);
                frame.setVisible(true);
            }

        });
    }

    public class MyTableModel extends DefaultTableModel {

        public MyTableModel() {
            super(new Object[][]{
                {"Item ID", ""},
                {"Radius", 0},
                {"Center", 0},
                {"Mass", 0}
            }, new String[]{
                "Property", "Value"
            });
        }
        Class[] types = new Class[]{
            String.class, Object.class
        };
        boolean[][] canEdit = new boolean[][]{
            {false, false},
            {false, true},
            {true, true},
            {true, false},
        };

        @Override
        public Class getColumnClass(int columnIndex) {
            return types[columnIndex];
        }

        @Override
        public boolean isCellEditable(int rowIndex, int columnIndex) {
            return canEdit[rowIndex][columnIndex];
        }
    }
}

My version of isCellEditable function to make cell (0,1) uneditable:

@Override
    public boolean isCellEditable(int rowIndex, int columnIndex) 
    {
        if(columnIndex==0)
            return false;
        if(columnIndex==1 && rowIndex>=1)
            return true;
        else return false;
    }

Using conditional return is best if the no. of editable cells is greater than uneditable cells or vice-versa and cells follow an edit-unedit pattern. Otherwise it would be good to use a 2D boolean array.

This works for me.

public class MyDefaultTableModel extends    javax.swing.table.DefaultTableModel {

    public MyDefaultTableModel() { // constructor
        super( new Object [][] {
        {null, null, null, null, null, null},
        {null, null, null, null, null, null},
        {null, null, null, null, null, null},
        {null, null, null, null, null, null}
        },
        new String [] {
            "Folio", "Artículo", "Precio", "Descuento", "Total", "Entregar"
        });

    }


     Class[] types = new Class [] {
        java.lang.Object.class, java.lang.Object.class,     java.lang.Object.class,java.lang.Object.class, java.lang.Object.class,     java.lang.Boolean.class
    };

    @Override
    public Class getColumnClass(int columnIndex) {
        return types [columnIndex];
    }
    boolean[][] canEdit = new boolean[][]{
        {false, false, false, false, false, true},
        {false, false, false, false, false, true},
        {false, false, false, false, false, true},
        {false, false, false, false, false, true}
    };

    @Override
    public boolean isCellEditable(int rowIndex, int columnIndex) {
        return canEdit[rowIndex][columnIndex];
    }

    public void setCellEditable(int row, int col, boolean value) {
        canEdit[row][col] = value; // set cell true/false
        this.fireTableCellUpdated(row, col);
    }
}

Set the model to the jtable.

jTable.setModel(new MyDefaultTableModel());

Set specific cell non editable.

MyDefaultTableModel vlModelDtm = (MyDefaultTableModel) jTable.getModel();
vlModelDtm.setCellEditable(0, 5, false);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM