2014-09-25 75 views
1

我有JFrame一個JTableJComboBox有選擇如何針對特定列過濾JTable?

  • 「別代碼」
  • 「離開描述」

我需要相對於過濾表中的數據離開代碼休假代碼時在組合框中被選中並且當在組合框中選擇離開描述時針對leavedesc進行過濾。

這是我用來過濾數據的功能:

private void filtervalue(String filterString) { 
    TableModel model = tableLeave.getModel(); 
    final TableRowSorter<TableModel> sorter = new TableRowSorter<TableModel>(model); 
    tableLeave.setRowSorter(sorter); 
    if (filterString.length() == 0) { 
     sorter.setRowFilter(null); 
    } else { 
     sorter.setRowFilter(RowFilter.regexFilter("(?i)" + filterString)); 
    } 
} 

但問題是它會通過leavedesc排序以及leavecode,所以在組合框中沒有影響。請幫我實施基於列的過濾器。

+2

考慮提供一個[runnable示例](https://stackoverflow.com/help/mcve),它演示你的問題。這將導致更少的混淆和更好的反應 – MadProgrammer 2014-09-25 05:45:19

+0

您的過濾器將過濾所有列,您應該提供您要過濾的列索引(s)... – MadProgrammer 2014-09-25 05:46:51

+0

感謝您的回覆 – 2014-09-25 06:14:58

回答

3

沒有一個可運行的例子,我不能100%確定,但我猜想每次更改過濾器文本時設置RowSorter都會導致表重新排序。

相反,設置TableRowSorter在創建JTable,並簡單地更新過濾器,例如...

import java.awt.BorderLayout; 
import java.awt.EventQueue; 
import java.awt.GridBagLayout; 
import java.awt.event.ActionEvent; 
import java.awt.event.ActionListener; 
import javax.swing.JComboBox; 
import javax.swing.JFrame; 
import javax.swing.JPanel; 
import javax.swing.JScrollPane; 
import javax.swing.JTable; 
import javax.swing.JTextField; 
import javax.swing.RowFilter; 
import javax.swing.UIManager; 
import javax.swing.UnsupportedLookAndFeelException; 
import javax.swing.event.DocumentEvent; 
import javax.swing.event.DocumentListener; 
import javax.swing.table.DefaultTableModel; 
import javax.swing.table.TableModel; 
import javax.swing.table.TableRowSorter; 

public class TableFilter { 

    public static void main(String[] args) { 
     new TableFilter(); 
    } 

    private JTable table; 
    private JComboBox filterBy; 
    private JTextField filterText; 

    public TableFilter() { 
     EventQueue.invokeLater(new Runnable() { 
      @Override 
      public void run() { 
       try { 
        UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName()); 
       } catch (ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException ex) { 
        ex.printStackTrace(); 
       } 

       DefaultTableModel model = new DefaultTableModel(new Object[]{"Code", "Description"}, 0); 
       model.addRow(new Object[]{"A001", "Holidays"}); 
       model.addRow(new Object[]{"B001", "Sick"}); 
       model.addRow(new Object[]{"A002", "Zombitse"}); 
       model.addRow(new Object[]{"C001", "Crazy bin"}); 
       model.addRow(new Object[]{"C002", "Postal"}); 
       model.addRow(new Object[]{"D002", "Job Interview"}); 
       model.addRow(new Object[]{"D004", "it's sunny outside"}); 

       table = new JTable(model); 
       table.setRowSorter(new TableRowSorter<TableModel>(model)); 

       JPanel filterPane = new JPanel(new GridBagLayout()); 
       filterBy = new JComboBox(new Object[]{"Nothing", "Code", "Description"}); 
       filterText = new JTextField(20); 
       filterPane.add(filterBy); 
       filterPane.add(filterText); 

       filterBy.setSelectedIndex(0); 
       filterBy.addActionListener(new ActionListener() { 
        @Override 
        public void actionPerformed(ActionEvent e) { 
         updateFilter(); 
        } 
       }); 

       filterText.getDocument().addDocumentListener(new DocumentListener() { 
        @Override 
        public void insertUpdate(DocumentEvent e) { 
         updateFilter(); 
        } 

        @Override 
        public void removeUpdate(DocumentEvent e) { 
         updateFilter(); 
        } 

        @Override 
        public void changedUpdate(DocumentEvent e) { 
         updateFilter(); 
        } 
       }); 

       JFrame frame = new JFrame("Testing"); 
       frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
       frame.add(filterPane, BorderLayout.NORTH); 
       frame.add(new JScrollPane(table)); 
       frame.pack(); 
       frame.setLocationRelativeTo(null); 
       frame.setVisible(true); 
      } 
     }); 
    } 

    protected void updateFilter() { 
     Object selected = filterBy.getSelectedItem(); 
     TableRowSorter<TableModel> sorter = (TableRowSorter<TableModel>) table.getRowSorter(); 
     String text = "(?i)" + filterText.getText(); 
     if ("Nothing".equals(selected)) { 
      sorter.setRowFilter(null); 
     } else { 
      int col = -1; 
      if ("Code".equals(selected)) { 
       col = 0; 
      } else if ("Description".equals(selected)) { 
       col = 1; 
      } 
      sorter.setRowFilter(RowFilter.regexFilter(text, col)); 
     } 
    } 

} 

這個例子演示了實時更新,所以你鍵入,該表將被過濾。 ..

+0

hmmmm - '如何過濾關於JTable你有沒有閱讀'從JComboBox中過濾'(RowFilter中的'和'和'nor') – mKorbel 2014-09-25 08:55:40

+0

@mKorbel *「我需要在離開代碼時過濾關於離開代碼的表數據在組合框中選擇,並在組合框中選擇離開描述時針對leavedesc進行過濾「* - 因此,當選擇」代碼「時,數據將根據reg-expr僅針對代碼列和相同的描述...不同於OP的代碼,它是通過所有列過濾... – MadProgrammer 2014-09-25 09:56:50

+0

好或不好,它應該/可以,但管默認有兩個條目:-),然後沒有(從隧道右側)通信由OP – mKorbel 2014-09-25 11:25:34