报错信息如下,其中StandardTableModel是自己写的一个类。
public class StandardTableModel extends AbstractTableModel {
/**
* each element of this vector represents a single row of the table. Internally, a row is represented
* by a Object[].
*/
protected Vector<Object[]> rows = new Vector<Object[]>();
/**
* array that holds information on the names for the table columns
*/
protected String[] colNames = null; /**
* 设置表格列是否允许编辑
*/
protected boolean[] editables = null;
public StandardTableModel(String[] colNames, boolean[] editables) {
super();
this.colNames = colNames;
this.editables = editables;
} public int getColumnCount() {
return colNames.length;
} public int getRowCount() {
return rows.size();
} public String getColumnName(int column) {
return this.colNames[column];
}
/**
* deletes all data from the table (also imforms the GUI)
*/
public void clearTable(){
int numRows = this.getRowCount();
this.rows.clear();
if (numRows >= 1)
this.fireTableRowsDeleted(0, numRows - 1);
}
/**
* @param columnName name of column to get the index for
* @return the index of the column with the given name
*/
public int findColumn(String columnName) {
for ( int i=0; i<this.colNames.length; i++ ){
if (this.colNames[i].toLowerCase().equals(columnName.toLowerCase())) return i;
}
return -1;
}
public void setValueAt(Object aValue, int rowIndex, int columnIndex) {
(this.rows.get(rowIndex))[columnIndex] = aValue;
this.fireTableCellUpdated(rowIndex,columnIndex);
}
public Object getValueAt(int rowIndex, int columnIndex) {
return (this.rows.get(rowIndex))[columnIndex];
} public boolean isCellEditable(int rowIndex, int columnIndex) {
return editables[columnIndex];
}
public Class<?> getColumnClass(int columnIndex) {
return getValueAt(0, columnIndex).getClass();
}
/**
* simple method to add a row to the table.
* (It better matches the given column nummer and column types!!)
*@param newRow
*/
public void addRow(Object[] newRow){
this.rows.add(newRow);
this.fireTableRowsInserted(this.getRowCount()-1,this.getRowCount()-1);
}
/**
*@return array containing the names of the columns
*/
public String[] getColNames() {
return colNames;
}
/**
* Setting new column names will flush the table, if the new array has not the same length as the old one!
*@param colNames array containing new column names
*/
protected void setColNames(String[] colNames) {
if (this.colNames.length != colNames.length)
this.clearTable();
this.colNames = colNames;
}
}
Exception in thread "AWT-EventQueue-0" java.lang.ArrayIndexOutOfBoundsException: Array index out of range: 0
at java.util.Vector.get(Unknown Source)
at com.spdb.swing.table.StandardTableModel.getValueAt(StandardTableModel.java:90)
at com.spdb.swing.table.StandardTableModel.getColumnClass(StandardTableModel.java:98)
at javax.swing.JTable.getColumnClass(Unknown Source)
at org.jvnet.substance.SubstanceTableUI.__org__jvnet__substance__SubstanceTableUI__installDefaults(SubstanceTableUI.java:242)
at org.jvnet.substance.SubstanceTableUI.installDefaults(SubstanceTableUI.java)
at javax.swing.plaf.basic.BasicTableUI.installUI(Unknown Source)
at org.jvnet.substance.SubstanceTableUI.__org__jvnet__substance__SubstanceTableUI__installUI(SubstanceTableUI.java)
at org.jvnet.substance.SubstanceTableUI.installUI(SubstanceTableUI.java)
at javax.swing.JComponent.setUI(Unknown Source)
at javax.swing.JTable.setUI(Unknown Source)
at javax.swing.JTable.updateUI(Unknown Source)
at javax.swing.JTable.<init>(Unknown Source)
at javax.swing.JTable.<init>(Unknown Source)
at yxkh.swing.PZhwjibSettingPanel.initGUI(PZhwjibSettingPanel.java:126)
at yxkh.swing.PZhwjibSettingPanel.<init>(PZhwjibSettingPanel.java:48)
at yxkh.swing.MainFrame.actionPerformed(MainFrame.java:783)
at javax.swing.AbstractButton.fireActionPerformed(Unknown Source)
at javax.swing.AbstractButton$Handler.actionPerformed(Unknown Source)
at javax.swing.DefaultButtonModel.fireActionPerformed(Unknown Source)
at javax.swing.DefaultButtonModel.setPressed(Unknown Source)
at javax.swing.AbstractButton.doClick(Unknown Source)
at javax.swing.plaf.basic.BasicMenuItemUI.doClick(Unknown Source)
at javax.swing.plaf.basic.BasicMenuItemUI$Handler.mouseReleased(Unknown Source)
at java.awt.Component.processMouseEvent(Unknown Source)
at javax.swing.JComponent.processMouseEvent(Unknown Source)
at java.awt.Component.processEvent(Unknown Source)
at java.awt.Container.processEvent(Unknown Source)
at java.awt.Component.dispatchEventImpl(Unknown Source)
at java.awt.Container.dispatchEventImpl(Unknown Source)
at java.awt.Component.dispatchEvent(Unknown Source)
at java.awt.LightweightDispatcher.retargetMouseEvent(Unknown Source)
at java.awt.LightweightDispatcher.processMouseEvent(Unknown Source)
at java.awt.LightweightDispatcher.dispatchEvent(Unknown Source)
at java.awt.Container.dispatchEventImpl(Unknown Source)
at java.awt.Window.dispatchEventImpl(Unknown Source)
at java.awt.Component.dispatchEvent(Unknown Source)
at java.awt.EventQueue.dispatchEvent(Unknown Source)
at java.awt.EventDispatchThread.pumpOneEventForFilters(Unknown Source)
at java.awt.EventDispatchThread.pumpEventsForFilter(Unknown Source)
at java.awt.EventDispatchThread.pumpEventsForHierarchy(Unknown Source)
at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
at java.awt.EventDispatchThread.run(Unknown Source)具体调用代码如下
String[] columnNames = { "col1", "col2", "col3", "col4", "val1", "val2" };
boolean[] editables = { false, false, false, true, true, true};
model = new StandardTableModel(columnNames, editables);
//此时model中为空,程序报错。
//如果我先add一条记录,再创建table,不会报错。难道substance初始化table的时候必须要有记录?
//哪位朋友指点一下。
table = new JTable(model);
public class StandardTableModel extends AbstractTableModel {
/**
* each element of this vector represents a single row of the table. Internally, a row is represented
* by a Object[].
*/
protected Vector<Object[]> rows = new Vector<Object[]>();
/**
* array that holds information on the names for the table columns
*/
protected String[] colNames = null; /**
* 设置表格列是否允许编辑
*/
protected boolean[] editables = null;
public StandardTableModel(String[] colNames, boolean[] editables) {
super();
this.colNames = colNames;
this.editables = editables;
} public int getColumnCount() {
return colNames.length;
} public int getRowCount() {
return rows.size();
} public String getColumnName(int column) {
return this.colNames[column];
}
/**
* deletes all data from the table (also imforms the GUI)
*/
public void clearTable(){
int numRows = this.getRowCount();
this.rows.clear();
if (numRows >= 1)
this.fireTableRowsDeleted(0, numRows - 1);
}
/**
* @param columnName name of column to get the index for
* @return the index of the column with the given name
*/
public int findColumn(String columnName) {
for ( int i=0; i<this.colNames.length; i++ ){
if (this.colNames[i].toLowerCase().equals(columnName.toLowerCase())) return i;
}
return -1;
}
public void setValueAt(Object aValue, int rowIndex, int columnIndex) {
(this.rows.get(rowIndex))[columnIndex] = aValue;
this.fireTableCellUpdated(rowIndex,columnIndex);
}
public Object getValueAt(int rowIndex, int columnIndex) {
return (this.rows.get(rowIndex))[columnIndex];
} public boolean isCellEditable(int rowIndex, int columnIndex) {
return editables[columnIndex];
}
public Class<?> getColumnClass(int columnIndex) {
return getValueAt(0, columnIndex).getClass();
}
/**
* simple method to add a row to the table.
* (It better matches the given column nummer and column types!!)
*@param newRow
*/
public void addRow(Object[] newRow){
this.rows.add(newRow);
this.fireTableRowsInserted(this.getRowCount()-1,this.getRowCount()-1);
}
/**
*@return array containing the names of the columns
*/
public String[] getColNames() {
return colNames;
}
/**
* Setting new column names will flush the table, if the new array has not the same length as the old one!
*@param colNames array containing new column names
*/
protected void setColNames(String[] colNames) {
if (this.colNames.length != colNames.length)
this.clearTable();
this.colNames = colNames;
}
}
Exception in thread "AWT-EventQueue-0" java.lang.ArrayIndexOutOfBoundsException: Array index out of range: 0
at java.util.Vector.get(Unknown Source)
at com.spdb.swing.table.StandardTableModel.getValueAt(StandardTableModel.java:90)
at com.spdb.swing.table.StandardTableModel.getColumnClass(StandardTableModel.java:98)
at javax.swing.JTable.getColumnClass(Unknown Source)
at org.jvnet.substance.SubstanceTableUI.__org__jvnet__substance__SubstanceTableUI__installDefaults(SubstanceTableUI.java:242)
at org.jvnet.substance.SubstanceTableUI.installDefaults(SubstanceTableUI.java)
at javax.swing.plaf.basic.BasicTableUI.installUI(Unknown Source)
at org.jvnet.substance.SubstanceTableUI.__org__jvnet__substance__SubstanceTableUI__installUI(SubstanceTableUI.java)
at org.jvnet.substance.SubstanceTableUI.installUI(SubstanceTableUI.java)
at javax.swing.JComponent.setUI(Unknown Source)
at javax.swing.JTable.setUI(Unknown Source)
at javax.swing.JTable.updateUI(Unknown Source)
at javax.swing.JTable.<init>(Unknown Source)
at javax.swing.JTable.<init>(Unknown Source)
at yxkh.swing.PZhwjibSettingPanel.initGUI(PZhwjibSettingPanel.java:126)
at yxkh.swing.PZhwjibSettingPanel.<init>(PZhwjibSettingPanel.java:48)
at yxkh.swing.MainFrame.actionPerformed(MainFrame.java:783)
at javax.swing.AbstractButton.fireActionPerformed(Unknown Source)
at javax.swing.AbstractButton$Handler.actionPerformed(Unknown Source)
at javax.swing.DefaultButtonModel.fireActionPerformed(Unknown Source)
at javax.swing.DefaultButtonModel.setPressed(Unknown Source)
at javax.swing.AbstractButton.doClick(Unknown Source)
at javax.swing.plaf.basic.BasicMenuItemUI.doClick(Unknown Source)
at javax.swing.plaf.basic.BasicMenuItemUI$Handler.mouseReleased(Unknown Source)
at java.awt.Component.processMouseEvent(Unknown Source)
at javax.swing.JComponent.processMouseEvent(Unknown Source)
at java.awt.Component.processEvent(Unknown Source)
at java.awt.Container.processEvent(Unknown Source)
at java.awt.Component.dispatchEventImpl(Unknown Source)
at java.awt.Container.dispatchEventImpl(Unknown Source)
at java.awt.Component.dispatchEvent(Unknown Source)
at java.awt.LightweightDispatcher.retargetMouseEvent(Unknown Source)
at java.awt.LightweightDispatcher.processMouseEvent(Unknown Source)
at java.awt.LightweightDispatcher.dispatchEvent(Unknown Source)
at java.awt.Container.dispatchEventImpl(Unknown Source)
at java.awt.Window.dispatchEventImpl(Unknown Source)
at java.awt.Component.dispatchEvent(Unknown Source)
at java.awt.EventQueue.dispatchEvent(Unknown Source)
at java.awt.EventDispatchThread.pumpOneEventForFilters(Unknown Source)
at java.awt.EventDispatchThread.pumpEventsForFilter(Unknown Source)
at java.awt.EventDispatchThread.pumpEventsForHierarchy(Unknown Source)
at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
at java.awt.EventDispatchThread.run(Unknown Source)具体调用代码如下
String[] columnNames = { "col1", "col2", "col3", "col4", "val1", "val2" };
boolean[] editables = { false, false, false, true, true, true};
model = new StandardTableModel(columnNames, editables);
//此时model中为空,程序报错。
//如果我先add一条记录,再创建table,不会报错。难道substance初始化table的时候必须要有记录?
//哪位朋友指点一下。
table = new JTable(model);
解决方案 »
- 救急啊!!!!!!!!!!!!!!!!!!!!
- 征集人民币大小写转换思路(要求在万亿级别)
- 麻烦各位.帮个忙看个题目.关于圆形角度的问题
- 我把一个源码的图片替换成和原图不一样尺寸的后,不显示图片啦?
- ActionListener和ItemListener的问题
- Hello World运行不了!!!!!!!!!!!!!!!!!!!!!
- IOException异常出发后是否有固定的错误代码?
- 一个JAVA的问题,请各位大虾帮忙!
- 一个问题,如果没人回答,自己最后知道答案了,怎么结掉?
- 为什么会出现这样的Exception?(Error occurred during initialization of VM;java/lang/ClassNotFoundException: sun/io/ByteToCharGBK)
- 关于jar包中配置文件相互引用的问题,高手进~~
- 在JTextField中怎么 显示当前系统时间呢 ?有什么函数?
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.table.AbstractTableModel;public class StandardTableModel extends AbstractTableModel {
/**
* each element of this vector represents a single row of the table.
* Internally, a row is represented by a Object[].
*/
protected Vector<Object[]> rows = new Vector<Object[]>(); /**
* array that holds information on the names for the table columns
*/
protected String[] colNames = null; /**
*
*/
protected boolean[] editables = null; public StandardTableModel(String[] colNames, boolean[] editables) {
super();
this.colNames = colNames;
this.editables = editables;
} public int getColumnCount() {
return colNames.length;
} public int getRowCount() {
return rows.size();
} public String getColumnName(int column) {
return this.colNames[column];
} /**
* deletes all data from the table (also imforms the GUI)
*/
public void clearTable() {
int numRows = this.getRowCount();
this.rows.clear();
if (numRows >= 1) this.fireTableRowsDeleted(0, numRows - 1);
} /**
* @param columnName
* name of column to get the index for
* @return the index of the column with the given name
*/
public int findColumn(String columnName) {
for (int i = 0; i < this.colNames.length; i++) {
if (this.colNames[i].toLowerCase().equals(columnName.toLowerCase())) return i;
}
return -1;
} public void setValueAt(Object aValue, int rowIndex, int columnIndex) {
(this.rows.get(rowIndex))[columnIndex] = aValue;
this.fireTableCellUpdated(rowIndex, columnIndex);
} public Object getValueAt(int rowIndex, int columnIndex) {
return (this.rows.get(rowIndex))[columnIndex];
} public boolean isCellEditable(int rowIndex, int columnIndex) {
return editables[columnIndex];
} public Class< ? > getColumnClass(int columnIndex) {
return getValueAt(0, columnIndex).getClass();
} /**
* simple method to add a row to the table. (It better matches the given
* column nummer and column types!!)
*
* @param newRow
*/
public void addRow(Object[] newRow) {
this.rows.add(newRow);
this.fireTableRowsInserted(this.getRowCount() - 1, this.getRowCount() - 1);
} /**
* @return array containing the names of the columns
*/
public String[] getColNames() {
return colNames;
} /**
* Setting new column names will flush the table, if the new array has not
* the same length as the old one!
*
* @param colNames
* array containing new column names
*/
protected void setColNames(String[] colNames) {
if (this.colNames.length != colNames.length) this.clearTable();
this.colNames = colNames;
} public static void main(String[] args) {
String[] columnNames = {"col1", "col2", "col3", "col4", "val1", "val2" };
boolean[] editables = {false, false, false, true, true, true };
StandardTableModel model = new StandardTableModel(columnNames, editables);
JTable table = new JTable(model);
JScrollPane scrollPanel = new JScrollPane(table);
JFrame frame = new JFrame();
frame.getContentPane().add(scrollPanel);
frame.setVisible(true);
frame.setSize(200, 200);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
}没报错阿 没明白你说的意思
这个?
--------------------------------------------------------------
import java.util.Vector;import javax.swing.JFrame;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.SwingUtilities;
import javax.swing.UIManager;
import javax.swing.table.AbstractTableModel;public class StandardTableModel extends AbstractTableModel {
/**
* each element of this vector represents a single row of the table.
* Internally, a row is represented by a Object[].
*/
protected Vector<Object[]> rows = new Vector<Object[]>(); /**
* array that holds information on the names for the table columns
*/
protected String[] colNames = null; /**
*
*/
protected boolean[] editables = null; public StandardTableModel(String[] colNames, boolean[] editables) {
super();
this.colNames = colNames;
this.editables = editables;
} public int getColumnCount() {
return colNames.length;
} public int getRowCount() {
return rows.size();
} public String getColumnName(int column) {
return this.colNames[column];
} /**
* deletes all data from the table (also imforms the GUI)
*/
public void clearTable() {
int numRows = this.getRowCount();
this.rows.clear();
if (numRows >= 1) this.fireTableRowsDeleted(0, numRows - 1);
} /**
* @param columnName
* name of column to get the index for
* @return the index of the column with the given name
*/
public int findColumn(String columnName) {
for (int i = 0; i < this.colNames.length; i++) {
if (this.colNames[i].toLowerCase().equals(columnName.toLowerCase())) return i;
}
return -1;
} public void setValueAt(Object aValue, int rowIndex, int columnIndex) {
(this.rows.get(rowIndex))[columnIndex] = aValue;
this.fireTableCellUpdated(rowIndex, columnIndex);
} public Object getValueAt(int rowIndex, int columnIndex) {
try {
return (this.rows.get(rowIndex))[columnIndex];
} catch (Exception e) {
return null;
}
} public boolean isCellEditable(int rowIndex, int columnIndex) {
return editables[columnIndex];
} public Class< ? > getColumnClass(int columnIndex) {
try {
return getValueAt(0, columnIndex).getClass();
} catch (Exception e) {
return String.class;
}
} /**
* simple method to add a row to the table. (It better matches the given
* column nummer and column types!!)
*
* @param newRow
*/
public void addRow(Object[] newRow) {
this.rows.add(newRow);
this.fireTableRowsInserted(this.getRowCount() - 1, this.getRowCount() - 1);
} /**
* @return array containing the names of the columns
*/
public String[] getColNames() {
return colNames;
} /**
* Setting new column names will flush the table, if the new array has not
* the same length as the old one!
*
* @param colNames
* array containing new column names
*/
protected void setColNames(String[] colNames) {
if (this.colNames.length != colNames.length) this.clearTable();
this.colNames = colNames;
} public static void main(String[] args) {
// UIManager.setLookAndFeel(new SubstanceLookAndFeel());
JFrame.setDefaultLookAndFeelDecorated(true);
try {
UIManager.setLookAndFeel("org.jvnet.substance.skin.SubstanceRavenGraphiteLookAndFeel");
} catch (Exception e) {
System.out.println("Substance Raven Graphite failed to initialize");
} SwingUtilities.invokeLater(new Runnable() {
public void run() {
// JFrame w = new JFrame();
String[] columnNames = {"col1", "col2", "col3", "col4", "val1", "val2" };
boolean[] editables = {false, false, false, true, true, true };
StandardTableModel model = new StandardTableModel(columnNames, editables);
JTable table = new JTable(model);
JScrollPane scrollPanel = new JScrollPane(table);
JFrame frame = new JFrame();
frame.getContentPane().add(scrollPanel);
frame.setVisible(true);
frame.setSize(200, 200);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
// w.setVisible(true);
}
}); }
}
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;
import javax.swing.table.AbstractTableModel;import org.jvnet.substance.SubstanceLookAndFeel;public class StandardTableModel extends AbstractTableModel {
/**
* each element of this vector represents a single row of the table.
* Internally, a row is represented by a Object[].
*/
protected Vector<Object[]> rows = new Vector<Object[]>(); /**
* array that holds information on the names for the table columns
*/
protected String[] colNames = null; /**
*
*/
protected boolean[] editables = null; public StandardTableModel(String[] colNames, boolean[] editables) {
super();
this.colNames = colNames;
this.editables = editables;
} public int getColumnCount() {
return colNames.length;
} public int getRowCount() {
return rows.size();
} public String getColumnName(int column) {
return this.colNames[column];
} /**
* deletes all data from the table (also imforms the GUI)
*/
public void clearTable() {
int numRows = this.getRowCount();
this.rows.clear();
if (numRows >= 1) this.fireTableRowsDeleted(0, numRows - 1);
} /**
* @param columnName
* name of column to get the index for
* @return the index of the column with the given name
*/
public int findColumn(String columnName) {
for (int i = 0; i < this.colNames.length; i++) {
if (this.colNames[i].toLowerCase().equals(columnName.toLowerCase())) return i;
}
return -1;
} public void setValueAt(Object aValue, int rowIndex, int columnIndex) {
(this.rows.get(rowIndex))[columnIndex] = aValue;
this.fireTableCellUpdated(rowIndex, columnIndex);
} public Object getValueAt(int rowIndex, int columnIndex) {
return (this.rows.get(rowIndex))[columnIndex];
} public boolean isCellEditable(int rowIndex, int columnIndex) {
return editables[columnIndex];
} public Class< ? > getColumnClass(int columnIndex) {
try {
return getValueAt(0, columnIndex).getClass();
} catch (Exception e) {
return String.class;
}
} /**
* simple method to add a row to the table. (It better matches the given
* column nummer and column types!!)
*
* @param newRow
*/
public void addRow(Object[] newRow) {
this.rows.add(newRow);
this.fireTableRowsInserted(this.getRowCount() - 1, this.getRowCount() - 1);
} /**
* @return array containing the names of the columns
*/
public String[] getColNames() {
return colNames;
} /**
* Setting new column names will flush the table, if the new array has not
* the same length as the old one!
*
* @param colNames
* array containing new column names
*/
protected void setColNames(String[] colNames) {
if (this.colNames.length != colNames.length) this.clearTable();
this.colNames = colNames;
} public static void main(String[] args) {
try {
UIManager.setLookAndFeel(new SubstanceLookAndFeel());
} catch (UnsupportedLookAndFeelException e) {
e.printStackTrace();
}
String[] columnNames = {"col1", "col2", "col3", "col4", "val1", "val2" };
boolean[] editables = {false, false, false, true, true, true };
StandardTableModel model = new StandardTableModel(columnNames, editables);
JTable table = new JTable(model);
JScrollPane scrollPanel = new JScrollPane(table);
JFrame frame = new JFrame();
frame.getContentPane().add(scrollPanel);
frame.setVisible(true);
frame.setSize(200, 200);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
}