2 lcy 1024 lcy_1024 于 2014.05.08 00:10 提问

Swing Jtble 排序后,表格数据无法刷新

(源代码文件Test.java在最后已附上)
操作描述:
1.添加 -> 刷新
2.点击表头进行排序
3.清空 -> 刷新
4.刷新按钮失效,会报错,如何解决?(如果不点击表头排序,则功能正常)

Exception in thread "AWT-EventQueue-0" java.lang.ArrayIndexOutOfBoundsException: 0 >= 0
at java.util.Vector.elementAt(Vector.java:470)
at javax.swing.table.DefaultTableModel.getValueAt(DefaultTableModel.java:649)
at javax.swing.JTable.getValueAt(JTable.java:2720)
at javax.swing.JTable.prepareRenderer(JTable.java:5718)
at javax.swing.plaf.basic.BasicTableUI.paintCell(BasicTableUI.java:2114)
at javax.swing.plaf.basic.BasicTableUI.paintCells(BasicTableUI.java:2016)
at javax.swing.plaf.basic.BasicTableUI.paint(BasicTableUI.java:1812)
at javax.swing.plaf.ComponentUI.update(ComponentUI.java:161)
at javax.swing.JComponent.paintComponent(JComponent.java:778)
at javax.swing.JComponent.paint(JComponent.java:1054)
at javax.swing.JComponent.paintChildren(JComponent.java:887)
at javax.swing.JComponent.paint(JComponent.java:1063)
at javax.swing.JViewport.paint(JViewport.java:731)
at javax.swing.JComponent.paintChildren(JComponent.java:887)
at javax.swing.JComponent.paint(JComponent.java:1063)
at javax.swing.JComponent.paintToOffscreen(JComponent.java:5221)
at javax.swing.RepaintManager$PaintManager.paintDoubleBuffered(RepaintManager.java:1482)
at javax.swing.RepaintManager$PaintManager.paint(RepaintManager.java:1413)
at javax.swing.RepaintManager.paint(RepaintManager.java:1206)
......

整个测试代码文件如下:
Test.java

package examples;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Vector;

public class Test extends javax.swing.JFrame {

private Vector<Vector> tblDate; // 表数据
private Vector<String> tblHeaders; // 表头
private List<Integer> list; // 数据源

public Test() {
    tblDate = new Vector<Vector>();
    tblHeaders = new Vector<String>();
    list = new ArrayList<Integer>();
    tblHeaders.add("编号");
    tblHeaders.add("数据");

    initComponents();
    setLocationRelativeTo(null);
}

private void jButton2ActionPerformed(java.awt.event.ActionEvent evt) {
    // 添加
    list.add(list.size());
}

private void jButton3ActionPerformed(java.awt.event.ActionEvent evt) {
    // 清空
    list.clear();
}

private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {
    // 刷新
    tblDate.clear();

    for (int i : list) {
        tblDate.add(new Vector(Arrays.asList(i, "测试数据" + i)));
    }

    jTable1.repaint();
    jTable1.updateUI();
}

// 下面都是NetBeans自动生成代码,不要管
public static void main(String args[]) {
    try {
        for (javax.swing.UIManager.LookAndFeelInfo info
                : javax.swing.UIManager.getInstalledLookAndFeels()) {
            if ("Nimbus".equals(info.getName())) {
                javax.swing.UIManager.setLookAndFeel(info.getClassName());
                break;
            }
        }
    } catch (Exception ex) {
        // TODO
    }

    java.awt.EventQueue.invokeLater(new Runnable() {
        @Override
        public void run() {
            new Test().setVisible(true);
        }
    });
}

@SuppressWarnings("unchecked")
private void initComponents() {
    jScrollPane1 = new javax.swing.JScrollPane();
    jTable1 = new javax.swing.JTable();
    jButton1 = new javax.swing.JButton();
    jButton2 = new javax.swing.JButton();
    jButton3 = new javax.swing.JButton();

    setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);

    jTable1.setModel(new javax.swing.table.DefaultTableModel(
            this.tblDate, this.tblHeaders
    ));
    jTable1.setAutoCreateRowSorter(true);
    jScrollPane1.setViewportView(jTable1);

    jButton2.setText("添加");
    jButton2.addActionListener(new java.awt.event.ActionListener() {
        public void actionPerformed(java.awt.event.ActionEvent evt) {
            jButton2ActionPerformed(evt);
        }
    });

    jButton3.setText("清空");
    jButton3.addActionListener(new java.awt.event.ActionListener() {
        public void actionPerformed(java.awt.event.ActionEvent evt) {
            jButton3ActionPerformed(evt);
        }
    });

    jButton1.setText("刷新");
    jButton1.addActionListener(new java.awt.event.ActionListener() {
        public void actionPerformed(java.awt.event.ActionEvent evt) {
            jButton1ActionPerformed(evt);
        }
    });

    javax.swing.GroupLayout layout = new javax.swing
    .GroupLayout(getContentPane());
    getContentPane().setLayout(layout);
    layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                    .addContainerGap()
                    .addGroup(layout.createParallelGroup(javax.swing.GroupLayout
                            .Alignment.LEADING)
                            .addComponent(jScrollPane1, javax.swing.GroupLayout
                                    .PREFERRED_SIZE, 0, Short.MAX_VALUE)
                            .addGroup(layout.createSequentialGroup()
                                    .addGap(0, 168, Short.MAX_VALUE)
                                    .addComponent(jButton2)
                                    .addGap(18, 18, 18)
                                    .addComponent(jButton3)
                                    .addGap(18, 18, 18)
                                    .addComponent(jButton1)))
                    .addContainerGap())
    );
    layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                    .addContainerGap()
                    .addComponent(jScrollPane1,
                            javax.swing.GroupLayout.DEFAULT_SIZE, 190, Short
                                    .MAX_VALUE)
                    .addGap(18, 18, 18)
                    .addGroup(layout.createParallelGroup(javax.swing.GroupLayout
                            .Alignment.BASELINE)
                            .addComponent(jButton1)
                            .addComponent(jButton2)
                            .addComponent(jButton3))
                    .addContainerGap())
    );

    pack();
}

private javax.swing.JButton jButton1;
private javax.swing.JButton jButton2;
private javax.swing.JButton jButton3;
private javax.swing.JScrollPane jScrollPane1;
private javax.swing.JTable jTable1;
}
Csdn user default icon
上传中...
上传图片
插入图片
准确详细的回答,更有利于被提问者采纳,从而获得C币。复制、灌水、广告等回答会被删除,是时候展现真正的技术了!