Reposition Jtable Column Header

563 Views Asked by At

I have to disable right click on JTableHeader so that user cannot right click over column header and drag to reposition the columns. Do we have any trick to do that? Just to mention left mouse button click works good but when user does RMB and drags the column, the column is moved and is repainted over the other columns when mouse is released.

Any help is appreciatted.

2

There are 2 best solutions below

0
On

Triggering column drag/resizing with the right button clearly is a bug IMO.

A whacky workaround is to hook into the mouse/Motion/Listener installed by the uidelegate and silently eat all events which are not the left button. Something like (note: a more robust install of this wrapping listener which would survive a LAF switch is outlined in a recent answer):

public static class BugHook implements MouseListener, MouseMotionListener {

    private JTableHeader header;
    private MouseListener mouseDelegate;
    private MouseMotionListener motionDelegate;

    public BugHook(JTableHeader header) {
        this.header = header;
        MouseListener[] ls = header.getMouseListeners();
        for (int i = 0; i < ls.length; i++) {
            header.removeMouseListener(ls[i]);
            String name = ls[i].getClass().getName();
            if (name.contains("TableHeaderUI")) {
                this.mouseDelegate = ls[i];
                ls[i] = this;
            }
        }
        for (MouseListener l : ls) {
            header.addMouseListener(l);
        }

        MouseMotionListener[] motionLs = header.getMouseMotionListeners();
        for (int i = 0; i < motionLs.length; i++) {
            header.removeMouseMotionListener(motionLs[i]);
            String name = motionLs[i].getClass().getName();
            if (name.contains("TableHeaderUI")) {
                this.motionDelegate = motionLs[i];
                motionLs[i] = this;
            }
        }
        for (MouseMotionListener l : motionLs) {
            header.addMouseMotionListener(l);
        }

    }

    // methods delegation left buttons only
    @Override
    public void mousePressed(MouseEvent e) {
        if (!SwingUtilities.isLeftMouseButton(e)) return;
        mouseDelegate.mousePressed(e);
    }

    @Override
    public void mouseDragged(MouseEvent e) {
        if (!SwingUtilities.isLeftMouseButton(e)) return;
        motionDelegate.mouseDragged(e);
    }
    @Override
    public void mouseReleased(MouseEvent e) {
        if (!SwingUtilities.isLeftMouseButton(e)) return;
        mouseDelegate.mouseReleased(e);
    }

    /// ---------- methods delegating always
    @Override
    public void mouseClicked(MouseEvent e) {
        mouseDelegate.mouseClicked(e);
    }
    @Override
    public void mouseEntered(MouseEvent e) {
        mouseDelegate.mouseEntered(e);
    }

    @Override
    public void mouseExited(MouseEvent e) {
        mouseDelegate.mouseExited(e);
    }
    @Override
    public void mouseMoved(MouseEvent e) {
        motionDelegate.mouseMoved(e);
    }

}
1
On

I tried with Java versions 1.7.0_11 and 1.6.0_38 and doing this:

table.getTableHeader().setReorderingAllowed(false);

will lock the columns in place. Are you perhaps using older Java version or doing the disabling some other way?