问题描述
我有一个 JTable
,我需要能够重新排序列。但是我希望第一列无法重新排序。我使用以下命令启用重新排序:
I have a JTable
and I need to be able to reorder the columns. However I want the first column to not be able to be re-ordered. I used the following to enable reordering:
table.getTableHeader().setReorderingAllowed(true);
现在可以重新排序列,包括我不想要的第一列。有没有办法锁定第一列?
The columns can now be reordered including the first column which I don't want. Is there any way to lock the first column?
我见过一些使用两个表的解决方案,第一列在一个单独的表中,但也许有更好/更简单的方法。
I have seen some solutions that use two tables with the first column being in a separate table, but maybe there's a better/simpler way.
推荐答案
我认为您需要覆盖中的
。 columnMoved()
方法TableColumnModelListener TableColumnModelEvent
类有一个 getFromIndex()
方法,您应该能够查看它以确定它是否是您的固定列,然后你应该能够取消该事件。
I think that you need to override the columnMoved()
method in TableColumnModelListener
. the TableColumnModelEvent
class has a getFromIndex()
method that you should be able to look at to determine if it's your fixed column, and then you should be able to cancel the event.
希望有所帮助。 A
这篇关于如何防止单个列在JTable中重新排序?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!