如何通过外部提供列索引来对JTable进行排序

时间:2010-06-16 07:41:41

标签: java swing

我想通过在程序外部提供列索引来实现JTable的排序。这是我的示例代码,其中我已初始化JTable,添加一列和30行到JTable。添加行后,我通过提供列索引0来排序JTable,但我无法获取排序数据。如何按排序顺序获取我的第一列?我的代码出了什么问题。为什么sortCTableonColumnIndex()方法无法对指定列索引的数据进行排序?

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Component;
import java.awt.Dimension;
import java.util.ArrayList;
import java.util.List;
import javax.swing.JDialog;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.RowSorter;
import javax.swing.SortOrder;
import javax.swing.table.DefaultTableCellRenderer;
import javax.swing.table.DefaultTableModel;
import javax.swing.table.JTableHeader;
import javax.swing.table.TableColumn;
import javax.swing.table.TableRowSorter;

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
/**
 *
 * @author Admin
 */
public class Test {

    private JTable oCTable;
    private DefaultTableModel oDefaultTableModel;
    private JScrollPane oPane;
    private JTableHeader oTableHeader;
    private TableRowSorter sorter;

    public void adddata() {
        for (int i = 0; i < 30; i++) {
            Object[] row = new Object[1];
            String sValueA = "A";
            String sValueB = "A";
            row[0] = "";
            if (i % 2 == 0) {
                if (i < 15) {
                    sValueA = sValueA + sValueA;
                    row[1] = sValueA;
                } else {
                    if (i == 16) {
                        sValueB = "D";
                        row[1] = sValueA;
                    } else {
                        sValueB = sValueB + sValueB;
                        row[1] = sValueA;
                    }

                }
            } else {
                if (i < 15) {
                    sValueB = sValueB + sValueB;
                    row[1] = sValueB;
                } else {
                    if (i == 17) {
                        sValueB = "C";
                        row[1] = sValueB;
                    } else {
                        sValueB = sValueB + sValueB;
                        row[1] = sValueB;
                    }

                }
            }

        }
    }

    public void createTable() {
        oCTable = new JTable();
        oDefaultTableModel = new DefaultTableModel();
        oCTable.setModel(oDefaultTableModel);
        oTableHeader = oCTable.getTableHeader();
        oCTable.setAutoResizeMode(oCTable.AUTO_RESIZE_OFF);
        oCTable.setFillsViewportHeight(true);
        JTable oTable = new LineNumberTable(oCTable);
        oPane = new JScrollPane(oCTable);
        oPane.setRowHeaderView(oTable);
        JPanel oJPanel = new JPanel();
        oJPanel.setLayout(new BorderLayout());
        oJPanel.add(oPane, BorderLayout.CENTER);
        JDialog oDialog = new JDialog();
        oDialog.add(oJPanel);
        oDialog.setPreferredSize(new Dimension(500, 300));
        oDialog.pack();
        oDialog.setVisible(true);

    }

    public void insert() {
        oDefaultTableModel.addColumn("Name");
        int iColumnPlace = ((DefaultTableModel) oCTable.getModel()).findColumn("Name");
        CellRendererForRowHeader oCellRendererForRowHeader = new CellRendererForRowHeader();
        TableColumn Column = oCTable.getColumn(oTableHeader.getColumnModel().getColumn(iColumnPlace).getHeaderValue());
        Column.setPreferredWidth(300);
        Column.setMaxWidth(300);
        Column.setMinWidth(250);
        Column.setCellRenderer(oCellRendererForRowHeader);



        for (int i = 0; i < 30; i++) {
            Object[] row = new Object[1];
            String sValueA = "A";
            if (i % 2 == 0) {
                if (i < 15) {
                    sValueA = sValueA + "a";
                    oDefaultTableModel.insertRow(oCTable.getRowCount(), new Object[]{""});
                    oDefaultTableModel.setValueAt(sValueA, i, 0);
                } else {
                    if (i == 16) {
                        sValueA = sValueA + "b";
                        oDefaultTableModel.insertRow(oCTable.getRowCount(), new Object[]{""});
                        oDefaultTableModel.setValueAt(sValueA, i, 0);
                    } else {
                        sValueA = sValueA + "c";
                        oDefaultTableModel.insertRow(oCTable.getRowCount(), new Object[]{""});
                        oDefaultTableModel.setValueAt(sValueA, i, 0);
                    }

                }
            } else {
                if (i < 15) {
                    sValueA = sValueA + "d";
                    oDefaultTableModel.insertRow(oCTable.getRowCount(), new Object[]{""});
                    oDefaultTableModel.setValueAt(sValueA, i, 0);
                } else {
                    if (i == 17) {
                        sValueA = sValueA + "e";
                        oDefaultTableModel.insertRow(oCTable.getRowCount(), new Object[]{""});
                        oDefaultTableModel.setValueAt(sValueA, i, 0);
                    } else {
                        sValueA = sValueA + "f";
                        oDefaultTableModel.insertRow(oCTable.getRowCount(), new Object[]{""});
                        oDefaultTableModel.setValueAt(sValueA, i, 0);
                    }

                }
            }

        }
    }

    public void showTable() {
        createTable();
        insert();
        sortCTableonColumnIndex(0, true);

    }

    public void sortCTableonColumnIndex(int iColumnIndex, boolean bIsAsc) {
        sorter = new TableRowSorter(oDefaultTableModel);
        List<RowSorter.SortKey> sortKeys = new ArrayList<RowSorter.SortKey>();
        if (bIsAsc) {
            sortKeys.add(new RowSorter.SortKey(iColumnIndex, SortOrder.ASCENDING));
        } else {
            sortKeys.add(new RowSorter.SortKey(iColumnIndex, SortOrder.DESCENDING));
        }
        sorter.setSortKeys(sortKeys);
        oDefaultTableModel.fireTableStructureChanged();
        oCTable.updateUI();


    }

    public static void main(String[] argu) {
        Test oTest = new Test();
        oTest.showTable();


    }

    class CellRendererForRowHeader extends DefaultTableCellRenderer {

        public Component getTableCellRendererComponent(JTable table, Object value,
                boolean isSelected, boolean hasFocus, int row, int column) {
            JLabel label = null;
            try {
                label = (JLabel) super.getTableCellRendererComponent(table, value, isSelected, hasFocus, row, column);
                if (column == 0) {
                    label.setBackground(new JLabel().getBackground());
                    label.setForeground(Color.BLACK);
                }
            } catch (RuntimeException ex) {
            }
            return label;

        }
    }

    class LineNumberTable extends JTable {

        private JTable mainTable;

        public LineNumberTable(JTable table) {
            super();
            mainTable = table;
            setAutoCreateColumnsFromModel(false);
            setModel(mainTable.getModel());
            setAutoscrolls(false);
            addColumn(new TableColumn());
            getColumnModel().getColumn(0).setCellRenderer(mainTable.getTableHeader().getDefaultRenderer());

            getColumnModel().getColumn(0).setPreferredWidth(40);
            setPreferredScrollableViewportSize(getPreferredSize());

        }

        @Override
        public boolean isCellEditable(int row, int column) {
            return false;
        }

        @Override
        public Object getValueAt(int row, int column) {
            return Integer.valueOf(row + 1);
        }

        @Override
        public int getRowHeight(int row) {
            return mainTable.getRowHeight();
        }
    }
}

2 个答案:

答案 0 :(得分:2)

方法的细微变化趋于促进分类。

public void sortCTableonColumnIndex(int iColumnIndex, boolean bIsAsc) {
        oCTable.setAutoCreateRowSorter(true);
        DefaultRowSorter sorter = ((DefaultRowSorter) oCTable.getRowSorter());
        List<RowSorter.SortKey> sortKeys = new ArrayList<RowSorter.SortKey>();
        if (bIsAsc) {
            sortKeys.add(new RowSorter.SortKey(iColumnIndex, SortOrder.ASCENDING));
        } else {
            sortKeys.add(new RowSorter.SortKey(iColumnIndex, SortOrder.DESCENDING));
        }
        sorter.setSortKeys(sortKeys);
    }

由于

答案 1 :(得分:2)

在编写Java代码时,你应该坚持Java coding standards,但无论你走哪条路,你都应该至少保持一致。

查看代码,实际上并没有在表格上设置分拣机。 From the JavaDocs您的代码中缺少以下内容:

TableModel myModel = createMyTableModel();
JTable table = new JTable(myModel);
table.setRowSorter(new TableRowSorter(myModel));

所以它会在你的代码中看起来像这样(或者至少删除了你的非标准约定)

public void createTable() {
    tableModel = new DefaultTableModel();
    table = new JTable(tableModel);
    sorter = new TableRowSorter<DefaultTableModel>(tableModel);
    table.setRowSorter(sorter);
    ...
}