我使用JTable控件显示数据,
点击列头自动排序功能已经开启table.setAutoCreateRowSorter(true),
在有table有数据的情况下没有问题
可是如果table没有数据的话,点击会出现以下异常
java.lang.ArrayIndexOutOfBoundsException:0 >= 0哪位仁兄能帮忙解决下,不甚感激!

解决方案 »

  1.   

          animationTableModel =
                    new RingAndAnimationTableModel(animationTableDatas,animationTableHeaderNames);
            animationTable = new IBAnimationTable(animationTableModel);
            animationTable.setPreferredScrollableViewportSize(new Dimension(400, 80));
            animationTable.setRowHeight(20);
            animationTable.setAutoResizeMode(JTable.AUTO_RESIZE_OFF);
            // 是否显示网格线
            animationTable.setShowGrid(false);
            animationTable.setAutoCreateRowSorter(true);
            JTableHeader tableHeader = animationTable.getTableHeader();
            tableHeader.setReorderingAllowed(false);        JScrollPane jsp = new JScrollPane();
            jsp.getViewport().add(animationTable);
            animationTable.setBackground(jsp.getBackground());
            animationPanel.add(jsp, gbc);
      

  2.   

    就是设置了自动排序功能,然后table没有数据的情况下,点击列头就出异常了
      

  3.   

    要启用行的排序和过滤,请使用 RowSorter。可以通过以下两种方式之一设置一个行排序器。 直接设置 RowSorter。例如:table.setRowSorter(new TableRowSorter(model))。 
    将 autoCreateRowSorter 属性设置为 true,从而 JTable 可用于创建 RowSorter。例如:setAutoCreateRowSorter(true)。 
      

  4.   

    我已经设置了setAutoCreateRowSorter(true)。 
    问题是在table中没有内容时,点列头会出异常,有数据时一切都OK
      

  5.   

    这个问题我也碰到了,并且解决了。
    问题的起因应该是:如果AutoCreateSorter属性被设置为true,那么JTable控件将自动地为每一列生成一个Sorter对象,其运行原理似乎类似于Compare interface,我不是很清楚,但是显然,如果JTable中的数据为空的话,这个动作将导致贴主所说的异常地被抛出。
    因此,解决办法就是:仅仅在JTable中被装入了数据时,才设置AutoCreateSorter为true,否则就将其关闭。
    注意,这里是另外一个trick,你不能简单地通过调用setAutoCreateSorter(false)的办法让JTable不再为每个列自动生成Sorter对象,而要用JTable.setRowSorter(null)的方法来做到这一点!
    不知道为什么setAutoCreateSorter(false)并不能够中止JTable自动地为每个列生成Sorter对象。
      

  6.   

    animationTableDatas是什么类型的?
    我从没有遇到过这样的问题。即使没有数据也一样。
    请看一下如下代码,你所给出的细节不足以判断。你对比一下下面的,和你的差别在哪里,再找出问题所在吧。不可轻信人言,别人的水平未必比你高。
    import java.awt.Container;
    import java.util.Vector;import javax.swing.JFrame;
    import javax.swing.JScrollPane;
    import javax.swing.JTable;
    import javax.swing.table.DefaultTableModel;public class TableFrame extends JFrame {
    private JTable table; private Vector<Vector<String>> data = new Vector<Vector<String>>();
    private Vector<String> colNames = new Vector<String>(); public TableFrame() {
    super(); initColnames();
    initData();
    Container c = getContentPane();
    table = new JTable();
    table.setModel(new DefaultTableModel(data, colNames));
    table.setModel(new DefaultTableModel() {
    /**
     *
     */
    private static final long serialVersionUID = 1L; @Override
    public int getRowCount() {
    return data.size();
    } @Override
    public int getColumnCount() {
    return colNames.size();
    } @Override
    public String getColumnName(int column) {
    return colNames.get(column);
    } @Override
    public Object getValueAt(int row, int column) {
    return data.get(row).get(column);
    }
    });
    table.setAutoCreateRowSorter(true);
    table.getTableHeader().setReorderingAllowed(false); c.add(new JScrollPane(table)); setDefaultCloseOperation(EXIT_ON_CLOSE);
    setSize(600, 400);
    setLocationRelativeTo(null);
    setVisible(true);
    } private void initColnames() {
    for (int i = 0; i < 5; i++) {
    colNames.add("Col" + i);
    }
    } private void initData() {
    for (int i = 0; i < 20; i++) {
    Vector<String> record = new Vector<String>();
    for (int j = 0; j < 5; j++) {
    record.add(i + " " + j);
    } data.add(record);
    }
    } public static void main(String[] args) {
    new TableFrame();
    }
    }
      

  7.   

    哈哈哈,谢谢bigreen啦!这个问题困扰我很久了!终于解决了!简直要跳起来了!呵呵!代码如下:
    jtable.setRowSorter(null);
    modle.getDataVector().removeAllElements();
    for (Alert a : alertList) { 
         if (a.getAlertType().getCode() == 1) {
    Object[] object = { ...};
         modle.add(object);
    }
    jtable.setRowSorter(sorter);
    modle.fireTableDataChanged();