JTable 中的列移动 [完成] 事件
我应该如何检测 JTable 中的列移动操作已完成?我已将columnModeListener 添加到我的列模型中,但问题是每次列移动(按某些像素)时都会调用columnMoved 方法。我不想要这种行为。我只想检测列拖动何时完成。
columnModel.addColumnModelListener(new TableColumnModelListener() {
public void columnAdded(TableColumnModelEvent e) {
}
public void columnRemoved(TableColumnModelEvent e) {
}
public void columnMoved(TableColumnModelEvent e) {
//this is called so many times
//I don't want this, but something like column moved finished event
System.out.println("Moved "+e.getFromIndex()+", "+e.getToIndex());
}
public void columnMarginChanged(ChangeEvent e) {
}
public void columnSelectionChanged(ListSelectionEvent e) {
}
});
我希望我在寻找什么很清楚。谢谢。
How should I detect that column moved action is finished in JTable? I've added columnModeListener to my column model but the problem is columnMoved method is called every time a column moves (by certain pixels). I don't want this behavior. I just want to detect when the column dragging is finished.
columnModel.addColumnModelListener(new TableColumnModelListener() {
public void columnAdded(TableColumnModelEvent e) {
}
public void columnRemoved(TableColumnModelEvent e) {
}
public void columnMoved(TableColumnModelEvent e) {
//this is called so many times
//I don't want this, but something like column moved finished event
System.out.println("Moved "+e.getFromIndex()+", "+e.getToIndex());
}
public void columnMarginChanged(ChangeEvent e) {
}
public void columnSelectionChanged(ListSelectionEvent e) {
}
});
I hope it is clear what I'm looking for. Thanks.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(7)
这就是我最终所做的。我知道它很脏,但它适合我正在寻找的东西:
This is what I ended up doing. I know it is dirty, but it fits for what I'm looking:
这是我用来确定列顺序何时更改的内部类。请注意,此时用户可能还没有松开鼠标,因此拖动可能会继续进行。
这
对我来说效果很好。
Here's an inner class I use to determine when the column ordering has changed. Note that the user may not have let go of the mouse at this point, so the dragging may continue further.
}
It's worked well for me.
这可能是一个更好的方法:
This might be a better approach:
这对我有用(列移动和边距调整):
我扩展表格并覆盖
columnMoved
和columnMarginChanged
方法如下:
... 首先添加一些用于状态保持的变量
...
... 然后在我的表的构造函数中我这样做:
这有点像黑客,但它对我有用。
This is what works for me (both column movements and margin resizes):
I extend the table and override the
columnMoved
andcolumnMarginChanged
methods in the following way:
... first add some variables for state keeping
...
... then in the constructor of my table i do this:
It is kinda like a hack, but it works for me.
关于您自己的问题的答案很好ashokgelal。我认为只是一点点改进。您的代码也会在单击标题时触发。当列没有真正更改时,再使用一个标志可以防止“dragComplete”触发器。
修改后的代码:
Nice answer on your own question ashokgelal. Just a little improvement I think. Your code also trigger on single click on the header. Using one more flag you can prevent the 'dragComplete' trigger when the column haven't really changed.
Modified code:
如果我理解正确的话,也许你想看看鼠标监听器。也许是 MOUSE_RELEASED 事件?
If I understand you correctly, maybe you want to look at the mouse listeners. Maybe the MOUSE_RELEASED event?
所有答案都在一种用例中失败:如果表的布局填满整个窗口,则调整窗口大小将调整表及其列的大小。通过监视列标题上的鼠标事件,当用户调整窗口大小时,我们无法接收到该事件。
我查看了 JTable&friends 源代码,columnMarginChanged() 方法总是在 JTable.doLayout() 调用的 sub-sub-sub... 函数中调用。
然后,我的解决方案是监视至少触发一个 columnMarginChanged() 的 doLayout() 调用。
事实上,每一列都会调用columnMarginChanged()。
这是我的解决方案:
All answers fail at one use-case: if the table is in a layout filling up the entire window, then resizing the window will resize the table and thus its columns. By watching for mouse events on the column headers, we fail to receive the event when the user resize the window.
I looked at the JTable&friends source-code, and the columnMarginChanged() method is always called in a sub-sub-sub...-function called by JTable.doLayout().
Then, my solution is to watch for doLayout() calls that trigger at least one columnMarginChanged().
In fact, columnMarginChanged() is called for every columns.
Here is my solution: