我现在的代码如下,StockTableModel extends DefaultTableModel ,并将isCellEditable()的返回值设为false,但是JTable单元格仍然是可编辑的,大家看看。
etTableModel = new StockTableModel();
etTable = new JTable();
etTable.setModel(etTableModel);
etTableModel.setDataVector(etDataInfo, etColumnNames); etTable.setAutoResizeMode(JTable.AUTO_RESIZE_OFF);
etTable.setShowHorizontalLines(false);
etTable.setShowVerticalLines(false);
etTable.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
etTable.setToolTipText("本日銘柄データ");
etTable.getTableHeader().setReorderingAllowed(false); etTable.setPreferredScrollableViewportSize(new Dimension(this.ALL_BRAND_PANEL_X,
this.ALL_BRAND_PANEL_Y));
.............public class StockTableModel extends DefaultTableModel {
public boolean isCellEditable() {
return false;
}
}
etTableModel = new StockTableModel();
etTable = new JTable();
etTable.setModel(etTableModel);
etTableModel.setDataVector(etDataInfo, etColumnNames); etTable.setAutoResizeMode(JTable.AUTO_RESIZE_OFF);
etTable.setShowHorizontalLines(false);
etTable.setShowVerticalLines(false);
etTable.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
etTable.setToolTipText("本日銘柄データ");
etTable.getTableHeader().setReorderingAllowed(false); etTable.setPreferredScrollableViewportSize(new Dimension(this.ALL_BRAND_PANEL_X,
this.ALL_BRAND_PANEL_Y));
.............public class StockTableModel extends DefaultTableModel {
public boolean isCellEditable() {
return false;
}
}
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.*;import javax.swing.*;
import javax.swing.table.*;/**
* JTable的例子
*/
public class Test {
private JFrame frame = null; private JTable table = null; private Table_Model model = null; private JScrollPane s_pan = null; private JButton button_1 = null, button_2 = null; public Test() {
frame = new JFrame("Test");
button_1 = new JButton("清除现有的数据");
button_1.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
removeData();
}
});
button_2 = new JButton("再次添加数据");
button_2.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
addData();
}
});
model = new Table_Model(20);
table = new JTable(model);
table.setBackground(Color.white);
String [] age = {"16", "17", "18", "19", "20", "21", "22"};
JComboBox com = new JComboBox(age);
TableColumnModel tcm = table.getColumnModel();
tcm.getColumn(3).setCellEditor(new DefaultCellEditor(com)); s_pan = new JScrollPane(table); frame.getContentPane().add(s_pan, BorderLayout.CENTER);
frame.getContentPane().add(button_1, BorderLayout.NORTH);
frame.getContentPane().add(button_2, BorderLayout.SOUTH);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(300, 200);
frame.setVisible(true); } private void addData() {
model.addRow("李逵", true, "19");
table.updateUI(); // 刷新,将改变后的内容显示出来
} private void removeData() {
model.removeRows(0, model.getRowCount());
table.updateUI(); // 刷新,将改变后的内容显示出来
} public static void main(String args[]) {
new Test();
}}
class Table_Model extends AbstractTableModel {
private static final long serialVersionUID = -3094977414157589758L; private Vector content = null; private String[] title_name = { "ID", "姓名", "性别", "年龄" }; public Table_Model() {
content = new Vector();
} public Table_Model(int count) {
content = new Vector(count);
} public void addRow(String name, boolean sex, String age) {
Vector v = new Vector(4);
v.add(0, new Integer(content.size()));
v.add(1, name);
v.add(2, new Boolean(sex));
v.add(3, age);
content.add(v);
} public void removeRow(int row) {
content.remove(row);
} public void removeRows(int row, int count) {
for (int i = 0; i < count; i++) {
if (content.size() > row) {
content.remove(row);
}
}
}
/**
* 让表格中每个值都可修改,但需要setValueAt(Object value, int row, int col)方法配合才能是修改生效
*/
public boolean isCellEditable(int rowIndex, int columnIndex){
if(columnIndex == 0){ // 序列号不能修改
return false;
}
if(rowIndex < 5){ // 前5行不能修改
return false;
}
return true;
}
/**
* 使修改的内容生效
*/
public void setValueAt(Object value, int row, int col){
((Vector) content.get(row)).remove(col);
((Vector) content.get(row)).add(col, value);
this.fireTableCellUpdated(row, col);
} public String getColumnName(int col) {
return title_name[col];
} public int getColumnCount() {
return title_name.length;
} public int getRowCount() {
return content.size();
} public Object getValueAt(int row, int col) {
return ((Vector) content.get(row)).get(col);
}
/**
* 返回数据类型
*/
public Class getColumnClass(int col){
return getValueAt(0, col).getClass();
}}
/** Creates a new instance of StockTableModel */
public StockTableModel(int a,int b){
super(a,b);
setColumnIdentifiers(new String [] {"Col 1", "Col 2", "Col 3"});
}
Class[] types = new Class [] {
java.lang.Integer.class, java.lang.Integer.class, java.lang.Integer.class
};
boolean[] canEdit = new boolean [] {
false, true, false
};
public Class getColumnClass(int columnIndex) {
return types [columnIndex];
}
public boolean isCellEditable(int rowIndex, int columnIndex) {
return canEdit [columnIndex];
}
}
public class StockTableModel extends DefaultTableModel {
public boolean isCellEditable(int rowIndex, int columnIndex) {
return false;
}
}