Code Monkey home page Code Monkey logo

Comments (3)

eugener avatar eugener commented on May 26, 2024

TableRowFilterSupport creates it is own instance if TableRowSorter. Also calling convertRowIndexToModel on a row sorter is not correct - it is better be called on the table itself without any knowledge of internals (sorter in this case)

from oxbow.

chrisjones2004 avatar chrisjones2004 commented on May 26, 2024

I am not using a tablerowsorter, however I still see this bug.

  1. Click on the number 3 in the "Ints" column.
  2. Sort the column so that the number 3 is at the top
  3. Filter the column "Ints" to only display number 3

import java.awt.BorderLayout;

import javax.swing.*;
import javax.swing.event.ListSelectionEvent;
import javax.swing.event.ListSelectionListener;
import javax.swing.table.DefaultTableModel;
import javax.swing.table.TableColumn;

import com.ezware.oxbow.swingbits.table.filter.IFilterChangeListener;
import com.ezware.oxbow.swingbits.table.filter.ITableFilter;
import com.ezware.oxbow.swingbits.table.filter.TableRowFilterSupport;
import com.ezware.oxbow.swingbits.table.filter.JTableFilter;

public class TableFilter2 implements ListSelectionListener {
final JTable table;
JTableFilter tableFilter;
private int selectedRow = -1;
private int selectedColumn = -1;
private int index = 1;

public TableFilter2() {

    final String[] cols = { "Checkbox", "Ints", "Strings", "Bools", "test" };
    Object[][] data = { { false, 1, "A", false, "hide" },
            { false, 1, "B", false, "hide" },
            { false, 1, "C", false, "hide" },
            { false, 3, "C", false, "hide" } };

    final DefaultTableModel model = new DefaultTableModel(data, cols);
    table = new JTable(model) {
        @Override
        public boolean isCellEditable(int row, int col) {
            return false;
        }
    };
    table.getSelectionModel().setSelectionMode(
            ListSelectionModel.SINGLE_SELECTION);
    table.getSelectionModel().addListSelectionListener(this);
    table.getColumnModel().getSelectionModel()
            .addListSelectionListener(this);
    table.setRowSorter(null);
    table.setAutoCreateRowSorter(false);
    table.getTableHeader().setReorderingAllowed(false);

    tableFilter = new JTableFilter(table);
    tableFilter.addChangeListener(new IFilterChangeListener() {

        @Override
        public void filterChanged(ITableFilter<?> arg0) {
            System.out.println("Fitler Event");

        }

    });
    TableRowFilterSupport.forFilter(tableFilter).searchable(true).apply();

    TableColumn tc = table.getColumnModel().getColumn(0);
    tc.setCellEditor(table.getDefaultEditor(Boolean.class));
    tc.setCellRenderer(table.getDefaultRenderer(Boolean.class));

    JPanel panel = new JPanel(new BorderLayout());
    panel.add(new JScrollPane(table), BorderLayout.CENTER);

    JFrame frame = new JFrame();
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frame.add(panel);
    frame.pack();
    frame.setVisible(true);
}

public static void main(String[] args) {
    new TableFilter2();
}

public void valueChanged(ListSelectionEvent e) {
    if (e.getValueIsAdjusting()) {
        return;
    }

    if (table.getSelectedRow() == -1 || table.getSelectedColumn() == -1) {
        return;
    }

    if (selectedRow == table.convertRowIndexToModel(table.getSelectedRow())
            && selectedColumn == table.convertRowIndexToModel(table
                    .getSelectedColumn())) {
        return;
    }

    selectedRow = table.convertRowIndexToModel(table.getSelectedRow());
    selectedColumn = table
            .convertRowIndexToModel(table.getSelectedColumn());
    System.out.println("Row/Col " + selectedRow + " / " + selectedColumn);
    System.out.println(index++ + " " + e);

}

}

Error:

Exception in thread "AWT-EventQueue-0" java.lang.ArrayIndexOutOfBoundsException: 1
at javax.swing.DefaultRowSorter.convertRowIndexToModel(DefaultRowSorter.java:518)
at javax.swing.JTable.convertRowIndexToModel(JTable.java:2645)
at TableFilter2.valueChanged(TableFilter2.java:102)
at javax.swing.DefaultListSelectionModel.fireValueChanged(DefaultListSelectionModel.java:184)
at javax.swing.DefaultListSelectionModel.fireValueChanged(DefaultListSelectionModel.java:154)
at javax.swing.DefaultListSelectionModel.setValueIsAdjusting(DefaultListSelectionModel.java:685)
at javax.swing.JTable$SortManager.restoreSelection(JTable.java:4052)
at javax.swing.JTable$SortManager.processChange(JTable.java:4000)
at javax.swing.JTable.sortedTableChanged(JTable.java:4132)
at javax.swing.JTable.sorterChanged(JTable.java:3822)
at javax.swing.RowSorter.fireRowSorterChanged(RowSorter.java:340)
at javax.swing.RowSorter.fireRowSorterChanged(RowSorter.java:332)
at javax.swing.DefaultRowSorter.sort(DefaultRowSorter.java:612)
at javax.swing.DefaultRowSorter.setRowFilter(DefaultRowSorter.java:424)
at com.ezware.oxbow.swingbits.table.filter.JTableFilter.execute(JTableFilter.java:69)
at com.ezware.oxbow.swingbits.table.filter.AbstractTableFilter.apply(AbstractTableFilter.java:113)
at com.ezware.oxbow.swingbits.table.filter.TableFilterColumnPopup.applyColumnFilter(TableFilterColumnPopup.java:203)
at com.ezware.oxbow.swingbits.table.filter.TableFilterColumnPopup.access$6(TableFilterColumnPopup.java:199)
at com.ezware.oxbow.swingbits.table.filter.TableFilterColumnPopup$5.perform(TableFilterColumnPopup.java:180)
at com.ezware.oxbow.swingbits.popup.PopupWindow$CommandAction.actionPerformed(PopupWindow.java:161)

from oxbow.

chrisjones2004 avatar chrisjones2004 commented on May 26, 2024

Please ignore this bug post. The bug is actually due to me trying to convert a table column index to a model rowIndex.

Thanks

Chris

from oxbow.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.