2012-01-13 84 views
2

當我們從同一行中選擇時,設置爲監聽單元格選擇事件的JTable不會生成事件,如果我選擇除選定單元格之外的其他行,Jtable引發事件。設置爲監聽單元格選擇事件的Jtable不會觸發事件

這是代碼... 示例當我選擇單元格「row0 col0」,然後嘗試選擇同一行中的任何其他單元格時,jtable將不會觸發任何事件, 以便觸發事件「行0第1欄」細胞,我必須選擇在第2行的任何細胞,並且具有點擊「排0 COL1」細胞

public class test_jtable_event extends javax.swing.JFrame { 

    /** Creates new form test_jtable_event */ 
    public test_jtable_event() { 
     initComponents(); 
    } 



// <editor-fold defaultstate="collapsed" desc="Generated Code"> 
private void initComponents() { 

    jScrollPane1 = new javax.swing.JScrollPane(); 
    jTable1 = new javax.swing.JTable(); 

    setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE); 

    jTable1.setModel(new javax.swing.table.DefaultTableModel(
     new Object [][] { 
      {"row 0 column1", "row 0column 2", "row 0 column 3", "row 0column4"}, 
      {"row 1 column 0", "row 1 column 2", "row 1 column 2", "row 1 column 2"}, 
      {null, null, null, null}, 
      {null, null, null, null} 
     }, 
     new String [] { 
      "Title 1", "Title 2", "Title 3", "Title 4" 
     } 
    )); 
    jTable1.getSelectionModel().addListSelectionListener(new ListSelectionListener() 
     { 
      public void valueChanged(ListSelectionEvent e) 
      { 
       System.out.println(e.getSource()); 
       System.out.println(jTable1.getSelectedColumn()); 
       System.out.println(jTable1.getSelectedRow()); 
      } 

     }); 
     jTable1.setCellSelectionEnabled(true); 
     jScrollPane1.setViewportView(jTable1); 

     javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane()); 
     getContentPane().setLayout(layout); 
     layout.setHorizontalGroup(
      layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) 
      .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup() 
       .addContainerGap(13, Short.MAX_VALUE) 
       .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 375, javax.swing.GroupLayout.PREFERRED_SIZE) 
       .addContainerGap()) 
     ); 
     layout.setVerticalGroup(
      layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) 
      .addGroup(layout.createSequentialGroup() 
       .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 275, javax.swing.GroupLayout.PREFERRED_SIZE) 
       .addContainerGap(25, Short.MAX_VALUE)) 
     ); 

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

public static void main(String args[]) { 

    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(test_jtable_event.class.getName()).log(java.util.logging.Level.SEVERE, null, ex); 
    } catch (InstantiationException ex) { 
     java.util.logging.Logger.getLogger(test_jtable_event.class.getName()).log(java.util.logging.Level.SEVERE, null, ex); 
    } catch (IllegalAccessException ex) { 
     java.util.logging.Logger.getLogger(test_jtable_event.class.getName()).log(java.util.logging.Level.SEVERE, null, ex); 
    } catch (javax.swing.UnsupportedLookAndFeelException ex) { 
     java.util.logging.Logger.getLogger(test_jtable_event.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 test_jtable_event().setVisible(true); 
     } 
    }); 
} 
// Variables declaration - do not modify 
private javax.swing.JScrollPane jScrollPane1; 
private javax.swing.JTable jTable1; 
// End of variables declaration 
} 
+2

請學習Java命名約定並嚴格遵守。 – kleopatra 2012-01-13 11:12:39

回答

5

行和列選擇由不同的選擇模型來控制:

  • row:table.getSelectionModel()
  • 柱:table.getColumnModel()getSelectionModel()

你有你的聽衆註冊到兩個