想对对(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)
Kuncinya ialah kedudukan itu tidak wujud apabila anda menetapkannya...
Tambah lajur dan baris dahulu, kerana kedudukan set anda ialah 1,1, sekurang-kurangnya 2 baris dan 2 lajur