开发者

Sorting a table breaks the TableRowListener?

I have a sortable default table model with ListSelectionListener that listens for a doubleclick and then opens a details view of a particular column. This works fine, however, when I sort a column the listener no longer functions.

JTable foosTable = new JTable(model);
TableRowSorter<TableModel> fooSorter = new TableRowSorter<TableModel>(model);
foosTable.setRowSorter(fooSorter);
ListSelectionModel listMod = foosTable.getSelectionModel();
listMod.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
ListSelectionListener barListener = new Lis开发者_开发知识库tSelectionListener(foosTable);
listMod.addListSelectionListener(barListener);


I have never used TableRowListener which seems to only have an itemChanged event. I usually stick to standard swing. Add a mouse listener to the table, grab the location of a click event and then handle it.

 jTable.addMouseListener(new java.awt.event.MouseAdapter() {
     public void mouseClicked(java.awt.event.MouseEvent evt) {
         Point p = new Point(evt.getX(), evt.getY());
         int col = jTable.columnAtPoint(p);
         int row = jTable.rowAtPoint(p);
         if (evt.getButton() == MouseEvent.BUTTON1)
         {
            if (evt.getClickCount() >= 2)
            {
               ...
               ...
            }
        });

Edit Setup a TableRowSorter:

  TableRowSorter<YourJTableModel> sorter =
        new TableRowSorter<YourJTableModel>(yourJTableModel);
   jTable.setRowSorter(sorter);

Because you are changing the row order, you will need to use convertColumnIndexToModel to get the correct model data for the view.

For more complex sorting/filtering needs you may want to try glazed lists.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜