[英]How to make individual Cell of a JTable Uneditable
I am using the following cell Model for my JTable: 我为我的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? 如何设置单个单元格说(1,1)为不可编辑?
How can i set the individual cell say (1,1) as uneditable? 如何设置单个单元格说(1,1)为不可编辑?
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) 问题是,您的isCellEditable
方法使用的是一维数组(如果您要这样做,就可以这样做)
@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. 这基本上就是说,对于给定column
中的所有单元格,它们应该是可编辑的还是不可编辑的。
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
如果要使单个单元格可编辑/不可编辑,则需要确定row
和column
的组合是否使单元格可编辑/不可编辑,而不仅仅是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... 就个人而言,我不会为此使用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];
}
}
}
My version of isCellEditable function to make cell (0,1) uneditable: 我的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;
}
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. 否则,最好使用2D布尔数组。
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。
jTable.setModel(new MyDefaultTableModel());
Set specific cell non editable. 设置特定单元格不可编辑。
MyDefaultTableModel vlModelDtm = (MyDefaultTableModel) jTable.getModel();
vlModelDtm.setCellEditable(0, 5, false);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.