隐藏/显示jtable中的行

时间:2015-05-25 19:36:15

标签: java swing jtable rowfilter

我使用defaulttablemodel创建了表,其上面有组合框。我需要根据组合框值(10,20,30,40)隐藏/显示已经创建和填充的行。

private void newFilter(int numberOfRows) {
    RowFilter<TableModel, Integer> filter = new RowFilter<TableModel, Integer>() {
        @Override
        public boolean include(RowFilter.Entry<? extends TableModel, ? extends Integer> entry) {
            int i = table.convertRowIndexToView(entry.getIdentifier());

            return i < numberOfRows;

        }

    };

    ((TableRowSorter) table.getRowSorter()).setRowFilter(filter);
}


combobox.addItemListener(new ItemListener() {

        @Override
        public void itemStateChanged(ItemEvent e) {

            String s = (String) combobox.getSelectedItem();

            if (s.equals("10")) {
                newFilter(10);
            } else if (s.equals("15")) {
                newFilter(15);
            } else if (s.equals("20")) {
                newFilter(20);
            } else if (s.equals("30")) {
                newFilter(30);
            } else if (s.equals("40")) {
                newFilter(40);

            }

        }
    });

我试过这样,直到我再次选择相同的值,例如10,然后它什么也没做。我该怎么办?

1 个答案:

答案 0 :(得分:2)

int i = table.convertRowIndexToView(entry.getIdentifier());

当过滤行时,上面的语句返回-1。因此,一旦过滤了一行,它就会被过滤掉。

然后我尝试使用:

((TableRowSorter) table.getRowSorter()).setRowFilter(null);
((TableRowSorter) table.getRowSorter()).setRowFilter(filter);

尝试重置过滤器,但似乎没有工作,因为现在返回的值为&#34; i&#34;。

所以最后我尝试了:

private void newFilter(int numberOfRows)
{
    final RowFilter<TableModel, Integer> filter = new RowFilter<TableModel, Integer>()
    {
        @Override
        public boolean include(RowFilter.Entry<? extends TableModel, ? extends Integer> entry)
        {
            int i = table.convertRowIndexToView(entry.getIdentifier());

            //System.out.println(i + " : " + numberOfRows + " : " + entry.getIdentifier());

            return i < numberOfRows;
        }

    };

    ((TableRowSorter) table.getRowSorter()).setRowFilter(null);

    SwingUtilities.invokeLater(new Runnable()
    {
        public void run()
        {
            ((TableRowSorter) table.getRowSorter()).setRowFilter(filter);
        }
    });

}

它似乎有效。

希望有人有更好的解决方案。

以下是我用于测试的完整代码:

import java.awt.*;
import java.awt.event.*;
import java.awt.image.*;
import java.util.*;
//import java.util.List;
import javax.swing.*;
import javax.swing.event.*;
//import javax.swing.text.*;
//import java.beans.*;
import javax.swing.border.*;
//import javax.swing.plaf.*;
//import javax.swing.text.*;
import javax.swing.table.*;
//import java.io.*;
//import javax.imageio.*;
//import java.awt.geom.*;

public class SSCCE extends JPanel
{
    private JTable table;

    public SSCCE()
    {
        setLayout( new BorderLayout() );

        JComboBox<Integer> comboBox = new JComboBox<Integer>();
        comboBox.addItem( new Integer(10) );
        comboBox.addItem( new Integer(20) );
        comboBox.addItem( new Integer(30) );
        comboBox.addItem( new Integer(40) );

        comboBox.addItemListener( new ItemListener()
        {
            @Override
            public void itemStateChanged(ItemEvent e)
            {
                Integer value = (Integer)comboBox.getSelectedItem();
                newFilter( value );
            }
        });
        add(comboBox, BorderLayout.NORTH);

        table = new JTable(40, 3);
        table.setAutoCreateRowSorter(true);
        JScrollPane scrollPane = new JScrollPane(table);
        add(scrollPane, BorderLayout.CENTER);
    }

    private void newFilter(int numberOfRows)
    {
        final RowFilter<TableModel, Integer> filter = new RowFilter<TableModel, Integer>()
        {
            @Override
            public boolean include(RowFilter.Entry<? extends TableModel, ? extends Integer> entry)
            {
                int i = table.convertRowIndexToView(entry.getIdentifier());

                //System.out.println(i + " : " + numberOfRows + " : " + entry.getIdentifier());

                return i < numberOfRows;
            }

        };

        ((TableRowSorter) table.getRowSorter()).setRowFilter(null);

        SwingUtilities.invokeLater(new Runnable()
        {
            public void run()
            {
                ((TableRowSorter) table.getRowSorter()).setRowFilter(filter);
            }
        });

    }

    private static void createAndShowGUI()
    {
        JPanel panel = new JPanel();

        JFrame frame = new JFrame("SSCCE");
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.add(new SSCCE());
        frame.setLocationByPlatform( true );
        frame.pack();
        frame.setVisible( true );
    }

    public static void main(String[] args)
    {
        EventQueue.invokeLater(new Runnable()
        {
            public void run()
            {
                createAndShowGUI();
            }
        });
    }
}