JTable阻止所有排序

时间:2012-06-14 16:41:21

标签: java swing jtable

是否可以防止在JTable上一起排序?基本上,当用户点击表格标题时,我不希望发生任何事情,并且内容是静态的。

3 个答案:

答案 0 :(得分:4)

请参阅the Javadoc

  

public void setRowSorter(RowSorter sorter)

     

参数:sorter - RowSorter; null关闭排序

答案 1 :(得分:2)

  

基本上,当用户点击表格标题时,我不希望发生任何事情,并且内容是静态的。

基本上JTable没有任何分拣机,你必须删除代码行

- JTable#setAutoCreateRowSorter(true);

- table.setRowSorter(sorter);

- custom Comparator added as MouseEvent to the JTableHeader

查看并阅读JTable tutorial about Sorting and Filtering

答案 2 :(得分:0)

当用户点击任何表标题列时禁用排序的最佳和简单方法:

  1. 首先需要在表格标题
  2. 上创建鼠标单击侦听器
  3. 在其内部只留下鼠标左键(使用SwingUtilities)
  4. 插入此行代码

    yourTableVariable.setRowSorter(空);

  5. 实际例子:

    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                  
                }
            }
        });