用于过滤 jtable 行的 Java 摆动切换按钮 [英] Java swing toggle button to filter jtable rows

查看:20
本文介绍了用于过滤 jtable 行的 Java 摆动切换按钮的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个 JTable,它包含一个自定义的 AbstractTableModel,它在调用 getValueAt 时返回一个对象.当然,我有一个自定义的 TableCellRenderer,它能够从对象中获取/构造文本以便显示.

I have a JTable, it contains a custom AbstractTableModel that return an object when getValueAt is called. And of course I have a custom TableCellRenderer that is capable of getting/constructing the text from the object so that it can be displayed.

但是现在我想写一个过滤器.过滤器将是一个简单的切换按钮.当它打开时,我想应用过滤器,当它关闭时,我想删除过滤器.

However now I would like to write a filter. Filter will be a simple toggle button. When it is turned on I would like the filter to be applied and when it is turned off I would like filter to be removed.

因此我有两个问题.

第一个是,当您必须按对象而不是基元进行过滤时,我完全不知道如何编写过滤器.

First one is that I have absolutely no idea how to write a filter when you have to filter by object rather than a primitive.

其次是我不知道如何将所述过滤器连接到切换按钮以能够打开和关闭它.

Second is I have no idea how to attached the said filter to the toggle button to be able to turn it on and off.

如果这是一个智障问题,我很抱歉,但是 http://docs.oracle.com/javase/tutorial/uiswing/components/table.html#sorting 是我看到的最无用的文档,因为解释不深入.

I am sorry if this is a retarded question but http://docs.oracle.com/javase/tutorial/uiswing/components/table.html#sorting was the most useless documentation I saw since explanation was not in depth.

感谢任何人的帮助.

该对象包含多个字段,但我对两个过滤器切换特别感兴趣.当我说isSuper 时,一个返回一个boolean 值,当我调用getName 时,第二个返回一个string.如果第一个开关打开,它应该显示在 isSuper 上返回 true 的所有条目,第二个开关应该显示当我调用 时名称由两个单词(存在空格)组成的所有条目getName.

The object contains multiple fields, but I am interested in two filter toggles specifically. One returns a boolean value when I say isSuper, and the second return a string when I call getName. If first toggle is turned on it should show all entries that return true on isSuper, and the second toggle should show all entries where name is compromised of two words (space is present) when I call getName.

推荐答案

老实说,JavaDocs 很好地说明了它...

To be honest, the JavaDocs spell it out quite well...

由于要介绍的信息如此之少,您所能希望的就是概述...

With such little information to go on, the best you can hope for is an overview...

TableRowSorter<TableModel> trs = new TableRowSorter<TableModel>();
table.setRowSorter(trs);

// Create the row filder...
trs.setRowFilter(new RowFilter<TableModel, Integer>() {

    @Override
    public boolean include(RowFilter.Entry<? extends TableModel, ? extends Integer> entry) {
        boolean include = false;
        // Returns the value for the specific column...
        CustomObject value = (CustomObject)entry.getValue(filterColumn);
        if (filterBySuper) {
            include = value.isSuper();
        } else {
            include = value.getName().startsWith(fistPart) && value.getName().endWith(lastPart);
        }
        return include;
    }
});

当你想更新过滤器时,你只需要调用...

When you want to update the filter, you simply need to call...

trs.sort();

这篇关于用于过滤 jtable 行的 Java 摆动切换按钮的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆