[英]How to make individual Cell of a JTable Uneditable
我为我的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);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.