2012-06-14 78 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     
      } 
     } 
    });