How can i sort java JTable with an empty Row and force the Empty row always be last?

后端 未结 5 892
心在旅途
心在旅途 2020-12-18 16:49

I am using JTable with an empty row at the bottom of the table in order to give the ability of adding new line to the table.

After insert or writing data in the empt

5条回答
  •  刺人心
    刺人心 (楼主)
    2020-12-18 17:06

    I know it is an old thread, but I was stuck here trying to find a way. Please, let me know if there is a new way not mentioned before. Actually this is my first post on SO, but since I spent lots of time finding a solution for that I decided to share.

    I solved that ordering problem by, instead of using 'null' as my 'new entry', I created a specific object (in my case from Object class itself, but it could be something more meaninful like a 'NewEntryDoNotCompareWithMePleaseLetMeBeTheLastEntryIBegYouDoNotSortMe' empty class).

    When comparing, I first check if the 'object' that came to compare is a object. Not 'instanceof' of course, but (obj.getClass() == Object.class). -- If thats the case, check the sorting order and return 1 or -1.

    Feel free to comment the any problems you find with that, I can try to create a small-working-version if someone needs the code.

    Cheers, Daniel

提交回复
热议问题