Rumah  >  Soal Jawab  >  teks badan

java - JTable中 setValueAt() 方法ArrayIndexOutOfBoundsException

想对对(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)
黄舟黄舟2765 hari yang lalu635

membalas semua(1)saya akan balas

  • ringa_lee

    ringa_lee2017-04-18 09:26:16

    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

    
    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);
                        }
                    });
                }
            });
        }
    
    }
    

    balas
    0
  • Batalbalas