简体   繁体   中英

Can I use RowFilter with a JTable to filter greater than or less than a column with type double?

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.

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


    }
}

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM