[英]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: 我有一个包含一些数据的JTable
,列如下:
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. 在这里,您可以找到有关大于行过滤器的信息https://docs.oracle.com/javase/7/docs/api/javax/swing/RowFilter.html此示例向您展示如何使用整数。
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: 然后查看RowFilter
API:
RowFilter.numberFilter(...)
method to create two filters, one for "greater than" and one for "less than". 您可以使用RowFilter.numberFilter(...)
方法创建两个过滤器,一个用于“大于”,另一个用于“小于”。 RowFilter.and(...)
method to create the filter that you can use on your table. 然后,您可以使用上面的两个过滤器,并使用RowFilter.and(...)
方法创建可以在表上使用的过滤器。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.