已结贴√
问题点数:20 回复次数:9
新人求解,为什么表格无法编辑
import java.awt.*;
import javax.swing.*;
import javax.swing.table.AbstractTableModel;
import javax.swing.table.DefaultTableModel;
public class tb extends AbstractTableModel{//表格定义
String[] head={"会员","xm","check","year","ye","color"};
Class[] typeArray={Object.class,Object.class,Boolean.class,Object.class,Double.class,Color.class};
Object[] data1={"1001","dd",false,23,1577,Color.ORANGE};
Object[] data2={"1001","dd",false,24,1577,Color.ORANGE};
Object[] data3={"1001","dd",false,25,1577,Color.ORANGE};
Object[] data4={"1001","dd",false,26,1577,Color.ORANGE};
Object[][]data={data1,data2,data3,data4};
public int getColumnCount(){
return head.length;
}
public int getRowCount(){
return data.length;
}
public String getColumnName(int col){
return head[col];
}
public Object getValueAt(int r,int c){
return data[r][c];
}
public Class getColumnClass(int c){
return typeArray[c];
}
public Boolean isCellEdittable(int r,int c){
return true;
}
public void setValueAt(Object value,int r,int c){
data[r][c]=value;
this.fireTableCellUpdated(r,c);
}
}
import javax.swing.*;
import javax.swing.table.*;
import javax.swing.event.*;
public class tb1 extends JFrame {//主调程序
tb tby=new tb();
JTable table =new JTable(tby);
TableListener tablelistener=new TableListener();
tb1(){
table.getSelectionModel().addListSelectionListener(tablelistener);
table.getColumnModel().addColumnModelListener(tablelistener);
table.getModel().addTableModelListener(tablelistener);
this.add(table);
this.setBounds(200,200,700,300);
this.setVisible(true);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
public static void main(String[] args){
new tb1();
}
}
class TableListener implements ListSelectionListener,TableColumnModelListener,TableModelListener{
int row=0;
int col=0;
JTable table;
@Override
public void tableChanged(TableModelEvent e) {
String str=(String) table.getValueAt(row,col);
System.out.println(str);
}
@Override
public void columnAdded(TableColumnModelEvent e) {
// TODO Auto-generated method stub
}
@Override
public void columnRemoved(TableColumnModelEvent e) {
// TODO Auto-generated method stub
}
@Override
public void columnMoved(TableColumnModelEvent e) {
// TODO Auto-generated method stub
}
@Override
public void columnMarginChanged(ChangeEvent e) {
// TODO Auto-generated method stub
}
@Override
public void columnSelectionChanged(ListSelectionEvent e) {
col= table.getSelectedColumn();
}
@Override
public void valueChanged(ListSelectionEvent e) {
row=table.getSelectedRow();
}
}
所有源码都已贴出,是什么地方出错,望各位帮忙,谢谢