H
hon123456
I got the following exception when I try to press Enter in the cell in
order to add a row when the Enter is pressed.
Exception in thread "AWT-EventQueue-0"
java.lang.ArrayIndexOutOfBoundsException: 2 >= 2
at java.util.Vector.elementAt(Unknown Source)
at javax.swing.table.DefaultTableModel.justifyRows(Unknown Source)
at javax.swing.table.DefaultTableModel.insertRow(Unknown Source)
at javax.swing.table.DefaultTableModel.addRow(Unknown Source)
at javax.swing.table.DefaultTableModel.addRow(Unknown Source)
at NumericColumnTester$TblModel.addRow(NumericColumnTester.java:115)
at NumericColumnTester
$MyKeyListener.keyPressed(NumericColumnTester.java:143)
Here is My code, what's wrong with my code:
import java.awt.Component;
import java.awt.Toolkit;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.util.ArrayList;
import java.util.List;
import javax.swing.DefaultCellEditor;
import javax.swing.JFrame;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.JTextField;
import javax.swing.table.AbstractTableModel;
import javax.swing.table.DefaultTableModel;
import javax.swing.table.TableColumnModel;
import javax.swing.table.TableModel;
public class NumericColumnTester extends JFrame {
protected TblModel model_001;
public NumericColumnTester() {
// JTable table = new JTable(new Object[][] {
// { "a", new Integer(1), "2" }, { "b", new Integer(3), "4" } },
// new String[] { "Text", "Num 1", "Num 2" });
String[]header = { "Text", "Num 1", "Num 2" };
List data = new ArrayList();
data.add(new Object[]{ "a", new Integer(1), "2" });
data.add(new Object[]{ "b", new Integer(3), "4" });
model_001 = new TblModel(data, header);
model_001.addColumn("Text");
model_001.addColumn("Num1");
model_001.addColumn("Num2");
JTable table = new JTable(model_001);
table.setRowSelectionAllowed(false);
table.setSurrendersFocusOnKeystroke(true);
TableColumnModel cm = table.getColumnModel();
cm.getColumn(1).setCellEditor(new MyCellEditor());
cm.getColumn(2).setCellEditor(new MyCellEditor());
JScrollPane scrollPane = new JScrollPane(table);
getContentPane().add(scrollPane);
}
private class MyCellEditor extends DefaultCellEditor {
private final JTextField tf;
public MyCellEditor() {
super(new JTextField());
tf = (JTextField) getComponent();
tf.addKeyListener(new MyKeyListener());
setClickCountToStart(1);
}
public Component getTableCellEditorComponent(JTable table,
Object value, boolean isSelected, int row, int column) {
tf.setText(value.toString());
return tf;
}
public Object getCellEditorValue() {
return tf.getText();
}
}
class TblModel extends DefaultTableModel {
private List data;
private String[] header;
public TblModel(List data, String[] header) {
this.data = data;
this.header = header;
}
public boolean isCellEditable(int rowIndex, int columnIndex) {
return columnIndex == 1;
}
public int getColumnCount() {
return header == null ? 0 : header.length;
}
public int getRowCount() {
return data == null ? 0 : data.size();
}
public Object getValueAt(int rowIndex, int columnIndex) {
Object[] row = (Object[]) data.get(rowIndex);
return row[columnIndex];
}
public Class getColumnClass(int columnIndex) {
if (data == null) {
return Object.class;
}
Object[] row = (Object[]) data.get(0);
return row[columnIndex] == null ? Object.class :
row[columnIndex].getClass();
}
public String getColumnName(int column) {
return header[column];
}
public void setValueAt(Object aValue, int rowIndex, int columnIndex) {
Object[] row = (Object[]) data.get(rowIndex);
row[columnIndex] = aValue;
super.fireTableCellUpdated(rowIndex, columnIndex);
}
public void addRow(Object[] data){
super.addRow(data);
javax.swing.SwingUtilities.invokeLater(new Runnable(){
@Override
public void run(){
fireTableRowsInserted(0,0);
}
});
}
}
private class MyKeyListener implements KeyListener {
public void keyPressed(KeyEvent e) {int key = e.getKeyCode();
if (key == KeyEvent.VK_ENTER) {
Toolkit.getDefaultToolkit().beep();
System.out.println("ENTER pressed");
// THIS line cause the exception
model_001.addRow(new Object[] {"","",""});
//
}
}
public void keyReleased(KeyEvent e) {
}
public void keyTyped(KeyEvent e) {
if (!Character.isDigit(e.getKeyChar())) {
Toolkit.getDefaultToolkit().beep();
e.consume();
}
}
}
public static void main(String[] args) {
JFrame frame = new NumericColumnTester();
frame.setDefaultCloseOperation(EXIT_ON_CLOSE);
frame.pack();
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
}
order to add a row when the Enter is pressed.
Exception in thread "AWT-EventQueue-0"
java.lang.ArrayIndexOutOfBoundsException: 2 >= 2
at java.util.Vector.elementAt(Unknown Source)
at javax.swing.table.DefaultTableModel.justifyRows(Unknown Source)
at javax.swing.table.DefaultTableModel.insertRow(Unknown Source)
at javax.swing.table.DefaultTableModel.addRow(Unknown Source)
at javax.swing.table.DefaultTableModel.addRow(Unknown Source)
at NumericColumnTester$TblModel.addRow(NumericColumnTester.java:115)
at NumericColumnTester
$MyKeyListener.keyPressed(NumericColumnTester.java:143)
Here is My code, what's wrong with my code:
import java.awt.Component;
import java.awt.Toolkit;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.util.ArrayList;
import java.util.List;
import javax.swing.DefaultCellEditor;
import javax.swing.JFrame;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.JTextField;
import javax.swing.table.AbstractTableModel;
import javax.swing.table.DefaultTableModel;
import javax.swing.table.TableColumnModel;
import javax.swing.table.TableModel;
public class NumericColumnTester extends JFrame {
protected TblModel model_001;
public NumericColumnTester() {
// JTable table = new JTable(new Object[][] {
// { "a", new Integer(1), "2" }, { "b", new Integer(3), "4" } },
// new String[] { "Text", "Num 1", "Num 2" });
String[]header = { "Text", "Num 1", "Num 2" };
List data = new ArrayList();
data.add(new Object[]{ "a", new Integer(1), "2" });
data.add(new Object[]{ "b", new Integer(3), "4" });
model_001 = new TblModel(data, header);
model_001.addColumn("Text");
model_001.addColumn("Num1");
model_001.addColumn("Num2");
JTable table = new JTable(model_001);
table.setRowSelectionAllowed(false);
table.setSurrendersFocusOnKeystroke(true);
TableColumnModel cm = table.getColumnModel();
cm.getColumn(1).setCellEditor(new MyCellEditor());
cm.getColumn(2).setCellEditor(new MyCellEditor());
JScrollPane scrollPane = new JScrollPane(table);
getContentPane().add(scrollPane);
}
private class MyCellEditor extends DefaultCellEditor {
private final JTextField tf;
public MyCellEditor() {
super(new JTextField());
tf = (JTextField) getComponent();
tf.addKeyListener(new MyKeyListener());
setClickCountToStart(1);
}
public Component getTableCellEditorComponent(JTable table,
Object value, boolean isSelected, int row, int column) {
tf.setText(value.toString());
return tf;
}
public Object getCellEditorValue() {
return tf.getText();
}
}
class TblModel extends DefaultTableModel {
private List data;
private String[] header;
public TblModel(List data, String[] header) {
this.data = data;
this.header = header;
}
public boolean isCellEditable(int rowIndex, int columnIndex) {
return columnIndex == 1;
}
public int getColumnCount() {
return header == null ? 0 : header.length;
}
public int getRowCount() {
return data == null ? 0 : data.size();
}
public Object getValueAt(int rowIndex, int columnIndex) {
Object[] row = (Object[]) data.get(rowIndex);
return row[columnIndex];
}
public Class getColumnClass(int columnIndex) {
if (data == null) {
return Object.class;
}
Object[] row = (Object[]) data.get(0);
return row[columnIndex] == null ? Object.class :
row[columnIndex].getClass();
}
public String getColumnName(int column) {
return header[column];
}
public void setValueAt(Object aValue, int rowIndex, int columnIndex) {
Object[] row = (Object[]) data.get(rowIndex);
row[columnIndex] = aValue;
super.fireTableCellUpdated(rowIndex, columnIndex);
}
public void addRow(Object[] data){
super.addRow(data);
javax.swing.SwingUtilities.invokeLater(new Runnable(){
@Override
public void run(){
fireTableRowsInserted(0,0);
}
});
}
}
private class MyKeyListener implements KeyListener {
public void keyPressed(KeyEvent e) {int key = e.getKeyCode();
if (key == KeyEvent.VK_ENTER) {
Toolkit.getDefaultToolkit().beep();
System.out.println("ENTER pressed");
// THIS line cause the exception
model_001.addRow(new Object[] {"","",""});
//
}
}
public void keyReleased(KeyEvent e) {
}
public void keyTyped(KeyEvent e) {
if (!Character.isDigit(e.getKeyChar())) {
Toolkit.getDefaultToolkit().beep();
e.consume();
}
}
}
public static void main(String[] args) {
JFrame frame = new NumericColumnTester();
frame.setDefaultCloseOperation(EXIT_ON_CLOSE);
frame.pack();
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
}