⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 e962. sorting a column in a jtable component.txt

📁 这里面包含了一百多个JAVA源文件
💻 TXT
字号:
This example implements a method that sorts the data of a particular column of a DefaultTableModel. 
    DefaultTableModel model = new DefaultTableModel();
    JTable table = new JTable(model);
    
    // Add data here...
    
    // Disable autoCreateColumnsFromModel otherwise all the column customizations
    // and adjustments will be lost when the model data is sorted
    table.setAutoCreateColumnsFromModel(false);
    
    // Sort the values in the second column of the model
    // in descending order
    int mColIndex = 1;
    boolean ascending = false;
    sortColumn(model, mColIndex, ascending);
    
    // Regardless of sort order (ascending or descending), null values always appear last.
    // colIndex specifies a column in model.
    public void sortColumn(DefaultTableModel model, int colIndex, boolean ascending) {
        Vector data = model.getDataVector();
        Object[] colData = new Object[model.getRowCount()];
    
        // Copy the column data in an array
        for (int i=0; i<colData.length; i++) {
            colData[i] = ((Vector)data.get(i)).get(colIndex);
        }
    
        // Sort the array of column data
        Arrays.sort(colData, new ColumnSorter(ascending));
    
        // Copy the sorted values back into the table model
        for (int i=0; i<colData.length; i++) {
            ((Vector)data.get(i)).set(colIndex, colData[i]);
        }
        model.fireTableStructureChanged();
    }
    
    public class ColumnSorter implements Comparator {
        boolean ascending;
        ColumnSorter(boolean ascending) {
            this.ascending = ascending;
        }
        public int compare(Object a, Object b) {
            // Treat empty strains like nulls
            if (a instanceof String && ((String)a).length() == 0) {
                a = null;
            }
            if (b instanceof String && ((String)b).length() == 0) {
                b = null;
            }
    
            // Sort nulls so they appear last, regardless
            // of sort order
            if (a == null && b == null) {
                return 0;
            } else if (a == null) {
                return 1;
            } else if (b == null) {
                return -1;
            } else if (a instanceof Comparable) {
                if (ascending) {
                    return ((Comparable)a).compareTo(b);
                } else {
                    return ((Comparable)b).compareTo(a);
                }
            } else {
                if (ascending) {
                    return a.toString().compareTo(b.toString());
                } else {
                    return b.toString().compareTo(a.toString());
                }
            }
        }
    }

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -