java - JTable中 setValueAt() 方法ArrayIndexOutOfBoundsException
黄舟
黄舟 2017-04-18 09:24:16
0
1
641

想对对(1,1)单元格进行复制,代码如下


package org.occ.gui.table;

import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;

import javax.swing.JFrame;
import javax.swing.JScrollPane;
import javax.swing.JTable;

public class TableTest {

    /**
     * @param args
     */
    public static void main(String[] args) {
        // TODO Auto-generated method stub
        JFrame frame = new JFrame("welcome");
        JTable table2= new JTable();
        String name = "name";
        table2.setValueAt(name, 1, 1);

        JScrollPane scr = new JScrollPane(table2);
        frame.add(scr);
        frame.setSize(370,90);
        frame.setVisible(true);
        frame.addWindowListener(new WindowAdapter(){
            public void windowClosing(WindowEvent arg0){
                System.exit(1);
            }
        });

    }

}

报以下异常

Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 1 >= 0
    at java.util.Vector.elementAt(Vector.java:427)
    at javax.swing.table.DefaultTableColumnModel.getColumn(DefaultTableColumnModel.java:277)
    at javax.swing.JTable.convertColumnIndexToModel(JTable.java:2553)
    at javax.swing.JTable.setValueAt(JTable.java:2719)
    at org.occ.gui.table.TableTest.main(TableTest.java:27)
黄舟
黄舟

人生最曼妙的风景,竟是内心的淡定与从容!

Antworte allen(1)
左手右手慢动作

关键是你set的时候位置都不存在...
先把column和row添加上去吧,因为你set的位置是1,1 至少2行2列


package org.occ.gui.table;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;

import javax.swing.JFrame;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.SwingUtilities;
import javax.swing.table.DefaultTableModel;


public class TableTest {

    /**
     * @param args
     */
    public static void main(String[] args) {
        // TODO Auto-generated method stub

        SwingUtilities.invokeLater(new Runnable() {
            public void run() {
                JFrame frame = new JFrame("welcome");
                JTable table2 = new JTable();
                String name = "name";
                DefaultTableModel model = (DefaultTableModel) table2.getModel();
                model.addColumn("id");
                model.addColumn("name");
                Object[] row1 = new Object[]{1, null};
                model.addRow(row1);
                Object[] row2 = new Object[]{2, null};
                model.addRow(row2);
                table2.setValueAt(name, 1, 1);

                JScrollPane scr = new JScrollPane(table2);
                frame.add(scr);
                frame.setSize(370, 90);
                frame.setVisible(true);
                frame.addWindowListener(new WindowAdapter() {
                    public void windowClosing(WindowEvent arg0) {
                        System.exit(1);
                    }
                });
            }
        });
    }

}
Beliebte Tutorials
Mehr>
Neueste Downloads
Mehr>
Web-Effekte
Quellcode der Website
Website-Materialien
Frontend-Vorlage