2016-01-15 57 views
2

我有一個RowSorterListener。我希望能夠告訴哪一列火災RowSorterEvent。但是,當我試圖獲得專欄時,我沒有得到我想要的輸出。獲取啓動的列RowSorterEvent

public class CustomRowSorterListener implements RowSorterListener { 
    JTable table; 
    public CustomRowSorterListener(JTable table) { 
     this.table = table; 
    } 

    @Override 
    public void sorterChanged(RowSorterEvent e) 
    { 
     //Attempt 1 
     System.out.println(e.getSource()); //Returns RowSorter and not a column 

     //Attempt 2 
     System.out.println(e.getSource().getColumn()); //Caused error in code 

     //Attempt 3 
     System.out.println(table.getColumn()) //Didn't work because no arguments were 
               //provided. However, I was unsure if it could 
               //be done this way. 

     //System.out.println(table.getColumn(e.getSource())) also doesn't work 
    } 
} 

我將不勝感激任何幫助獲得解決方案的工作。

+1

TableColumnModel中可以重新排序,不得不指數從視圖轉換從鼠標和的KeyEvents模型,RowSorterEvent直接返回從XxxTableModel – mKorbel

回答

1

我想告訴哪個列觸發RowSorterEvent。

SortKey開始。它可以返回getColumn。例如:

Column NO。 - 0是排序​​
列號。 - 0是排序​​
列號。 - 0是排序​​
列號。 - 1已分類
列號。 - 1排序
BUILD SUCCESSFUL(總時間:9秒),

import java.awt.EventQueue; 
import java.util.List; 

import javax.swing.*; 
import javax.swing.RowSorter.SortKey; 
import javax.swing.event.*; 
import javax.swing.table.*; 

public class SortTest { 

    private JFrame frame = new JFrame(getClass().getSimpleName()); 

    private DefaultTableModel model = new DefaultTableModel(10, 2) { 
     private static final long serialVersionUID = 1L; 

     @Override 
     public Class<?> getColumnClass(int column) { 
      return column == 1 ? Integer.class : Object.class; 
     } 
    }; 
    private JTable table = new JTable(model); 

    public SortTest() { 
     for (int row = model.getRowCount(); --row >= 0;) { 
      int i = 20 + row % 20; 
      model.setValueAt(row + " " + i, row, 0); 
      model.setValueAt(i + row, row, 1); 
     } 
     table.setAutoCreateRowSorter(true); 
     TableRowSorter<?> sorter = (TableRowSorter<?>) table.getRowSorter(); 
     sorter.setSortsOnUpdates(true); 
     sorter.addRowSorterListener(new RowSorterListener() { 

      @Override 
      public void sorterChanged(RowSorterEvent rse) { 
       if (rse.getType() == RowSorterEvent.Type.SORT_ORDER_CHANGED) { 
        List<SortKey> keys = rse.getSource().getSortKeys(); 
        for (SortKey key : keys) { 
         System.out.println("Column NO. - " + key.getColumn() + " is sorted"); 
         if (key.getColumn() == 0) { 
          break; 
         } else { 
          break; 
         } 
        } 
       } 
      } 
     }); 
     frame.add(new JScrollPane(table)); 
     table.setPreferredScrollableViewportSize(table.getPreferredSize()); 
     frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
     frame.pack(); 
     frame.setLocationRelativeTo(null); 
     frame.setVisible(true); 
    } 

    public static void main(String[] args) { 
     EventQueue.invokeLater(() -> { 
      new SortTest(); 
     }); 
    } 
} 
+0

指數謝謝非常感謝您的幫助。對不起,從另一個帖子中打電話給你 – Dan