我如何排序带有空行的Java JTable并强制空行始终为最后? [英] How can i sort java JTable with an empty Row and force the Empty row always be last?

查看:125
本文介绍了我如何排序带有空行的Java JTable并强制空行始终为最后?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用JTable,在表的底部有一个空行,以便能够向表中添加新行.

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.

在空行中插入或写入数据后,我将在其下方自动添加一个新的空行. (它的行为类似于Microsoft的可视表)

After insert or writing data in the empty row i am adding automtacly a new empty row below it. (It suppose to act like the Microsoft visual tables)

我正在使用Java默认行排序器,

I am using the java default row sorter,

问题是我一直需要空行成为最后一行!但对表格进行排序后,它成为第一行.

The problem is that i need the empty row to be the last row all the time! but after sorting the table it become the first row.

DefaultRowSorter 类的" compare(int model1,int model2)"方法获取2行号,如果第一行的值是1,则返回-1. null,如果第二行的值为null,则返回1.并以-1递减以得到倒序.

The "compare(int model1, int model2)" method of the DefaultRowSorter class is getting 2 row numbers and return -1 if the value of the first row is null and 1 if the value of the second row is null. and incase of DESCENDING it mult by -1 to get the inverted order.

            //Treat nulls as < then non-null
            if (v1 == null) {
                if (v2 == null) {
                    result = 0;
                } else {
                    result = -1;
                }
            } else if (v2 == null) {
                result = 1;
            } else {
                result = sortComparators[counter].compare(v1, v2);
            }
            if (sortOrder == SortOrder.DESCENDING) {
                result *= -1;
            }

空行被排序为最小值,如果以降序排列,它将是第一行(由于mult为-1),因此会引起很多问题.

The empty line is sorted as the smallest value and incase of DESCENDING it will be the first line (because of the mult by -1) and causes alot of problems.

我可以覆盖它,以防空行(通常是最后一行)在DESCENDING模式下不乘-1,并且它将是任何排序后的最后一行. 但是问题在于,"比较"方法及其调用方""内部类在 DefaultRowSorter 中是私有的.

I could overide it and incase of the empty row(usualy the last row) do not mult by -1 in DESCENDING mode, and it will be the last row after any sorting. But the problem is that the "compare" method and it's caller "Row" inner class are private inside the DefaultRowSorter.

有没有一种方法可以避免对空行进行排序并使它始终是最后一行?

Is there a way to avoid sorting the empty row and make it always the last row?

推荐答案

我尝试了一下,我想找到了解决方法.

I gave this a try, and I think I found a solution.

我没有在TableModel中创建该空行,而是在JTable中对其进行了伪造,并且仅在用户实际输入一些数据时创建它.

Instead of creating that empty row in the TableModel, I fake it in the JTable, and only create it when the user actually enters some data.

RowSorter仅对TableModel的行进行排序,因此我们的行不受影响,并保留为最后一行.

The RowSorter only sorts rows of the TableModel, so our row is not affected and remains as the last row.

public class NewLineTable extends JTable {

    @Override
    public int getRowCount() {
        // fake an additional row
        return super.getRowCount() + 1;
    }

    @Override
    public Object getValueAt(int row, int column) {
        if(row < super.getRowCount()) {
            return super.getValueAt(row, column);
        }
        return ""; // value to display in new line
    }

    @Override
    public int convertRowIndexToModel(int viewRowIndex) {
        if(viewRowIndex < super.getRowCount()) {
            return super.convertRowIndexToModel(viewRowIndex);
        }
        return super.getRowCount(); // can't convert our faked row
    }

    @Override
    public void setValueAt(Object aValue, int row, int column) {
        if(row < super.getRowCount()) {
            super.setValueAt(aValue, row, column);
        }
        else {
            Object[] rowData = new Object[getColumnCount()];
            Arrays.fill(rowData, "");
            rowData[convertColumnIndexToModel(column)] = aValue;
            // That's where we insert the new row.
            // Change this to work with your model.
            ((DefaultTableModel)getModel()).addRow(rowData);
        }
    }
}

这篇关于我如何排序带有空行的Java JTable并强制空行始终为最后?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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