How to filter a table based on the values in a column in Java?

Hi,
I have a table that I create in Java. I populate some text in the fields of the table and now I would like to create a filter for this table.

This is how I create this table:

    public static void populateResultsTable(List<CheckDetail> checkDetails, Shell shell, Table table, String[] titles){
            
        boolean evenRow = true;
        for (CheckDetail checkDetail : checkDetails) {
            TableItem item = new TableItem(table, SWT.NULL);
            item.setText(0, checkDetail.getStatus()); //Status
            item.setText(1, checkDetail.getName()); //Check
            item.setText(2, checkDetail.getFileName()); //Filename
            item.setText(3, checkDetail.getExistingLineNumbers()); //Existing Line Numbers
            item.setText(4, checkDetail.getNewLineNumbers()); //New Line Numbers
            
            //This is to make shadow for every other line
            if (evenRow)
                item.setBackground(shell.getDisplay().getSystemColor(SWT.COLOR_WIDGET_LIGHT_SHADOW));
            evenRow = !evenRow;
         }
    }

Open in new window


Now let's assume this is the table that it creates:

Status   | Check | FileName | ExistingLine Numbers | New Line Numbers
PASSED   check1   file1              4,56,78                             3,45,67
FAILED    check2   file2              6                                       
PASSED   check3   file3              9                                       39
PASSED   check4   file4              23                                     47

Open in new window


So, how can I create a filter (actually, I already have checkboxes for it) to show only let's say the FAILED ones in the table.

This is the expected result:

Status   | Check | FileName | ExistingLine Numbers | New Line Numbers
FAILED    check2   file2              6                                       

Open in new window


This is the code that I have. It creates the 3 vertical checkboxes:
public class CSFilteringCheckBoxes extends Composite {
    private Button error = new Button(this, SWT.CHECK);
    private Button failed = new Button(this, SWT.CHECK);
    private Button passed = new Button(this, SWT.CHECK);
    private Label caption = new Label(this, SWT.HORIZONTAL | SWT.CENTER);

    public CSFilteringCheckBoxes(Composite parent, int style) {
        super(parent, style);
        Font font = new Font(parent.getDisplay(),"Arial",9, SWT.NORMAL);
        final Color lightgray;
        lightgray = new Color (parent.getDisplay(), 140, 140, 140);
        
        GridLayout gl = new GridLayout(); gl.verticalSpacing = 4;
        setLayout(gl);
        error.setText("Error");
        failed.setText("Failed");
        passed.setText("Passed");
        caption.setText("Filtering");
        //caption.setForeground(lightgray);
        caption.setFont(font);
        
        
        error.setSelection(true);
        failed.setSelection(true);
        passed.setSelection(false);
        
        caption.setLayoutData(new GridData(GridData.FILL_HORIZONTAL)); // center the caption
        setBackground(parent.getDisplay().getSystemColor(SWT.COLOR_GRAY));
        error.setBackground(parent.getDisplay().getSystemColor(SWT.COLOR_GRAY));
        failed.setBackground(parent.getDisplay().getSystemColor(SWT.COLOR_GRAY));
        passed.setBackground(parent.getDisplay().getSystemColor(SWT.COLOR_GRAY));
        caption.setBackground(parent.getDisplay().getSystemColor(SWT.COLOR_GRAY));
        
    }

}

Open in new window


When I mark a checkbox as checked, I would like to populate the relevant information on the table.

How can I do it? Can you please show me?
TolgarAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Amitkumar PSr. ConsultantCommented:
Use can try exclude property... first set it to true..  when need to filter, set to false, filter it, reset to true.

Hope this works for you.
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
TolgarAuthor Commented:
I would appreciate if you could show me how I will implement it in my code?

Thanks,
0
zzynxSoftware engineerCommented:
Small demonstration app you can just run.
It should give you an idea of how to apply a filter on the rows of a table.

import javax.swing.*;
import javax.swing.table.DefaultTableModel;
import javax.swing.table.TableModel;
import javax.swing.table.TableRowSorter;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

public class JTableFilterDemo {
    public static void main(String[] args) {
        Object[][] data = { { "A", 5 }, { "B", 2 }, { "C", 4 }, { "D", 8 } };
        String columnNames[] = { "Item", "Value" };
        TableModel model = new DefaultTableModel(data, columnNames) {
            public Class<?> getColumnClass(int column) {
                return getValueAt(0, column).getClass();
            }
        };
        JTable table = new JTable(model);

        RowFilter<Object, Object> filter = new RowFilter<Object, Object>() {
            public boolean include(Entry entry) {
                Integer population = (Integer) entry.getValue(1);
                return population.intValue() > 4;
            }
        };

        TableRowSorter<TableModel> sorter = new TableRowSorter<TableModel>(model);
        table.setRowSorter(sorter);
        JScrollPane scrollPane = new JScrollPane(table);
        JFrame frame = new JFrame("Filtering Table");
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.getContentPane().setLayout(new BorderLayout());
        frame.getContentPane().add(scrollPane, BorderLayout.CENTER);
        frame.getContentPane().add(getButtonPnl(sorter, filter), BorderLayout.SOUTH);
        frame.setSize(300, 200);
        frame.setVisible(true);
    }

    private static JPanel getButtonPnl(final TableRowSorter sorter, final RowFilter filter) {
        final String applyFilterTxt = "Apply filter";
        final String removeFilterTxt = "Remove filter";
        final JButton filterBtn = new JButton(applyFilterTxt);
        filterBtn.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                if (filterBtn.getText().equals(applyFilterTxt)) {
                    sorter.setRowFilter(filter);
                    filterBtn.setText(removeFilterTxt);
                } else {
                    filterBtn.setText(applyFilterTxt);
                    sorter.setRowFilter(null);
                }
            }
        });
        JPanel btnPnl = new JPanel();
        btnPnl.add(filterBtn);
        return btnPnl;
    }
}

Open in new window

0
TolgarAuthor Commented:
This example is not for SWT.

Does anyone have any idea about how to do table filtering in SWT?
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Java

From novice to tech pro — start learning today.