如何获得过滤后的模型?

发布于 2024-10-27 15:30:05 字数 411 浏览 1 评论 0原文

我正在使用 JTables 来显示用户可以过滤的信息,如果用户在过滤后保存,我想将过滤后的表保存到文本文件中以实现持久性(这意味着任何被过滤掉的内容都不会保存到文本文件中)。

对于过滤,我只是按照本教程的过滤部分进行操作: http:// /download.oracle.com/javase/tutorial/uiswing/components/table.html#sorting 它工作正常,但我不确定有什么方法可以获取当前显示的模型到包含尚未过滤掉的所有内容的基础模型。

有什么办法可以用我过滤的方式来做到这一点吗?

谢谢!

I'm working with JTables to display information that users can filter, and if the user saves after filtering I want to save the filtered table to a textfile for persistence (meaning anything that got filtered out will not be saved to the textfile).

For filtering I just followed the filtering part of this tutorial: http://download.oracle.com/javase/tutorial/uiswing/components/table.html#sorting and it works fine, but I'm not sure of any way that I can get a model of the current display as opposed to the underlying model that contains everything that hasn't been filtered out.

Is there any way to do this with this with the way that I'm filtering?

Thanks!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

风筝有风,海豚有海 2024-11-03 15:30:05

询问表的行数(使用getRowCount()),这将返回过滤的(可见)行数。从 0 迭代到 rowCount,使用 convertRowIndexToModel() 将每个行索引转换为模型索引,并向模型询问每个模型索引处的数据以构建已过滤(可见)数据的列表。

Ask the table its number of rows (using getRowCount()), which will return the number of filtered (visible) rows. Iterate from 0 to the rowCount, convert each row index to the model index using convertRowIndexToModel(), and ask your model the data at each model index to build the list of filtered (visible) data.

子栖 2024-11-03 15:30:05

此代码展示了如何执行此操作。请注意,按下按钮后,B 行不会打印到输入中。

import javax.swing.JFrame;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.RowFilter;
import javax.swing.table.DefaultTableModel;
import javax.swing.table.TableModel;
import javax.swing.table.TableRowSorter;
import javax.swing.JButton;
import java.awt.BorderLayout;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;

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

        TableRowSorter<TableModel> sorter = new TableRowSorter<TableModel>(
                model);
        sorter.setRowFilter(filter);
        table.setRowSorter(sorter);
        JScrollPane scrollPane = new JScrollPane(table);
        JFrame frame = new JFrame("Filtering Table");
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

        JButton btnNewButton = new JButton("Print values");
        btnNewButton.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {

                for(int row = 0;row < table.getRowCount();row++) {
                    System.out.println(table.getModel().getValueAt(table.convertRowIndexToModel(row), 0));
                }
            }
        });
        frame.getContentPane().add(btnNewButton, BorderLayout.SOUTH);
        frame.getContentPane().add(scrollPane);
        frame.setSize(300, 200);
        frame.setVisible(true);
    }
}

This code shows how to do this. Please note that B row is not printed to the input after the button is pressed.

import javax.swing.JFrame;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.RowFilter;
import javax.swing.table.DefaultTableModel;
import javax.swing.table.TableModel;
import javax.swing.table.TableRowSorter;
import javax.swing.JButton;
import java.awt.BorderLayout;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;

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

        TableRowSorter<TableModel> sorter = new TableRowSorter<TableModel>(
                model);
        sorter.setRowFilter(filter);
        table.setRowSorter(sorter);
        JScrollPane scrollPane = new JScrollPane(table);
        JFrame frame = new JFrame("Filtering Table");
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

        JButton btnNewButton = new JButton("Print values");
        btnNewButton.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {

                for(int row = 0;row < table.getRowCount();row++) {
                    System.out.println(table.getModel().getValueAt(table.convertRowIndexToModel(row), 0));
                }
            }
        });
        frame.getContentPane().add(btnNewButton, BorderLayout.SOUTH);
        frame.getContentPane().add(scrollPane);
        frame.setSize(300, 200);
        frame.setVisible(true);
    }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文