Java swing切换按钮来过滤jtable行 [英] Java swing toggle button to filter jtable rows

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

问题描述

我有一个 JTable ,它包含一个自定义的 AbstractTableModel ,当 getValueAt 被调用。当然,我还有一个自定义的 TableCellRenderer ,它可以从对象中获取/构造文本,以便显示它。



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

我有两个问题,由于这一点。




$ b $第一个是我完全不知道如何编写过滤器,而不是按对象进行过滤。 b $ b

其次是我不知道如何将上述过滤器附加到切换按钮,以便能够打开和关闭。



对不起如果这是一个迟缓的问题,但 http://docs.oracle。 com / javase / tutorial / uiswing / components / table.html#sorting 是我看到的最无用的文档,因为解释并不深入。



感谢任何人的帮助。

编辑:
对象包含多个字段,但我特别感兴趣的两个过滤器切换。当我说 isSuper ,返回一个布尔值值,第二个返回一个 string 当我调用 getName 时。如果第一个开关打开,它应该显示所有在 isSuper 上返回true的条目,而第二个切换应该显示名称受到两个单词(空间存在)当我调用 getName

解决方案

href =http://docs.oracle.com/javase/7/docs/api/javax/swing/RowFilter.html =nofollow> JavaDocs 很好地说明了这一点。



有了这么少的信息,最好的你可以期待的是一个概述...

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

//创建行填充...
trs.setRowFilter(new RowFilter< TableModel,Integer>(){

@Override
public boolean include(RowFilter.Entry<?extends TableModel,?extends Integer> entry){
boolean include = false;
//返回特定列的值...
CustomObject value = (CustomObject)entry.getValue(filterColumn);
if(filterBySuper){$ b $ include = value.isSuper();
} else {$ b $ include = value.getName()。 startsWith(fistPart)&& value.getName()。endWith(lastPart);
}
return include;
}
});

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

  trs.sort(); 


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.

I have two problems due to that.

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.

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.

Thanks to anyone for their help.

EDIT: 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.

解决方案

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

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

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