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);
}
}