2015-04-28 72 views
0

我在做我的项目,并且存在这个问题。我无法选择整行。我尝试了setRowSelectionAllowed(true)并重写isCellEditable(),但没有任何真正的工作。如何选择整个行而不是单个单元格

我有这段代码。

我有一个表格,首先显示我所有的手头产品。

 String query = "select * from tbl_items where status = 'Available'"; 
     Connection cn_oh_readRows = null; 
     Connection cn_oh_readContent = null; 

     int countRows = 0; 
     try { 
      Class.forName("com.mysql.jdbc.Driver"); 
      cn_oh_readRows = DriverManager.getConnection("jdbc:mysql://localhost/cashiering and inventory system","root","");   
      Statement state = cn_oh_readRows.createStatement(); 

      ResultSet rowsCount = state.executeQuery(query); 

      while (rowsCount.next()){ 
       countRows++; 
      } 
     } 
     catch(Exception ex){} 

     Object [][] rows_data = new Object [countRows][7]; 

     try { 
      Class.forName("com.mysql.jdbc.Driver"); 
      cn_oh_readContent = DriverManager.getConnection("jdbc:mysql://localhost/cashiering and inventory system","root","");    
      Statement state = cn_oh_readContent.createStatement(); 


      ResultSet rowsRead = state.executeQuery(query); 

      int c = 0; 
      while (rowsRead.next()) { 
       rows_data[c][0] = rowsRead.getString(1); 
       rows_data[c][1] = rowsRead.getString(2); 
       rows_data[c][2] = rowsRead.getString(3); 
       rows_data[c][3] = rowsRead.getString(4); 
       rows_data[c][4] = rowsRead.getString(5); 
       rows_data[c][5] = rowsRead.getString(6); 
       rows_data[c][6] = rowsRead.getString(7); 
       c++; 
      } 
     } 
     catch(Exception ex){}; 

     Object columnNames [] = {"Item code", "Item name", "Brand", "Original Price", "Retail Price", "Quantity", "Classification"}; 

     DefaultTableModel tableModel = new DefaultTableModel(rows_data, columnNames){ 
      @Override 
      public boolean isCellEditable(int row, int column) { 
       return false; 
      } 
     }; 

     tbl_onHand = new JTable(rows_data, columnNames); 
     tbl_onHand.setAutoCreateRowSorter(true); 
     tbl_onHand.setShowGrid(false); 
     tbl_onHand.getTableHeader().setReorderingAllowed(false); 
     tbl_onHand.getTableHeader().setResizingAllowed(false); 
     tbl_onHand.setModel(tableModel); 
     tbl_onHand.setRowSelectionAllowed(true); 
     tbl_onHand.setSelectionBackground(Color.pink); 


     sp_tbl_onHand = new JScrollPane(tbl_onHand); 
     sp_tbl_onHand.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED); 
     sp_tbl_onHand.setHorizontalScrollBarPolicy(JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED); 
     sp_tbl_onHand.setBounds(0, 40, 780, 232); 

然后有这个JTextField用于搜索。

txt_search_onHand = new JTextField(); 
    txt_search_onHand.setBorder(new border_round().round); 
    txt_search_onHand.setBounds(2, 15, 210, 20); 

我有这个DocumentListener在字符串插入到JtextField时更改JTable的值。

DocumentListener dl_onHand = new DocumentListener() { 
     @Override 
     public void removeUpdate(DocumentEvent e) {proc_onHand();} 
     @Override 
     public void insertUpdate(DocumentEvent e) {proc_onHand();} 
     @Override 
     public void changedUpdate(DocumentEvent e) {} 
    }; 

这是proc_onHand():

private void proc_onHand() { 
    String search_onHand = txt_search_onHand.getText(); 

    String query_onHand = "select * from tbl_items where status = 'Available' and item_code like '%" + search_onHand + "%' or " 
      + "status = 'Available' and name like '%"+ search_onHand +"%' or " 
      + "status = 'Available' and brand like '%" + search_onHand + "%' or " 
      + "status = 'Available' and classification like '%" + search_onHand + "%'"; 


    Connection cn_oh_readRows = null; 
    Connection cn_oh_readContent = null; 

    int countRows = 0; 
    try { 
     Class.forName("com.mysql.jdbc.Driver"); 
     cn_oh_readRows = DriverManager.getConnection("jdbc:mysql://localhost/cashiering and inventory system","root","");   
     Statement state = cn_oh_readRows.createStatement(); 

     ResultSet rowsCount = state.executeQuery(query_onHand); 

     while (rowsCount.next()){ 
      countRows++; 
     } 
    } 
    catch(Exception ex){} 

    Object [][] rows_data = new Object [countRows][7]; 

    try { 
     Class.forName("com.mysql.jdbc.Driver"); 
     cn_oh_readContent = DriverManager.getConnection("jdbc:mysql://localhost/cashiering and inventory system","root","");    
     Statement state = cn_oh_readContent.createStatement(); 


     ResultSet rowsRead = state.executeQuery(query_onHand); 

     int c = 0; 
     while (rowsRead.next()) { 
      rows_data[c][0] = rowsRead.getString(1); 
      rows_data[c][1] = rowsRead.getString(2); 
      rows_data[c][2] = rowsRead.getString(3); 
      rows_data[c][3] = rowsRead.getString(4); 
      rows_data[c][4] = rowsRead.getString(5); 
      rows_data[c][5] = rowsRead.getString(6); 
      rows_data[c][6] = rowsRead.getString(7); 
      c++; 
     } 
    } 
    catch(Exception ex){}; 


    Object columnNames [] = {"Item code", "Item name", "Brand", "Original Price", "Retail Price", "Quantity", "Classification"}; 

    DefaultTableModel tableModel = new DefaultTableModel(rows_data, columnNames){ 
     @Override 
     public boolean isCellEditable(int row, int column) { 
      return false; 
     } 
    }; 

    tbl_onHand = new JTable(rows_data, columnNames); 
    tbl_onHand.setAutoCreateRowSorter(true); 
    tbl_onHand.setShowGrid(false); 
    tbl_onHand.setSelectionBackground(Color.pink); 
    tbl_onHand.getTableHeader().setReorderingAllowed(false); 
    tbl_onHand.getTableHeader().setResizingAllowed(false); 
    tbl_onHand.setModel(tableModel); 
    tbl_onHand.setRowSelectionAllowed(true); 

    sp_tbl_onHand = new JScrollPane(tbl_onHand); 
    sp_tbl_onHand.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED); 
    sp_tbl_onHand.setHorizontalScrollBarPolicy(JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED); 
    sp_tbl_onHand.setBounds(0, 40, 780, 232); 


    pnl_onHand.add(sp_tbl_onHand, BorderLayout.CENTER); 
} 

我可以在桌子上的临时查看选择一整行,其中它显示所有的手头产品。但是,当我向txt_search_onHand插入一个字符串以过滤JTable中的值时,JTable中的整行结果不能被选中,而是被选中,但只是少数几个单元。我必须拖动我的鼠标来选择所有单元格。

请帮帮我!我做得好吗?请指导我。我是Java新手!谢谢。

回答

0
table.setSelectionModel(ListSelectionModel.SINGLE_SELECTION); 
相关问题