2017-07-30 39 views
1

我想编辑一个特定的单元格,所以在编辑isCellEditable方法之后,当我调用isCellEditable组件时,它的编辑值为true,但它不编辑该特定值。请帮助我。正如传递行值和列值为0和0.我该怎么做才能使特定的单元格可编辑?isCellEditable无法正常工作Swing

package javaapplication2; 

import javax.swing.table.DefaultTableModel; 

/** 
* 
* @author Himanshu 
*/ 
public class NewJFrame extends javax.swing.JFrame { 
public boolean edit = false; 
    /** 
    * Creates new form NewJFrame 
    */ 
    public NewJFrame() { 
     initComponents(); 
    } 

    /** 
    * This method is called from within the constructor to initialize the form. 
    * WARNING: Do NOT modify this code. The content of this method is always 
    * regenerated by the Form Editor. 
    */ 
    @SuppressWarnings("unchecked") 
    // <editor-fold defaultstate="collapsed" desc="Generated Code">       
    private void initComponents() { 

     jTabbedPane1 = new javax.swing.JTabbedPane(); 
     jPanel1 = new javax.swing.JPanel(); 
     jScrollPane1 = new javax.swing.JScrollPane(); 
     jTable1 = new javax.swing.JTable(); 
     jButton2 = new javax.swing.JButton(); 
     jButton1 = new javax.swing.JButton(); 

     setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE); 

     jPanel1.setLayout(new java.awt.GridLayout(3, 1)); 

     jTable1.setModel(new javax.swing.table.DefaultTableModel(
      new Object [][] { 
       {null, null, null, null}, 
       {null, null, null, null}, 
       {null, null, null, null}, 
       {null, null, null, null} 
      }, 
      new String [] { 
       "Title 1", "Title 2", "Title 3", "Title 4" 
      } 
     ) { 
      Class[] types = new Class [] { 
       java.lang.String.class, java.lang.String.class, java.lang.String.class, java.lang.String.class 
      }; 
      boolean[] canEdit = new boolean [] { 
       false, false, false, false 
      }; 

      public Class getColumnClass(int columnIndex) { 
       return types [columnIndex]; 
      } 
      public boolean isCellEditable(int rowIndex, int columnIndex) { 
       if(edit == true){ 
        return true; 
       } 
       else{ 
        return false; 
       } 
      } 
     } 
    ); 
    jScrollPane1.setViewportView(jTable1); 

    jPanel1.add(jScrollPane1); 

    jButton2.setText("jButton2"); 
    jButton2.addActionListener(new java.awt.event.ActionListener() { 
     public void actionPerformed(java.awt.event.ActionEvent evt) { 
      jButton2ActionPerformed(evt); 
     } 
    }); 
    jPanel1.add(jButton2); 

    jButton1.setText("jButton1"); 
    jButton1.addActionListener(new java.awt.event.ActionListener() { 
     public void actionPerformed(java.awt.event.ActionEvent evt) { 
      jButton1ActionPerformed(evt); 
     } 
    }); 
    jPanel1.add(jButton1); 

    jTabbedPane1.addTab("tab1", jPanel1); 

    javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane()); 
    getContentPane().setLayout(layout); 
    layout.setHorizontalGroup(
     layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) 
     .addComponent(jTabbedPane1, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.DEFAULT_SIZE, 771, Short.MAX_VALUE) 
    ); 
    layout.setVerticalGroup(
     layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) 
     .addComponent(jTabbedPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 432, Short.MAX_VALUE) 
    ); 

    pack(); 
    }// </editor-fold>       

    private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {           
     // TODO add your handling code here: 
    }           

    private void jButton2ActionPerformed(java.awt.event.ActionEvent evt) {           
     // TODO add your handling code here: 
    }           

    /** 
    * @param args the command line arguments 
    */ 
    public static void main(String args[]) { 
     /* Set the Nimbus look and feel */ 
     //<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) "> 
     /* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel. 
     * For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html 
     */ 
     try { 
      for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) { 
       if ("Nimbus".equals(info.getName())) { 
        javax.swing.UIManager.setLookAndFeel(info.getClassName()); 
        break; 
       } 
      } 
     } catch (ClassNotFoundException ex) { 
      java.util.logging.Logger.getLogger(NewJFrame.class.getName()).log(java.util.logging.Level.SEVERE, null, ex); 
     } catch (InstantiationException ex) { 
      java.util.logging.Logger.getLogger(NewJFrame.class.getName()).log(java.util.logging.Level.SEVERE, null, ex); 
     } catch (IllegalAccessException ex) { 
      java.util.logging.Logger.getLogger(NewJFrame.class.getName()).log(java.util.logging.Level.SEVERE, null, ex); 
     } catch (javax.swing.UnsupportedLookAndFeelException ex) { 
      java.util.logging.Logger.getLogger(NewJFrame.class.getName()).log(java.util.logging.Level.SEVERE, null, ex); 
     } 
     //</editor-fold> 

     /* Create and display the form */ 
     java.awt.EventQueue.invokeLater(new Runnable() { 
      public void run() { 
       new NewJFrame().setVisible(true); 
      } 
     }); 
     NewJFrame n = new NewJFrame(); 
     n.initial(); 
    } 
    public void initial(){ 

     // String [][] data = new String[][]{{"1","2","3","4"},{"1","2","3","4"},{"1","2","3","4" }}; 
     DefaultTableModel model = (DefaultTableModel) jTable1.getModel(); 
     edit = true; 
     model.isCellEditable(0, 0); 

    } 

    // Variables declaration - do not modify      
    private javax.swing.JButton jButton1; 
    private javax.swing.JButton jButton2; 
    private javax.swing.JPanel jPanel1; 
    private javax.swing.JScrollPane jScrollPane1; 
    private javax.swing.JTabbedPane jTabbedPane1; 
    private javax.swing.JTable jTable1; 
    // End of variables declaration     
} 

回答

1

你应该这样做

java.awt.EventQueue.invokeLater(new Runnable() { 
     public void run() { 
      final NewJFrame frame = new NewJFrame(); 
      frame.initial(); 
      frame.setVisible(true); 
     } 
    }); 
/*  NewJFrame n = new NewJFrame(); 
     n.initial();*/ // delete the commented code 

你以前的代码新的两个框架,一个可编辑的,但不可见n,一个可见的,但不能编辑。


对于仅(0,0)是可编辑的,isCellEditable方法的呼叫是没有用的,它是没有副作用和纯它只是返回一个布尔值。

你可以在你的isCellEditable法的实施改变

public boolean isCellEditable(int rowIndex, int columnIndex) { 
return edit && (rowIndex == 0 && columnIndex == 0); 
} 

当然,你总是可以选择不硬编码数字,写这样的方法setEditableCell和变化return edit && (rowIndex == rowNum && columnIndex == columnNum);

+0

谢谢@aristotll。但是,当我在单元格可编辑函数中传递(0,0)时,还有一个问题,但它使所有单元格都可编辑,而不是特定的单元格。你能帮忙吗? – himanshu2212

+0

@ himanshu2212我已经更新了我的答案。 – aristotll

+0

非常感谢@ aristotll。现在明白了 – himanshu2212