简体   繁体   English

使用数据向量对JTable进行排序

[英]sorting a JTable using a data vector

I got stuck with a performance problem while writing my program and I need your help! 我在编写程序时遇到性能问题,需要您的帮助! :) :)

I'm using a JTable to view test results taken from a vector I made and it has 4 columns in it. 我正在使用JTable来查看从我制作的向量中获得的测试结果,并且其中有4列。 When I click on a row the details from a saved txt file of that test are shown in a child window. 当我单击一行时,该测试的已保存txt文件中的详细信息将显示在子窗口中。 Also, when I click on the columns header the event sends the vector to a function that sorts it according to the pressed column. 另外,当我单击栏标题时,该事件会将向量发送到根据所按栏对它进行排序的函数。 Every time a new value needs to be entered the sorting function is called again. 每次需要输入新值时,都会再次调用排序功能。

My program works fine with a small number of rows. 我的程序可以在少数行中正常运行。 But, when I enter say, 150 rows, every time I enter a new row the Table flicks (the sort probably takes a lot of time), but I have to keep the vector synchronized with the jable because of the "push to view the result" option. 但是,当我输入说150行时,每当我进入一个新行时,Table都会滑动(排序可能会花费很多时间),但是由于“按下以查看该行,结果”选项。

I would really appreciate some help with this. 我真的很感谢您的帮助。 thanks 谢谢

You shouldn't have to do any sorting yourself. 您不必自己进行任何排序。 JTable supports sorting natively, and has the convertRowIndexToModel and convertRowIndexToView methods to go from the view index to the model index and vice-versa. JTable本机支持排序,并且具有convertRowIndexToModelconvertRowIndexToView方法,以从视图索引到模型索引,反之亦然。

See http://docs.oracle.com/javase/tutorial/uiswing/components/table.html#sorting . 请参阅http://docs.oracle.com/javase/tutorial/uiswing/components/table.html#sorting

Use JTable's internal sorter (DefaultRowSorter). 使用JTable的内部排序器(DefaultRowSorter)。 Do not re-create the vector which holds the data - use Vector's add() method to add new records. 不要重新创建保存数据的矢量-使用Vector的add()方法添加新记录。 In many years of Java GUI development I haven't seen a single case where I had to keep records in the TableModel sorted. 在Java GUI开发的许多年中,我从未见过需要将TableModel中的记录排序的情况。 Make sure getColumnClass() returns a proper type, so the default sorter knows how to sort the column, and that is all. 确保getColumnClass()返回正确的类型,以便默认排序器知道如何对列进行排序,仅此而已。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM