0

I have a JTable with some data and the columns are as follows:

  • Market - String
  • Currency - String
  • Volume - Double
  • High - Double
  • Low - Double

I have a text field for each and when you click a button it should filter the table. I have it working with the Market and Currency Strings using:

RowFilter.regexFilter(txtMarket.getText(), 0);
RowFilter.regexFilter(txtCurrency.getText(), 1);

I want to use something similar to filter a greater than or less than for volume, high and low. Is this possible or should I try something else?

Sorry this is my first time asking a question I apologize for any missing information or formatting issues.

Andrew Thompson
  • 168,117
  • 40
  • 217
  • 433
  • 1
    [for example](http://stackoverflow.com/questions/17854854/jtable-rowfilter-and-rowfilter-entry), rest is in APIs – mKorbel Jun 01 '14 at 10:15
  • I looked at this but I find it very confusing and hard to read. I am fairly new to java and have never used jTables prior to this. I am specifically interested in finding out how to filter doubles in my JTable using RowFilter. Perhaps someone is able to break down how rowfilter deals with doubles? – user3458837 Jun 01 '14 at 10:40
  • 1
    @user3458837 "how rowfilter deals with doubles" they are unrelated. Fundamentally, you don't have to worry about if you are using doubles or a String or anything else. You just need to learn about how RowFilters work, how the `include` method works, what information you get in the `Entry` argument. And then you just do whatever you want to do. Whether it's double or string or integers, it makes no difference you just need to have a good picture of how the system work as a whole and make a good design. The details don't alter that. – DSquare Jun 01 '14 at 12:00

2 Answers2

1

Here you can find info on greater than rowfilters https://docs.oracle.com/javase/7/docs/api/javax/swing/RowFilter.html This example shows you how you do it with integers.

Example:

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;   
import javax.swing.JButton;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.JTextField;
import javax.swing.RowFilter;
import javax.swing.table.DefaultTableModel;
import javax.swing.table.TableRowSorter;

public class example extends JPanel {
    private String[] columnNames = { "Volume"
    };

    private Object[][] data = { {new Integer(5)},
            {new Integer(1)},
            {new Integer(2)},
            {new Integer(3)},
            {new Integer(5)},
            {new Integer(5)},
            {new Integer(7)} };

    private DefaultTableModel myModel = new DefaultTableModel(data, columnNames);
    private JTable table = new JTable(myModel);
    private JScrollPane scrollPane = new JScrollPane(table);
    private TableRowSorter<DefaultTableModel> sorter = new TableRowSorter<DefaultTableModel>(myModel);
    private JButton button;
    private JButton button2;
    private JTextField filterText;



    RowFilter<DefaultTableModel, Integer> GreaterThan = new RowFilter<DefaultTableModel, Integer>() {
        public boolean include(Entry<? extends DefaultTableModel, ? extends Integer> entry) {
            myModel = entry.getModel();

            //0 is the first column
            if ((int) table.getValueAt(entry.getIdentifier(), 0) > Integer.parseInt(filterText.getText())) {
                return true;
            }
            return false;
        }
    };

    RowFilter<DefaultTableModel, Integer> LessThan = new RowFilter<DefaultTableModel, Integer>() {
        public boolean include(Entry<? extends DefaultTableModel, ? extends Integer> entry) {
            myModel = entry.getModel();

            //0 is the first column
            if ((int) table.getValueAt(entry.getIdentifier(), 0) < Integer.parseInt(filterText.getText())) {
                return true;
            }
            return false;
        }
    };


    public example() {
        filterText = new JTextField();
        button = new JButton("Click to sort Greater Than");
        button2 = new JButton("Click to sort Less Than");
        add(button);
        add(button2);
        add(scrollPane);
        table.setRowSorter(sorter);

        button.addActionListener(new ActionListener() {

            @Override
            public void actionPerformed(ActionEvent e) {
                sorter.setRowFilter(GreaterThan);
            }
        });

        button2.addActionListener(new ActionListener() {

            @Override
            public void actionPerformed(ActionEvent e) {
                sorter.setRowFilter(LessThan);
            }
        });


    }
}
Felix Torssell
  • 45
  • 1
  • 12
0

I have it working with the Market and Currency Strings using:

Then check out the RowFilter API:

  1. You can use the RowFilter.numberFilter(...) method to create two filters, one for "greater than" and one for "less than".
  2. Then you can use the two filters from above and use the RowFilter.and(...) method to create the filter that you can use on your table.
camickr
  • 321,443
  • 19
  • 166
  • 288