1

我正在为我的 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];
    }
});

但这会将整行和列设置为可编辑/不可编辑。如何将单个单元格(1,1)设置为不可编辑?

4

4 回答 4

2

如何将单个单元格(1,1)设置为不可编辑?

通过简单地使用传递的行和列索引

@Override
public boolean isCellEditable(int rowIndex, int columnIndex) {
  return !( rowIndex == 1 && columnIndex == 1 );
}
于 2013-09-13T22:08:40.347 回答
1

问题是,您的isCellEditable方法使用的是单维数组(如果您希望这样做,可以这样做)

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

这基本上说,对于给定的所有单元格column,它们应该是可编辑的或不可编辑的。

如果要使单个单元格可编辑/不可编辑,则需要确定row和的组合是否column使单元格可编辑/不可编辑,而不仅仅是column

用简单的例子更新

这是该概念的一个简单示例。就个人而言,我不会为此使用 2D 数组,因为使用动态表模型来管理它变得很困难......

在此处输入图像描述

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];
        }
    }
}
于 2013-09-13T22:23:41.367 回答
0

我的 isCellEditable 函数版本使单元格 (0,1) 不可编辑:

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

如果不是,最好使用条件返回。可编辑单元格的数量大于不可编辑单元格,反之亦然,并且单元格遵循编辑-不可编辑模式。否则最好使用二维布尔数组。

于 2013-09-20T13:41:33.360 回答
0

这对我有用。

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);
    }
}

将模型设置为 jtable。

jTable.setModel(new MyDefaultTableModel());

设置特定单元格不可编辑。

MyDefaultTableModel vlModelDtm = (MyDefaultTableModel) jTable.getModel();
vlModelDtm.setCellEditable(0, 5, false);
于 2015-11-20T19:14:58.243 回答