How can you disable all sorting code in JTable in 1.6

百般思念 提交于 2019-12-05 07:34:59

Have you tried JTable.setRowSorter(null) ?

edit : and setAutoCreateRowSorter ? (1. create table, 2. row sorter to null, 3. autocreate sorter to false, 4. set model).

I use this in my JTable subclass and it catches mouse events just fine:

class QueueTable extends JTable {
    public QueueTable() {
        ...
        getTableHeader().addMouseListener(new SortColumnListener(1));
    }
}

The SortColumnListener is implemented like so:

class SortColumnListener extends MouseAdapter {
    SortColumnListener(int column) { ... }

    public void mouseClicked(MouseEvent e) {
        TableColumnModel colModel = QueueTable.this.getColumnModel();
        int columnModelIndex = colModel.getColumnIndexAtX(e.getX());

        if(columnModelIndex == column) {
            // Do stuff
        }
    }
}

This catches mouse events in the SortColumnListener just fine and I can do whatever I want with those events. I have no RowSorter implementation set on the JTable and this works perfectly in Java 5 and Java 6.

For full sourcecode of this class, see: QueueTable.java

As I understand it you have two problems here:

  1. Because of the new sorting code in JTable, your sorting does not work.
  2. Even if you disable sorting by setRowSorter(null) or by overriding the setRowSorter(TableRowsorter) to do nothing, it does not work because the events on header are not passed to your JTable.

In that case I think the option for you is to just have your sorting code implemented as TableRowSorter. I am not aware how complex your sorting code is and whether it can map the TableRowSorter API, but this seems to be one more alternative you can try.

JTable.setAutoCreateRowSorter(false);

Unless the TableRowSorter is set somewhere, I don't think that you have to call setRowSorter(null)

A way to disable sorting when clicking on the header, is to remove all the table header's listeners:

        for(MouseListener listener : table.getTableHeader().getMouseListeners()){
        table.getTableHeader().removeMouseListener(listener);
    }

Then in case you want some other specific action (like column resizing) you could just add a specific listener for that particular action.

I tested all possibilities mentioned here on Sun table sort example and they all are working.

Unfortunately there are still many bugs in table sorting. There can not be much done until you post your code here. One possibility is to try out SwingX solution.

I solved your problem in your edit:

package jtableheadermouseevent;

import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import javax.swing.JFrame;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.table.DefaultTableModel;

/**
 *
 * @author martijn
 */
public class Main {

    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) {
        JFrame fr = new JFrame("JTable Header Mouse Listener");
        fr.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        final JTable table = new JTable();
        JScrollPane pane = new JScrollPane(table);

        String[][] data = {{"Foo", "Bar"}, {"Baz", "Coffee"}};
        String[] columns = {"Header 0", "Header 1"};

        DefaultTableModel model = new DefaultTableModel(data, columns);
        table.setModel(model);
        fr.add(pane);
        table.getTableHeader().addMouseListener(new MouseAdapter() {

            @Override
            public void mouseClicked(MouseEvent e) {
                super.mouseClicked(e);
                System.out.println("Header clicked : (X: " + e.getX() + ", Y: " + e.getY() + ") With button " + e.getButton() );
                int header = table.getTableHeader().columnAtPoint(e.getPoint());
                System.out.println("This means header " + header + " is clicked!");
            }

        });
        fr.pack();
        fr.setSize(800, 300);
        fr.setVisible(true);
    }

}

This works perfect in Linux, so I suppose also on OSX and Windows. I also tested it after resizing the columns: it still know which columns was pressed. But after reordering the columns, the column which was first "column 0" became "column 1".
But you can always disallow the user to move the columns with this:

table.getTableHeader().setReorderingAllowed(false);

Hope this helps

rafal.powroznik

Try this:

public abstract class BaseTable extends JTable {
    public BaseTable() {
        init();
        ..
    }

    protected boolean sortableDisable() {
        return false;
    }

    private void init() {
        TableRowSorter<BaseTableModel> sorter =
            new TableRowSorter<BaseTableModel>(tableModel);
        if (sortableDisable()) {
            setAutoCreateRowSorter(false);
            for (int c = 0; c < tableModel.getColumnCount(); c++) {
                sorter.setSortable(c, false);
            }
        }
        setRowSorter(sorter);
        ..
    }
}

public class TableX extends BaseTable() {

    @Override
    protected boolean sortableDisabled() {
        return true;
    }
    ..
}
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!