11

TableRowSorter をテーブルとそれに対応するモデルに追加した後、特に firetabletablerowsinserted に対応する追加を行うと、例外が発生します。GetRowCount() がモデルの範囲を超える値を返していることはテストから明らかです。ただし、ソーターまたはフィルターが追加された後、テーブルに値を追加し続ける方法は意味がありませんか?

例として、テーブルに何かを追加する前に行フィルターを設定し、モデルで次の呼び出しを使用してテーブルに値を追加します。

this.addRow(row, createRow(trans,row));
this.fireTableRowsInserted(this.getRowCount(), this.getRowCount());

行数のサイズは 1 で、例外がスローされます。

java.lang.IndexOutOfBoundsException: Invalid range
at javax.swing.DefaultRowSorter.checkAgainstModel(Unknown Source)
at javax.swing.DefaultRowSorter.rowsInserted(Unknown Source)
at com.gui.model

最初にソーターを追加せずに同じ手順を実行すると、すべて問題ありません。おそらく、ソーターが変更を加えた可能性があることをモデルに通知する必要があると想定し、次のことを試みましたが、それでも例外が返されました。

this.addRow(row, createRow(trans,row));
this.fireTableStructureChanged()
this.fireTableRowsInserted(this.getRowCount(), this.getRowCount());

以下のように fire を呼び出す前に、値がモデルに追加されたことをモデル内のソーターに通知しようとしましたが、同様に失敗します:

 this.addRow(row, createRow(trans,row));
 if(sorter.getRowFilter() != null){
      //if a sorter exists we are in add notify sorter
      sorter.rowsInserted(getRowCount(), getRowCount());
  }
  this.fireTableRowsInserted(this.getRowCount(), this.getRowCount());

最後に、FireTableRowsInsterted(0,0) をハードコーディングしましたが、例外はスローされません。しかし、テーブルには何も追加されませんか? したがって、それは間違いなく OutOfBounds の問題の一種であることはわかっています。私はすべてを見てきましたが、答えを見つけることができないようです。これがどのように機能すると思われるかを誰かが知っているなら、それは非常に役に立ちます。JPanel内でソーターを設定するコードは次のとおりです。

    messageTable.setRowSorter(null);
     HttpTransactionTableModel m = getTransactionTableModel();
     final int statusIndex = m.getColIndex("status");
     RowFilter<Object,Object> startsWithAFilter = new RowFilter<Object,Object>() {
           public boolean include(Entry<? extends Object, ? extends Object> entry) {

               for(char responseCode:responseCodes)
               {
                   if (entry.getStringValue(statusIndex).startsWith(Character.toString(responseCode))) {
                         return true;
                       }
               }


             // None of the columns start with "a"; return false so that this
             // entry is not shown
             return false;
           }
         };


        m.sorter.setRowFilter(startsWithAFilter);
        messageTable.setRowSorter(m.sorter);

モデルに価値を追加するモデル内のコードは次のとおりです。

public void update(Observable o, Object evt) {
    if (evt instanceof ObservableEvent<?>) {

        ObservableEvent<?> event = (ObservableEvent<?>) evt;

        if (event.getElement() instanceof HttpTransaction) {

            HttpTransaction trans = (HttpTransaction) event.getElement();

            // handle adding of an element
            if (event.getAction() == PUT) {

                if (includeTransaction(trans)) {

                    // handle request elements
                    if (trans.getRequest() != null && idMap.get(trans.getID()) == null) {

                        idMap.put(trans.getID(), count++);
                       // transactionManager.save(trans);
                        int row = idMap.get(trans.getID());
                        this.addRow(row, createRow(trans,row));
                        if(sorter.getRowFilter() != null){
                            sorter.rowsInserted(getRowCount(), getRowCount());
                        }
                        this.fireTableRowsInserted(this.getRowCount(), this.getRowCount());

                    }
4

3 に答える 3

3

アウト バイ 1 エラーが発生しました。イベントを発生させるための正しいコードは次のとおりです。

this.fireTableRowsInserted(this.getRowCount()-1, this.getRowCount()-1);
于 2011-06-03T04:07:17.127 に答える
2

私は戻って、クレオパトラのコメントを見た後、これをよく見ました. RowSorter を作成した後、RowSorter を JTable にアタッチする前に TableModel を変更していました。これは、私が抱えていた問題を示す例です。

import javax.swing.*;
import javax.swing.table.AbstractTableModel;
import javax.swing.table.TableRowSorter;
import java.util.ArrayList;
import java.util.List;

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

    public TestTableMain() {
        SwingUtilities.invokeLater(new Runnable() {
            @Override
            public void run() {
                buildAndShowMainFrame();
            }
        });
    }

    private void buildAndShowMainFrame() {
        JFrame frame = new JFrame();
        JScrollPane scrollPane = new JScrollPane();

        TestTableModel model = new TestTableModel();
        JTable table = new JTable(model);

        TableRowSorter<TestTableModel> rowSorter = new TableRowSorter<>(model);
        rowSorter.setRowFilter(null);

        model.add("First added item.");
        /* The RowSorter doesn't observe the TableModel directly. Instead,
         * the JTable observes the TableModel and notifies the RowSorter
         * about changes. At this point, the RowSorter(s) internal variable
         * modelRowCount is incorrect.  There are two easy ways to fix this:
         *
         * 1. Don't add data to the model until the RowSorter has been
         * attached to the JTable.
         *
         * 2. Notify the RowSorter about model changes just prior to
         * attaching it to the JTable.
         */

        // Uncomment the next line to notify rowSorter that you've changed
        // the model it's using prior to attaching it to the table.
        //rowSorter.modelStructureChanged();
        table.setRowSorter(rowSorter);

        scrollPane.setViewportView(table);
        frame.setContentPane(scrollPane);

        frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
        frame.pack();
        frame.setVisible(true);

        model.add("Second added item.");
    }

    private class TestTableModel extends AbstractTableModel {
        private List<String> items = new ArrayList<>();

        public TestTableModel() {
            for(int i=0;i<5;i++) {
                add("Item " + i);
            }
        }

        @Override
        public int getRowCount() {
            return items.size();
        }

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

        @Override
        public Object getValueAt(int rowIndex, int columnIndex) {
            return items.get(rowIndex);
        }

        public void add(String item) {
            items.add(item);
            fireTableRowsInserted(items.size() - 1, items.size() - 1);
        }
    }
}
于 2012-08-16T17:45:18.910 に答える
0

したがって、今のところ、現在ソートモードにある場合はモデルをチェックインし、その場合はソートモデルの更新のみを呼び出すように見えます。それ以外の場合は、通常のモデル ファイア アップデートを呼び出すと、これまでのところすべてが機能しているようです。ただし、これを処理するより良い方法についてはまだオープンです。

                         if(sorter.getRowFilter() != null){
                             sorter.modelStructureChanged();
                           }
                           else
                         this.fireTableRowsInserted(this.getRowCount(), this.getRowCount());
于 2011-05-29T18:05:44.010 に答える