如何在JTable中显示/隐藏不同的行节 [英] How to show/hide different row sections in a JTable
问题描述
我有一个JTable,其中的行列出了目录中的文件和文件夹.文件夹行有一个按钮,可隐藏和显示列出文件夹中文件的行.当应用程序启动时,文件夹行之后的文件行将被隐藏.在子类方法中创建表之后,我最初使用RowFilter来隐藏这些行:
I have a JTable with rows that list files and folders in a directory. The folder rows have a button that hide and show rows listing the files in the folder. When the application starts up, the files rows that come after their folder row are hidden. I used RowFilter to initially hide these rows after the table was created in a subclass method:
...
public void createTable(){
//Create and populate table
...
//Add the filter to initially hide the subfile rows
TableRowSorter<DefaultTableModel> sorter = new TableRow Sorter<DefaultTableModel>(tableModel);
RowFilter hidefilter = getRowFilter();
sorter.setRowFilter(hidefilter);
myTable.setRowSorter(sorter);
}
...
private RowFilter getRowFilter() {
RowFilter<DefaultTableModel, Integer> filter = new RowFilter<DefaultTableModel, Integer>() {
@Override
public boolean include(RowFilter.Entry<? extends DefaultTableModel, ? extends Integer> entry) {
int modelRow = entry.getIdentifier();
if(/* current row column contains a certain flag */){
//Hide the row that represents a file in the folder
return false;
}
else return true;
}
};
return filter;
}
此代码成功隐藏了行,但是我想知道如何仅显示和隐藏某些隐藏行.可以使用类似我已经做过的过滤器类来完成此操作吗?有没有一种方法可以通过按钮和基于列中找到的值的过滤器来调用过滤器功能?
This code successfully hides the rows, but I'm wondering how to show and re-hide only some of the hidden rows. Can this be done with a filter class like the one I've already done? Is there a way for a filter function to be called by a button and filter based on a value found in a column?
推荐答案
基本思想是我们需要一个Filter
类,该类根据其字段的值过滤条目(我只创建了一个字段来保留示例简单),并且过滤器还具有更改这些字段的值的方法.
The basic idea is that we need to have a Filter
class that filters entries depending on the value of its fields (I only created one field to keep the example simple), and the filter also has methods that change the value of those fields.
然后我们创建一个按钮,按下该按钮时将调用此方法,然后调用table.getRowSorter().allRowsChanged()
,该按钮将通知表该数据已更改,因此必须重绘自身.
Then we create a button that calls this method when pressed, followed by a call to table.getRowSorter().allRowsChanged()
, which signals to the table that the data has changed so it has to redraw itself.
这是一个可行的例子.
Here's a working example.
public class Test {
public static class Filter extends RowFilter<TableModel, Integer> {
private String includePrefix = "Foo";
@Override
public boolean include(
javax.swing.RowFilter.Entry<? extends TableModel, ? extends Integer> entry) {
return entry.getStringValue(0).startsWith( includePrefix );
}
// Calling this method changes the filter to allow a different prefix
public void swapPrefix() {
this.includePrefix = this.includePrefix.equals("Foo") ? "Bar" : "Foo";
}
}
public static void main(String[] args) {
//setup
JFrame frame = new JFrame();
frame.setLayout( new BoxLayout(frame.getContentPane(), BoxLayout.PAGE_AXIS));
JTable table = new JTable( new Object[][]{ new Object[]{ "Foo 1" },
new Object[]{ "Bar 1" },
new Object[]{ "Foo 2" },
new Object[]{ "Foo 3" },
new Object[]{ "Bar 2" }},
new Object[] { "Foo"});
//create and configure sorter
Filter filter = new Filter();
TableRowSorter<TableModel> sorter = new TableRowSorter<TableModel>(table.getModel());
sorter.setRowFilter(filter);
table.setRowSorter(sorter);
JButton changeFilter = new JButton( "Change filter");
// pressing the button changes the filter first, then tells the table sorter to update the display
changeFilter.addActionListener( e -> { filter.swapPrefix(); table.getRowSorter().allRowsChanged(); });
//display window
frame.add( table );
frame.add(changeFilter);
frame.pack();
frame.setVisible( true );
}
}
这篇关于如何在JTable中显示/隐藏不同的行节的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!