Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 2 of 2

Thread: Filter JTable using andFilter and orFilter

  1. #1
    Junior Member
    Join Date
    Dec 2010
    Posts
    5
    Thanks
    0
    Thanked 0 Times in 0 Posts

    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
    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));
     
            //This is the part that I am stock, I do not know what to call or do
            //I know RowFilter.andFilter is not correct...
            //RowFilter filterer = RowFilter.andFilter(joinedFilter);
     
            trs.setRowFilter(filterer);
        }
     
        public static void main(String[] args)
        {
            AndOrFilter aoFilter = new AndOrFilter();
            aoFilter.setVisible(true);
        }
     
    }
    Cross-posted: http://www.java-forums.org/new-java/...-orfilter.html


  2. #2
    Junior Member
    Join Date
    Dec 2010
    Posts
    5
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Filter JTable using andFilter and orFilter

    I was able to solve it by overiding include method.

    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. How to set Document Filter
    By nimishalex in forum AWT / Java Swing
    Replies: 0
    Last Post: December 27th, 2010, 03:32 AM
  2. Hello everyone! I need help with JTable
    By hektor in forum AWT / Java Swing
    Replies: 6
    Last Post: October 11th, 2010, 10:11 AM
  3. JTable Row Color
    By aussiemcgr in forum Java Theory & Questions
    Replies: 2
    Last Post: October 8th, 2010, 03:59 PM
  4. Word filter assignment help
    By normandygahn in forum What's Wrong With My Code?
    Replies: 3
    Last Post: September 15th, 2010, 07:42 AM
  5. Internet Filter to display some website
    By sundarjothi in forum JavaServer Pages: JSP & JSTL
    Replies: 1
    Last Post: May 15th, 2008, 05:03 AM