gpt4 book ai didi

java - 重新定位 Jtable 列标题

转载 作者:行者123 更新时间:2023-11-30 11:42:03 24 4
gpt4 key购买 nike

我必须禁用右键单击 JTableHeader,这样用户就不能右键单击列标题并拖动以重新定位列。我们有什么技巧可以做到这一点吗?只是提到鼠标左键单击效果很好,但是当用户使用 RMB 并拖动列时,该列会移动并在释放鼠标时重新绘制在其他列上。

感谢任何帮助。

最佳答案

使用右键触发列拖动/调整大小显然是 IMO 的错误。

一个古怪的解决方法是挂接到 uidelegate 安装的鼠标/Motion/Listener 并默默地吃掉所有不是左键的事件。类似的东西(注意:这个包装监听器的更健壮的安装可以在 LAF 开关中存活下来是 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);
}

}

关于java - 重新定位 Jtable 列标题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11976019/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com