Sort JTable Except for Last Row

后端 未结 5 1246
盖世英雄少女心
盖世英雄少女心 2020-12-03 23:54

I have a JTable where the last row is the total row that aggregates all other rows. When the user clicks a column header on the table, rows are sorted by that c

5条回答
  •  北海茫月
    2020-12-04 00:28

    Based on this example, the complete example example below illustrates the approach in the accepted answer. In particular,

    • The implementation of getRowCount() in the TableModel returns the size() of the List, which is one less than the number of rows displayed in the table.

    • The implementation of getValueAt() in the TableModel illustrates the call to sum() in the COLUMN_SALARY and some possible display choices for the remaining columns in the extra row at the bottom.

    • The implementation of getViewRowCount() in the TableRowSorter enables the display of the extra row but precludes reasonable filtering.

    import java.awt.BorderLayout;
    import java.awt.Component;
    import java.awt.Dimension;
    import java.awt.EventQueue;
    import java.text.NumberFormat;
    import java.util.ArrayList;
    import java.util.List;
    import javax.swing.JFrame;
    import javax.swing.JLabel;
    import javax.swing.JScrollPane;
    import javax.swing.JTable;
    import javax.swing.table.AbstractTableModel;
    import javax.swing.table.DefaultTableCellRenderer;
    import javax.swing.table.TableModel;
    import javax.swing.table.TableRowSorter;
    
    /**
     * @see https://stackoverflow.com/a/37913520/230513
     * @see https://stackoverflow.com/a/37892395/230513
     */
    public class JTableColumnTotalExample {
    
        public static void main(String[] args) {
            EventQueue.invokeLater(JTableColumnTotalExample::display);
        }
    
        public static void display() {
            JFrame f = new JFrame("JTable Sorting Example");
            f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            List listEmployees = createListEmployees();
            TableModel model = new EmployeeTableModel(listEmployees);
            JTable table = new JTable(model) {
                @Override
                public Dimension getPreferredScrollableViewportSize() {
                    return new Dimension(500, getRowCount() * getRowHeight());
                }
            };
            table.getColumnModel().getColumn(3).setCellRenderer(new CurrencyFormatter());
            TableRowSorter sorter = new TableRowSorter(model) {
                @Override
                public int convertRowIndexToModel(int index) {
                    int maxRow = super.getViewRowCount();
                    if (index >= maxRow) {
                        return index;
                    }
                    return super.convertRowIndexToModel(index);
                }
    
                @Override
                public int convertRowIndexToView(int index) {
                    int maxRow = super.getModelRowCount();
                    if (index > maxRow) {
                        return index;
                    }
                    return super.convertRowIndexToView(index);
                }
    
                @Override
                public int getViewRowCount() {
                    return super.getViewRowCount() + 1;
                }
            };
            table.setRowSorter(sorter);
            f.add(new JScrollPane(table), BorderLayout.CENTER);
            f.pack();
            f.setLocationRelativeTo(null);
            f.setVisible(true);
        }
    
        private static List createListEmployees() {
            List listEmployees = new ArrayList<>();
            listEmployees.add(new Employee("Peter", "Manager", 40000));
            listEmployees.add(new Employee("Paul", "Programmer", 25000));
            listEmployees.add(new Employee("Mary", "Designer", 25000));
            listEmployees.add(new Employee("Donald", "Leader", 30000));
            listEmployees.add(new Employee("Tom", "Designer", 28000));
            listEmployees.add(new Employee("Samantha", "Analyst", 50000));
            listEmployees.add(new Employee("Jerome", "Programmer", 32000));
            listEmployees.add(new Employee("Jonathon", "Developer", 29000));
            listEmployees.add(new Employee("Kevin", "Programmer", 23000));
            listEmployees.add(new Employee("Anthony", "Programmer", 23000));
            listEmployees.add(new Employee("John", "Designer", 33000));
            listEmployees.add(new Employee("David", "Developer", 28000));
            listEmployees.add(new Employee("Harry", "Designer", 31000));
            listEmployees.add(new Employee("Charles", "Programmer", 26000));
            listEmployees.add(new Employee("Joseph", "Manager", 40000));
            return listEmployees;
        }
    
        private static class EmployeeTableModel extends AbstractTableModel {
    
            private static final int COLUMN_NUM = 0;
            private static final int COLUMN_NAME = 1;
            private static final int COLUMN_JOB = 2;
            private static final int COLUMN_SALARY = 3;
    
            private final String[] columnNames = {"No", "Name", "Job", "Salary"};
            private final List listEmployees;
    
            public EmployeeTableModel(List listEmployees) {
                this.listEmployees = listEmployees;
                int indexCount = 1;
                for (Employee employee : listEmployees) {
                    employee.setIndex(indexCount++);
                }
            }
    
            @Override
            public int getColumnCount() {
                return columnNames.length;
            }
    
            @Override
            public int getRowCount() {
                return listEmployees.size();
            }
    
            @Override
            public String getColumnName(int columnIndex) {
                return columnNames[columnIndex];
            }
    
            @Override
            public Class getColumnClass(int columnIndex) {
                return getValueAt(0, columnIndex).getClass();
            }
    
            @Override
            public Object getValueAt(int rowIndex, int columnIndex) {
                if (rowIndex == getRowCount()) {
                    switch (columnIndex) {
                        case COLUMN_NUM:
                            return 999_999_999;
                        case COLUMN_NAME:
                            return "Total";
                        case COLUMN_JOB:
                            return "Salary";
                        case COLUMN_SALARY:
                            return sum();
                    }
                }
                Employee employee = listEmployees.get(rowIndex);
                switch (columnIndex) {
                    case COLUMN_NUM:
                        return employee.getIndex();
                    case COLUMN_NAME:
                        return employee.getName();
                    case COLUMN_JOB:
                        return employee.getJob();
                    case COLUMN_SALARY:
                        return employee.getSalary();
                    default:
                        throw new IllegalArgumentException("Invalid column index");
                }
            }
    
            private int sum() {
                int sum = 0;
                for (int r = 0; r < getRowCount(); r++) {
                    sum += (int) getValueAt(r, COLUMN_SALARY);
                }
                return sum;
            }
        }
    
        private static class Employee {
    
            private int index;
            private String name;
            private String job;
            private int salary;
    
            public Employee(String name, String job, int salary) {
                this.name = name;
                this.job = job;
                this.salary = salary;
            }
    
            public int getIndex() {
                return index;
            }
    
            public void setIndex(int index) {
                this.index = index;
            }
    
            public String getName() {
                return name;
            }
    
            public void setName(String name) {
                this.name = name;
            }
    
            public String getJob() {
                return job;
            }
    
            public void setJob(String job) {
                this.job = job;
            }
    
            public int getSalary() {
                return salary;
            }
    
            public void setSalary(int age) {
                this.salary = age;
            }
        }
    
        private static class CurrencyFormatter extends DefaultTableCellRenderer {
    
            private NumberFormat numberFormat = NumberFormat.getCurrencyInstance();
    
            @Override
            public Component getTableCellRendererComponent(JTable jTable, Object value,
                boolean isSelected, boolean hasFocus, int row, int column) {
                Component c = super.getTableCellRendererComponent(jTable, value,
                    isSelected, hasFocus, row, column);
                if (c instanceof JLabel && value instanceof Number) {
                    JLabel label = (JLabel) c;
                    label.setHorizontalAlignment(JLabel.RIGHT);
                    Number num = (Number) value;
                    String text = numberFormat.format(num);
                    label.setText(text);
                }
                return c;
            }
        }
    }
    

提交回复
热议问题