Фильтрация с помощью AbstractTableModel

Я пытаюсь добавить фильтр в мою модель абстрактной таблицы. Я просматривал форумы, веб, но все еще не могу понять, что не так с моим кодом. Будем рады любым предложениям или помощи. Я знаю, что это похоже на то, что я спам повсюду с моим кодом, но я просто честно понятия не имею, где я делаю ошибку. Большое спасибо всем.

Мой класс графического интерфейса выглядит следующим образом:

public class GUIMain extends JFrame {
    private Registr registr = new Registr();
    private JTable tblSmlouvy;
    private SmlouvyModel model;
    private JTextField tfHledat = new JTextField(20);
    private TableRowSorter<SmlouvyModel> sorter = new TableRowSorter<GUIMain.SmlouvyModel>(model);

    //abstract actions
    private AbstractAction actpridat;
    private AbstractAction actupravit;
    private AbstractAction actulozCsv;
    private AbstractAction acttridit;
    private AbstractAction acthledat;

    public GUIMain() {
        initgui();
    }

public void initgui() {
        setTitle("Registr smluv");
        setDefaultCloseOperation(EXIT_ON_CLOSE);
        smlouvaDialog = new SmlouvaDialog(this);

        vytvoritAkce();

        model = new SmlouvyModel();
        // I tried putting tblSmlouvy = netblSmlouvy.setRowSorter(sorter) instead of the line below, but the table won't display at all 
        tblSmlouvy.setAutoCreateRowSorter(true);

        //toolbar
        JToolBar toolbar = new JToolBar();
        add(toolbar, BorderLayout.NORTH);
        toolbar.add(acthledat);
        toolbar.add(tfHledat);      

        pack();
        setLocationRelativeTo(null);
        add(new JScrollPane(tblSmlouvy), BorderLayout.CENTER);

    }



public void vytvoritAkce() {
acthledat = new AbstractAction("Hledat") {

            @Override
            public void actionPerformed(ActionEvent e) {
                String text = tfHledat.getText();
                if(text.length() == 0) {
                    sorter.setRowFilter(null);
                }
                else {
                    sorter.setRowFilter(RowFilter.regexFilter(text));
                }   
            }
        };      
    }

Моя настольная модель выглядит следующим образом:

class SmlouvyModel extends AbstractTableModel {


        private static final long serialVersionUID = 1L;

        @Override
        public int getRowCount() {
            return registr.getPocetSmluv();
        }

        @Override
        public int getColumnCount() {
            return 5;
        }

        @Override
        public Object getValueAt(int rowIndex, int columnIndex) {
            Smlouva s = registr.getSmlouva(rowIndex);
            switch(columnIndex) {
            case 0:
                return s.getCislo();
            case 1:
                return s.getProtistrana();
            case 2:
                return s.getDatumUzavreni();
            case 3:
                return s.getHodnota();
            case 4:
                return s.getGarant();               
            }
            return " ";
        }

        @Override
        public String getColumnName(int column) {

            switch(column) {
            case 0:
                return "Contract ID";
            case 1:
                return "Protistrana";
            case 2:
                return "Datum uzavření";
            case 3:
                return "Hodnota";
            case 4:
                return "Garant";
            }
            return "N/A";
        }
    }

И основной метод выглядит так:

 public static void main(String[] args) {
        SwingUtilities.invokeLater(new Runnable() {

            @Override
            public void run() {
                new GUIMain().setVisible(true);

            }
        }); 

    }

0 ответов

Другие вопросы по тегам