Results 1 to 4 of 4
  1. #1
    mine0926 is offline Senior Member
    Join Date
    Apr 2010
    Location
    Philippines
    Posts
    580
    Rep Power
    5

    Default Filter JTable using andFilter and orFilter

    hi, I am trying to use andFilter and orFilter simultaneously, Is it possible?
    My aim here is not to hide the row that has "MONITORS" and "MOUSE" on column#1.
    Here is a sample code of what I am trying to do, in rowFilter() method I commented the line that I am stacked-up

    Java Code:
    import java.awt.BorderLayout;
    import java.util.ArrayList;
    import javax.swing.*;
    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 AndOrFilter extends JFrame
    {
        private JPanel pnlFrame, pnlFields;
        private JTable table;
        private JTextField text1, text2;
        private JLabel label1, label2;
    
        public AndOrFilter()
        {
            setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            initComponents();
            addDocListener();
            pack();
        }
    
        private void initComponents()
        {
            pnlFields = new JPanel();
    
            label1 = new JLabel("BRAND: ");
            pnlFields.add(label1);
            text1 = new JTextField(25);
            pnlFields.add(text1);
            label2 = new JLabel("MODEL: ");
            pnlFields.add(label2);
            text2 = new JTextField(25);
            pnlFields.add(text2);
    
            label1.setLabelFor(text1);
            label2.setLabelFor(text2);
    
            
            pnlFrame = new JPanel(new BorderLayout());
            pnlFrame.add(pnlFields, BorderLayout.PAGE_START);
    
            Object[] colNames = {"COLUMN#1", "BRAND", "MODEL"};
            Object[][] rowData = {
                {"MONITORS", "", ""},
                {"", "LG", "D2468"},
                {"", "LG", "D2479"},
                {"", "AOC", "ABC"},
                {"", "AOC", "DEF"},
                {"", "AOC", "GHI"},
                {"", "DELL", "XYZ"},
                {"", "DELL", "ASD58"},
                {"", "SONY", "GH55884"},
                {"", "SONY", "GH55885"},
                {"", "SONY", "GH55894"},
                {"", "SONY", "GH55895"},
                {"PROCESSOR", "", ""},
                {"", "INTEL", "CORE2 DUO"},
                {"", "INTEL", "i3"},
                {"", "INTEL", "i5"},
                {"", "INTEL", "i7"},
            };
    
            DefaultTableModel model = new DefaultTableModel(rowData, colNames);
            table = new JTable(model);
            pnlFrame.add(table, BorderLayout.CENTER);
    
            add(pnlFrame);
        }
    
        private void addDocListener()
        {
            text1.getDocument().addDocumentListener(new DocumentListener()
            {
                public void insertUpdate(DocumentEvent e)
                {   rowFilter();
                }
    
                public void removeUpdate(DocumentEvent e)
                {   rowFilter();
                }
    
                public void changedUpdate(DocumentEvent e)
                {   rowFilter();
                }
            });
    
            text2.getDocument().addDocumentListener(new DocumentListener()
            {
                public void insertUpdate(DocumentEvent e)
                {   rowFilter();
                }
    
                public void removeUpdate(DocumentEvent e)
                {   rowFilter();
                }
    
                public void changedUpdate(DocumentEvent e)
                {   rowFilter();
                }
            });
        }
    
        private void rowFilter()
        {
            TableModel tm = (TableModel)table.getModel();
            TableRowSorter trs = new TableRowSorter(tm);
            table.setRowSorter(trs);
            ArrayList<RowFilter<Object,Object>> andFilterCol =
                new ArrayList<RowFilter<Object,Object>>();
            ArrayList<RowFilter<Object,Object>> orFilterCol =
                new ArrayList<RowFilter<Object,Object>>();
            try
            {
                orFilterCol.add(RowFilter.regexFilter("MONITOR", 0));
                orFilterCol.add(RowFilter.regexFilter("MOUSE", 0));
                if(text1.getText().length() > 0)
                {
                    andFilterCol.add(RowFilter.regexFilter(
                            "(?i)" + text1.getText(), 1));
                }
    
                if(text2.getText().length() > 0)
                {
                    andFilterCol.add(RowFilter.regexFilter(
                            "(?i)" + text2.getText(), 2));
                }
            }
            catch (java.util.regex.PatternSyntaxException e)
            {
                return;
            }
    
            ArrayList<RowFilter<Object,Object>> joinedFilter =
                new ArrayList<RowFilter<Object,Object>>();
    
            joinedFilter.add(RowFilter.andFilter(andFilterCol));
            joinedFilter.add(RowFilter.orFilter(orFilterCol));
    
            //[b]This is the part that I am stock, I do not know what to call or do
            //I know RowFilter.andFilter is not correct...[/b]
            //RowFilter filterer = RowFilter.andFilter(joinedFilter);
    
            trs.setRowFilter(filterer);
        }
    
        public static void main(String[] args)
        {
            AndOrFilter aoFilter = new AndOrFilter();
            aoFilter.setVisible(true);
        }
    
    }
    Thanks in advance.

  2. #2
    mine0926 is offline Senior Member
    Join Date
    Apr 2010
    Location
    Philippines
    Posts
    580
    Rep Power
    5

    Default

    Anyone that could suggest? Please let me know if I have to explain it more.
    Thanks.

  3. #3
    mine0926 is offline Senior Member
    Join Date
    Apr 2010
    Location
    Philippines
    Posts
    580
    Rep Power
    5

  4. #4
    mine0926 is offline Senior Member
    Join Date
    Apr 2010
    Location
    Philippines
    Posts
    580
    Rep Power
    5

    Default

    I was able to solve it by overiding include method.

    Java Code:
    import java.awt.BorderLayout;
    import java.util.ArrayList;
    import javax.swing.*;
    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 AndOrFilter extends JFrame
    {
        private JPanel pnlFrame, pnlFields;
        private JTable table;
        private JTextField text1, text2;
        private JLabel label1, label2;
    
        public AndOrFilter()
        {
            setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            initComponents();
            addDocListener();
            pack();
        }
    
        private void initComponents()
        {
            pnlFields = new JPanel();
    
            label1 = new JLabel("BRAND: ");
            pnlFields.add(label1);
            text1 = new JTextField(25);
            pnlFields.add(text1);
            label2 = new JLabel("MODEL: ");
            pnlFields.add(label2);
            text2 = new JTextField(25);
            pnlFields.add(text2);
    
            label1.setLabelFor(text1);
            label2.setLabelFor(text2);
    
    
            pnlFrame = new JPanel(new BorderLayout());
            pnlFrame.add(pnlFields, BorderLayout.PAGE_START);
    
            Object[] colNames = {"COLUMN#1", "BRAND", "MODEL"};
            Object[][] rowData = {{"MONITORS", "", ""},{"", "AOC", "ABC"},
                {"", "AOC", "DEF"},{"", "AOC", "GHI"},{"", "APPLE", "C-ALPP"},
                {"", "LG", "D2468"},{"", "LG", "D2479"},{"", "DELL", "XYZ"},
                {"", "DELL", "ASD58"},{"", "SONY", "GH55884"},{"", "SONY", "GH55885"},
                {"", "SONY", "GH55894"},{"", "SONY", "GH55895"},
                {"PROCESSOR", "", ""},{"", "INTEL", "CORE2 DUO"},
                {"", "INTEL", "i3"},{"", "INTEL", "i5"},{"", "INTEL", "i7"}};
            
            DefaultTableModel model = new DefaultTableModel(rowData, colNames);
            table = new JTable(model);
            pnlFrame.add(table, BorderLayout.CENTER);
    
            add(pnlFrame);
        }
    
        private void addDocListener()
        {
            text1.getDocument().addDocumentListener(new DocumentListener()
            {
                public void insertUpdate(DocumentEvent e)
                {   rowFilter();
                }
    
                public void removeUpdate(DocumentEvent e)
                {   rowFilter();
                }
    
                public void changedUpdate(DocumentEvent e)
                {   rowFilter();
                }
            });
    
            text2.getDocument().addDocumentListener(new DocumentListener()
            {
                public void insertUpdate(DocumentEvent e)
                {   rowFilter();
                }
    
                public void removeUpdate(DocumentEvent e)
                {   rowFilter();
                }
    
                public void changedUpdate(DocumentEvent e)
                {   rowFilter();
                }
            });
        }
    
        private void rowFilter()
        {
            RowFilter filterer = new RowFilter()
            {
                @Override
                public boolean include(Entry entry)
                {
                    //ArrayList that will hold txtFields' Value and its index
                    ArrayList<String> txtField = new ArrayList<String>();
                    if(text1.getText().length() > 0)
                    {
                        txtField.add(text1.getText() + "\t1");
                    }
                    if(text2.getText().length() > 0)
                    {
                        txtField.add(text2.getText() + "\t2");
                    }
                    /*
                     * If column zero(0) of table is equals to MONITOR or MOUSE then
                     * (if has value) then do not hide row
                     * else check row's validity
                     *
                     * @return:
                     *      true - show entry
                     *      false - hide entry
                     */
                    String entryValue = entry.getStringValue(0);
                    if(entryValue.length() > 0){   return true;    }
    
                    /* 
                     * Check if entry starts with txtField
                     */
                    for(int i = 0; i < txtField.size(); i++)
                    {
                        String[] txtFieldValue = txtField.get(i).
                                toUpperCase().split("\t");
                        int colToCheck = Integer.parseInt(txtFieldValue[1]);
                        entryValue = entry.getStringValue(colToCheck);
                        if(!entryValue.startsWith(txtFieldValue[0]))
                        {   return false;
                        }
                    }
                    return true;
                }
            };
            TableModel tm = (TableModel)table.getModel();
            TableRowSorter trs = new TableRowSorter(tm);
            table.setRowSorter(trs);
            trs.setRowFilter(filterer);
    
        }
    
        public static void main(String[] args)
        {
            AndOrFilter aoFilter = new AndOrFilter();
            aoFilter.setVisible(true);
        }
    }

    If you have suggestions on how to improve this, I will appreciate it

Similar Threads

  1. update jtable with filter
    By simo_mon in forum AWT / Swing
    Replies: 7
    Last Post: 06-15-2010, 05:53 AM
  2. JTable with "Filter" like EXCEL
    By atom86 in forum AWT / Swing
    Replies: 1
    Last Post: 10-14-2009, 11:18 AM
  3. Sort and filter in Jtable (java ver 1.5 )
    By itaipee in forum AWT / Swing
    Replies: 3
    Last Post: 04-16-2009, 01:03 PM
  4. web content filter or internet filter
    By sundarjothi in forum Advanced Java
    Replies: 3
    Last Post: 05-15-2008, 12:36 PM
  5. Replies: 2
    Last Post: 05-15-2008, 11:15 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •