如何在java netbeans中使用iscelleditable方法

时间:2018-03-14 08:19:46

标签: java mysql

嘿,我是Java新手所以我问我怎么能在java中实现is cell editable方法每次我尝试实现它时我得到一个数组索引超出界限错误我的表是随机生成的数据库设置&我想将第1第2列设置为不可编辑

//Get columns if data has been recorded for that ticket 
public void getColumns() {
    //jTable1.setModel(new DefaultTableModel(null,new String[]{""}));
    DefaultTableModel tableModel = new DefaultTableModel();
    String query="Select subjectName from subject where class ='"+c+"' order by subjectName asc";
    DefaultTableModel model=(DefaultTableModel)jTable1.getModel();
    try {
        PreparedStatement ps=conn.prepareStatement(query);
        ResultSet rs=ps.executeQuery(query);
        tableModel.addColumn("FirstName");//0
        tableModel.addColumn("MiddleName");//1
        tableModel.addColumn("SurName");//2
        tableModel.addColumn("Examdate");//3
        tableModel.addColumn("Examtype");//4
        tableModel.addColumn("Stream");//5
        tableModel.addColumn("Avg");//6
        tableModel.addColumn("Total");//7
        tableModel.addColumn("Position");//8

        while(rs.next()){
            //Local varibles are assigned to values gained in the database          
            String columnName=rs.getString(1);
            tableModel.addColumn(columnName);
        }

        jTable1.setModel(tableModel);
        jTable1.setModel(dtm);
    }
    catch(SQLException se){
        JOptionPane.showMessageDialog(null,se.getMessage());
    }

}    
DefaultTableModel dtm = new DefaultTableModel(0, 0) {
public boolean isCellEditable(int row, int column) {
return false;
}
};    

来自第1,第2,第3,第4和第5列的数据来自数据库,其余数据由用户手动输入 负责检索此数据的方法是

//gets names avg,examtype date strm and results
public void getResults(){
int pos=0;
int x=0;
int index=9;
String query="Select name,mname,sname,examtype,examdate,Stream,avg,total,pos "   
+ "from result"+c+" where examtype='"+en+"' and stream='"+strm+"'   
order by total desc";
DefaultTableModel model=(DefaultTableModel)jTable1.getModel();
try{
PreparedStatement ps=conn.prepareStatement(query);
ResultSet rs=ps.executeQuery(query);
while(rs.next()){
//Local varibles are assigned to values gained in the database

String fnamee=rs.getString(1);
String mnamee=rs.getString(2);
String snamee=rs.getString(3);
String examname=rs.getString(4);
String examdate=rs.getString(5);
String Stream=rs.getString(6);
double avg=rs.getDouble(7);
int total=rs.getInt(8);
pos++;
//Until seen false a row will be added and display data from database
model.addRow(new Object[]
{fnamee,mnamee,snamee,examname,examdate,Stream,avg,total,pos});
}
//Hides unwanted columns
jTable1.getColumnModel().getColumn(3).setMinWidth(0);
jTable1.getColumnModel().getColumn(3).setMaxWidth(0);
jTable1.getColumnModel().getColumn(3).setWidth(0);
jTable1.getColumnModel().getColumn(4).setMinWidth(0);
jTable1.getColumnModel().getColumn(4).setMaxWidth(0);
jTable1.getColumnModel().getColumn(4).setWidth(0);  
jTable1.getColumnModel().getColumn(5).setMinWidth(0);
jTable1.getColumnModel().getColumn(5).setMaxWidth(0);
jTable1.getColumnModel().getColumn(5).setWidth(0); 
//method responisble for udapting  the position
positionInsert();
}

见表 enter image description here

1 个答案:

答案 0 :(得分:0)

这是我的问题的解决方案我想出来了

   //Get columns if data has been recorded for that ticket 
    public void getColumns(){

        //Start
         DefaultTableModel tableModel = new DefaultTableModel() {
    @Override
    public boolean isCellEditable(int row, int column) {
        if(column<10){
        return false;
        }
        else{
           return true;
        }

    }
    };

    //end

     String query="Select subjectName from subject where class ='"+c+"' order by subjectName asc";

          try{
    PreparedStatement ps=conn.prepareStatement(query);
    ResultSet rs=ps.executeQuery(query);
    tableModel.addColumn("FirstName");//0
    tableModel.addColumn("MiddleName");//1
    tableModel.addColumn("SurName");//2
    tableModel.addColumn("Examdate");//3
    tableModel.addColumn("Examtype");//4
    tableModel.addColumn("Stream");//5
    tableModel.addColumn("Avg");//6
    tableModel.addColumn("Grade");//7
    tableModel.addColumn("Total");//8
    tableModel.addColumn("Position");//9
    while(rs.next()){
    //Local varibles are assigned to values gained in the database      
    String columnName=rs.getString(1);

   tableModel.addColumn(columnName);

   }

   //Sets new Tablemodel

  jTable1.setModel(tableModel);

    }