button to filter JTable for checkbox is selected

前端 未结 2 1903
名媛妹妹
名媛妹妹 2021-01-25 10:31

I have a JTable fairly similar to this one.

http://docs.oracle.com/javase/tutorial/uiswing/components/table.html#sorting

2条回答
  •  情歌与酒
    2021-01-25 10:44

    Your RowFilter is too simplistic, and instead you need to give it more heft, more meat. The RowFilter API actually has a small bit of very useful example code that could help you. For example, I used that code to help my create a very simplistic but workable RowFilter:

    import java.awt.BorderLayout;
    import java.awt.event.ItemEvent;
    import java.awt.event.ItemListener;
    
    import javax.swing.*;
    import javax.swing.table.DefaultTableModel;
    import javax.swing.table.TableRowSorter;
    
    public class Foo {
       public static void main(String[] args) {
          String[] petStrings = { "Bird", "Cat", "Dog", "Rabbit", "Pig" };
          JComboBox petCombo = new JComboBox<>(petStrings);
          String[] columnNames = {"Name", "Animal", "Male", "Delete"};
          Object[][] data = {
              {"Tweetie", petStrings[0], false, null},
              {"Sylvester", petStrings[1], true, null},
              {"Lassie", petStrings[2], false, null},
              {"Peter", petStrings[3], true, null},
              {"Arnold", petStrings[4], true, null}
          };
    
          final MyTableModel model = new MyTableModel(data, columnNames);
          final JTable table = new JTable(model);
          table.getColumnModel().getColumn(1).setCellEditor(new DefaultCellEditor(petCombo));
    
    
          final TableRowSorter sorter = new TableRowSorter(model);
          final RowFilter maleSexFilter = new RowFilter() {
             public boolean include(Entry entry) {
                MyTableModel personModel = entry.getModel();
                boolean maleSex = ((Boolean) personModel.getValueAt(entry.getIdentifier(), 2)).booleanValue();
               return maleSex;
              }
            };
    
          sorter.setRowFilter(null);
          table.setRowSorter(sorter);
    
          JToggleButton showMalesButton = new JToggleButton("Show Males");
          showMalesButton.addItemListener(new ItemListener() {
    
             @Override
             public void itemStateChanged(ItemEvent iEvt) {
                if (iEvt.getStateChange() == ItemEvent.SELECTED) {
                   System.out.println("selected!");
                   sorter.setRowFilter(maleSexFilter);
                } else {
                   sorter.setRowFilter(null);
                }
             }
          });
    
          JPanel bottomPanel = new JPanel();
          bottomPanel.add(showMalesButton);
    
          JPanel panel = new JPanel(new BorderLayout());
          panel.add(new JScrollPane(table), BorderLayout.CENTER);
          panel.add(bottomPanel, BorderLayout.PAGE_END);
    
         JOptionPane.showMessageDialog(null, panel);
       }
    }
    
    @SuppressWarnings("serial")
    class MyTableModel extends DefaultTableModel {
       public MyTableModel(Object[][] data, String[] columnNames) {
          super(data, columnNames);
       }
    
       @Override
       public Class getColumnClass(int columnIndex) {
          if (columnIndex == 2) {
             return Boolean.class;
          }
          return super.getColumnClass(columnIndex);
       }
    }
    

    The filter code is:

      final RowFilter maleSexFilter = new RowFilter() {
         public boolean include(Entry entry) {
            MyTableModel personModel = entry.getModel();
            boolean maleSex = ((Boolean) personModel.getValueAt(entry.getIdentifier(), 2)).booleanValue();
           return maleSex;
          }
        };
    

    where all I do is get the boolean value held by the 2nd column, row determined by entry.getIdentifier(), and return that value. If true, then it is selected.

提交回复
热议问题