JTable的行条目少于显示和排序问题 [英] JTable with less row entries than displayed and sorting issues

查看:61
本文介绍了JTable的行条目少于显示和排序问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用JTable对象显示一些存储到数据库中的数据. 现在,我想获得以下结果:

I'm using a JTable object to show some datas stored into a database. Now I would like to achieve the following result:

即使我的数据库表中的条目较少,显示的JTable也必须至少具有n行(使其为10)行.空行必须始终显示在表的末尾.

The displayed JTable must have at least n (let it be 10 ) rows even if my db table has less entry. The empty row must always be displayed at the end of the table.

从现在开始,我将执行以下操作:

Since now I do the following:

我实现了扩展AbstractTableModel的自定义表格模型.在实现的方法getRowCount中,我做到了:

I implemented my custom tabel model extending AbstractTableModel. And in the implemented method getRowCount I did:

public int getRowCount() {
       if (myRowCount < 10 )

           return 10;
       else 
           return myRowCount;
}

问题是:

  1. 此代码可以正常工作,但我当时 想知道这是否正确 实现此结果的方法.是 这是一个好的解决方案?
  2. 除此之外,如果我尝试排序 我得到了不希望有的输出的行.实际上,排序时似乎考虑了空行,产生的结果是(例如,按后代顺序对int列进行排序)空行显示在表的开头.我是否必须实现我的自定义TableRowSorter来避免该行为,或者是否可以使用默认行为(table.setAutoCreateRowSorter(true))?
  1. This code works fine, but I was wondering if this is the right approach to achieve this result. Is this a good solution?
  2. In addition to that, if I try sort the row I got an undesired output. In fact empty rows seems to be taked in account while sorting, producing the result that (for example ordering a colum of int in descendant order) empty rows are displayed at the beginning of the table. Do I have to implement my custom TableRowSorter to avoid that behavior, or is it possible with the default one (table.setAutoCreateRowSorter(true)) ?

预先感谢

我阅读了带有@kleopatra @camickr的@mKorbel链接帖子.它对我有很大帮助.但是我真的想以最简洁的方式做到这一点,因为此类在我的应用程序中将非常重要. 前一个链接文章中提出的解决方案是否足够干净?还是有一些我现在看不到的缺点?

i read @mKorbel links with @kleopatra @camickr posts. It help me a lot. But I really want to do that in the cleanest way as possible, because this class will be very important in my application. Is the solution proposed in the previous linked post clean enough? Or it has some disadvantages that I can't see right now?

我开始赏金.请给我很好的建议! 谢谢.

I started a bounty. Please give me good advices!! thanks..

马可

推荐答案

如果您希望能够编辑这些空行,那么我想唯一的方法是覆盖DefaultRowSortergetComparator(column)方法.

If you want to be able to edit these empty rows, then I guess the only way is to override DefaultRowSorters getComparator(column) method.

编辑我以为您可以覆盖getComparator(),或为每个列设置一个Comparator,以便它检查值之一是否为null等. ,因为DefaultRowSorter中存在null检查,并且还有其他问题.

EDIT I thought you could either override getComparator(), or set a Comparator to each column so that it checks if one of the values is null etc. I tried it, it didn't work, because there are null checks in the DefaultRowSorter, and there are other problems.

不幸的是,DefaultRowSorter中的大多数内容都是私有的,因此自定义的空间不大.如果您想要这种行为,则必须编写自己的RowSorter实现.我建议您仅在此功能对您的应用绝对重要时才采用这种方法.

Unfortunately, most stuff in DefaultRowSorter is private, so there's not much room for customising. If you want this behaviour, you'd have to write your own implementation of RowSorter. I'd recommend you to take this approach only if this functionality is absolutely critical to your app.

这篇关于JTable的行条目少于显示和排序问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆