Hello all,
I have a JTable to display data from MySQL table.
The MySQL table is defined as follows:
------------------------------------------------------------------------
CREATE TABLE `tb_extuser` (
`id` MEDIUMINT NOT NULL AUTO_INCREMENT,
`english_name` varchar(50) default NULL,
`cn_name` varchar(50) default NULL,
`location` varchar(50) default NULL,
`ext` varchar(50) NOT NULL,
`department` varchar(255) default NULL,
`day` varchar(255) default NULL,
`night` varchar(255) default NULL,
PRIMARY KEY (`id`)
)ENGINE=InnoDB DEFAULT CHARSET=gbk;
I use hibernate to access tb_extuser, the POJO for this table is Extuser class, as follows:
---------------------------------------------------------------------
package bill.hibernate.mapping;
// default package
/**
* Extuser entity. @author MyEclipse Persistence Tools
*/
public class Extuser implements java.io.Serializable {
// Fields
private Integer id;
private String englishName;
private String cnName;
private String location;
private String ext;
private String department;
private String day;
private String night;
// Constructors
/** default constructor */
public Extuser() {
}
/** minimal constructor */
public Extuser(String ext) {
this.ext = ext;
}
/** full constructor */
public Extuser(String englishName, String cnName, String location, String ext, String department, String day, String night) {
this.englishName = englishName;
this.cnName = cnName;
this.location = location;
this.ext = ext;
this.department = department;
this.day = day;
this.night = night;
}
// Property accessors
public Integer getId() {
return this.id;
}
public void setId(Integer id) {
this.id = id;
}
public String getEnglishName() {
return this.englishName;
}
public void setEnglishName(String englishName) {
this.englishName = englishName;
}
public String getCnName() {
return this.cnName;
}
public void setCnName(String cnName) {
this.cnName = cnName;
}
public String getLocation() {
return this.location;
}
public void setLocation(String location) {
this.location = location;
}
public String getExt() {
return this.ext;
}
public void setExt(String ext) {
this.ext = ext;
}
public String getDepartment() {
return this.department;
}
public void setDepartment(String department) {
this.department = department;
}
public String getDay() {
return this.day;
}
public void setDay(String day) {
this.day = day;
}
public String getNight() {
return this.night;
}
public void setNight(String night) {
this.night = night;
}
}
The mapping file is following:
--------------------------------
<?xml version="1.0" encoding="utf-8"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<!--
Mapping file autogenerated by MyEclipse Persistence Tools
-->
<hibernate-mapping>
<class name="bill.hibernate.mapping.Extuser" table="tb_extuser" catalog="bill">
<id name="id" type="java.lang.Integer">
<column name="id" />
<generator class="increment"></generator>
</id>
<property name="englishName" type="java.lang.String">
<column name="english_name" length="50" />
</property>
<property name="cnName" type="java.lang.String">
<column name="cn_name" length="50" />
</property>
<property name="location" type="java.lang.String">
<column name="location" length="50" />
</property>
<property name="ext" type="java.lang.String">
<column name="ext" length="50" not-null="true" />
</property>
<property name="department" type="java.lang.String">
<column name="department" />
</property>
<property name="day" type="java.lang.String">
<column name="day" />
</property>
<property name="night" type="java.lang.String">
<column name="night" />
</property>
</class>
</hibernate-mapping>
I write a ExtuserTableModel for the JTable:
----------------------------------------------------------------
import java.util.Vector;
import javax.swing.table.AbstractTableModel;
import bill.hibernate.mapping.*;
public abstract class BaseTableModel extends AbstractTableModel
{
private String[] columnNames ;
protected Vector<Object> dataVector;
public BaseTableModel()
{
dataVector = new Vector<Object>();
}
public void setColumnNames(String[] columnNames)
{
this.columnNames = columnNames;
}
public void setData(Vector<Object> dataVector)
{
this.dataVector = dataVector;
}
public Vector<Object> getData()
{
return this.dataVector;
}
public int getColumnCount()
{
return columnNames.length;
}
public String getColumnName(int column)
{
return columnNames[column];
}
public int getRowCount()
{
return dataVector.size();
}
public abstract Class getColumnClass(int column);
public abstract Object getValueAt(int row, int column);
public abstract void setValueAt(Object value, int row, int column);
public abstract void insertEmptyRowAt(int rowIndex);
public void deleteAll()
{
dataVector.removeAllElements();
this.fireTableDataChanged();
}
public void deleteRowAt(int rowIndex)
{
dataVector.removeElementAt(rowIndex);
this.fireTableRowsDeleted(rowIndex, rowIndex);
}
public boolean isCellEditable(int row, int column)
{
return true;
}
}
-------------------------------------------
package bill.ui.util;
import java.util.Vector;
import javax.swing.table.AbstractTableModel;
import bill.hibernate.mapping.*;
public class ExtuserTableModel extends BaseTableModel
{
public static final int ID_INDEX = 0;
public static final int ENGLISHNAME_INDEX = 1;
public static final int CNNAME_INDEX = 2;
public static final int LOCATIOM_INDEX = 3;
public static final int EXT_INDEX = 4;
public static final int DEPARTMENT_INDEX = 5;
public static final int DAY_INDEX = 6;
public static final int NIGHT_INDEX = 7;
private String[] columnNames = { "序号", "英文名", "中文名","办公地点","分机号码","部门","白天","晚上" };
public ExtuserTableModel()
{
super();
super.setColumnNames(columnNames);
}
public Class getColumnClass(int column)
{
// return getValueAt(0, column).getClass();
switch (column)
{
case ID_INDEX:
return Integer.class;
case ENGLISHNAME_INDEX:
case CNNAME_INDEX:
case LOCATIOM_INDEX:
case EXT_INDEX:
case DEPARTMENT_INDEX:
case DAY_INDEX:
case NIGHT_INDEX:
return String.class;
default:
return Object.class;
}
}
public Object getValueAt(int row, int column)
{
Extuser extuser = (Extuser) dataVector.get(row);
switch (column)
{
case ID_INDEX:
return extuser.getId();
case ENGLISHNAME_INDEX:
return extuser.getEnglishName();
case CNNAME_INDEX:
return extuser.getCnName();
case LOCATIOM_INDEX:
return extuser.getLocation();
case EXT_INDEX:
return extuser.getExt();
case DEPARTMENT_INDEX:
return extuser.getDepartment();
case DAY_INDEX:
return extuser.getDay();
case NIGHT_INDEX:
return extuser.getNight();
default:
return new Object();
}
}
public void setValueAt(Object value, int row, int column)
{
Extuser extuser = (Extuser) dataVector.get(row);
switch (column)
{
case ID_INDEX:
extuser.setId( (Integer) value);
case ENGLISHNAME_INDEX:
extuser.setEnglishName((String) value);
case CNNAME_INDEX:
extuser.setCnName((String) value);
case LOCATIOM_INDEX:
extuser.setLocation((String) value);
case EXT_INDEX:
extuser.setExt((String) value);
case DEPARTMENT_INDEX:
extuser.setDepartment((String) value);
case DAY_INDEX:
extuser.setDay((String) value);
case NIGHT_INDEX:
extuser.setNight((String) value);
default:
System.out.println("invalid index");
}
fireTableCellUpdated(row, column);
}
public void insertEmptyRowAt(int rowIndex)
{
dataVector.insertElementAt(new Extuser(), rowIndex);
fireTableRowsInserted(dataVector.size() - 1, dataVector.size() - 1);
}
}
********************************************************8
The ExtuserTableModel holds a dataVector of Extuser object, whose id
is an Integer, the other fields are String. After I clicked a cell of Jtable's first column ( whose data type is Integer) I got the following error, which indicate this statement have problem of "java.lang.Integer cannot be cast to java.lang.String": extuser.setEnglishName((String) value);
Anybody have any idea to get rid of this?
********************************************************
Exception in thread "AWT-EventQueue-0" java.lang.ClassCastException: java.lang.Integer cannot be cast to java.lang.String
at bill.ui.util.ExtuserTableModel.setValueAt(ExtuserTableModel.java:86)
at javax.swing.JTable.setValueAt(JTable.java:2676)
at javax.swing.JTable.editingStopped(JTable.java:4675)
at javax.swing.AbstractCellEditor.fireEditingStopped(AbstractCellEditor.java:125)
at javax.swing.DefaultCellEditor$EditorDelegate.stopCellEditing(DefaultCellEditor.java:330)
at javax.swing.DefaultCellEditor.stopCellEditing(DefaultCellEditor.java:215)
at javax.swing.JTable$GenericEditor.stopCellEditing(JTable.java:5429)
at javax.swing.plaf.basic.BasicTableUI$Handler.mousePressed(BasicTableUI.java:986)
at java.awt.AWTEventMulticaster.mousePressed(AWTEventMulticaster.java:263)
at java.awt.Component.processMouseEvent(Component.java:6035)
at javax.swing.JComponent.processMouseEvent(JComponent.java:3260)
at java.awt.Component.processEvent(Component.java:5803)
at java.awt.Container.processEvent(Container.java:2058)
at java.awt.Component.dispatchEventImpl(Component.java:4410)
at java.awt.Container.dispatchEventImpl(Container.java:2116)
at java.awt.Component.dispatchEvent(Component.java:4240)
at java.awt.LightweightDispatcher.retargetMouseEvent(Container.java:4322)
at java.awt.LightweightDispatcher.processMouseEvent(Container.java:3983)
at java.awt.LightweightDispatcher.dispatchEvent(Container.java:3916)
at java.awt.Container.dispatchEventImpl(Container.java:2102)
at java.awt.Window.dispatchEventImpl(Window.java:2429)
at java.awt.Component.dispatchEvent(Component.java:4240)
at java.awt.EventQueue.dispatchEvent(EventQueue.java:599)
at java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:273)
at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:183)
at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:173)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:168)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:160)
at java.awt.EventDispatchThread.run(EventDispatchThread.java:121)
|