如何使JTable的单个Cell不可编辑
我正在为我的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)设置为不可编辑?
如何将单个单元格(1,1)设置为不可编辑?
通过简单地使用传递的行和列索引
@Override public boolean isCellEditable(int rowIndex, int columnIndex) { return !( rowIndex == 1 && columnIndex == 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]; } } }
我的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; }
如果没有,最好使用条件返回。 可编辑细胞的数量大于不可编辑细胞,反之亦然,细胞遵循编辑 – 未编辑模式。 否则,最好使用2D布尔数组。
这对我有用。
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);
- Java中的字符是1字节还是2字节?
- 如何组织Java属性条目进行国际化?
- 如何从java类调用python方法?
- 获取对Java的默认http(s)URLStreamHandler的引用
- 如何在运行时获取DiscriminatorValue
- 有没有办法使用JPA注释和Hibernate动态选择@GeneratedValue策略?
- 如果做一个新的日期(0L),时代不是时代。 为什么?
- 套接字,BufferedReader.readline() – 为什么流没有准备好?
- 无法传输工件(https://repo.maven.apache.org/maven2):收到致命警报:protocol_version – >