2012-06-14 52 views
2

是否可以防止在JTable上一起排序?基本上我不想在用戶點擊表格標題時發生任何事情,並且內容要處於靜態順序。JTable防止所有排序

回答

4

the Javadoc

公共無效setRowSorter(RowSorter的分揀機)

參數:sorter - 在RowSorter; null輪流排序關

2

基本上,我什麼都不想,當用戶點擊表頭,併爲內容是在一個靜態的順序發生。

基本上JTable沒有任何分揀機,你必須刪除代碼行

- JTable#setAutoCreateRowSorter(true); 

- table.setRowSorter(sorter); 

- custom Comparator added as MouseEvent to the JTableHeader 

有看和讀JTable tutorial about Sorting and Filtering

0

禁用排序最好的和簡單的方式,當任何表用戶點擊標題列:

  1. 首先你需要在表頭上創建一個鼠標點擊監聽器
  2. 它裏面只作鼠標點擊左側avaible(與SwingUtilities類)
  3. 插入此行代碼

    yourTableVariable.setRowSorter(NULL);

實例:

yourTableVariable.getTableHeader().addMouseListener(new MouseAdapter() //here you make the click avaible ONLY on Table Header 
    { 
     @Override 
     public void mouseClicked(MouseEvent arg0) 
     { 
      if (SwingUtilities.isLeftMouseButton(arg0)) //here you select the mouse left click action 
      { 
       yourTableVariable.setRowSorter(null); //here is disableing the sorting     
      } 
     } 
    });