【发布时间】:2014-01-23 22:33:40
【问题描述】:
是否有将String[] 转换为Object[] 的解决方法?使用它时我不断收到错误消息。
我正在尝试插入jTable,它给了我一个错误,说我正在尝试将类型String 转换为类型Object...
我在这里获取日期并将其插入到对象月份中。
// Create calendar and format to first sunday of the year.
Calendar c;
Object[] months = new String[52];
c = Calendar.getInstance();
c.set(Calendar.MONTH,0);
c.set(Calendar.DATE, 5);
// Format Date and insert into months object array
DateFormat df = new SimpleDateFormat("MM/dd/yyy");
for (int i =0; i < 52; i++){
months[i] = df.format(c.getTime());
c.add(Calendar.DATE, 7);
}
//Insert Months array into jComboBox
jComboBox1.setModel(new DefaultComboBoxModel(months));
...
...
//Action performed retrieves selection from jComboBox and insert into table
private void jComboBox1ActionPerformed(java.awt.event.ActionEvent evt) {
// object[] o gets selection from selectedItem()
Object[] o = (Object[]) jComboBox1.getSelectedItem();
//error checking to println
System.out.println(jComboBox1.getSelectedItem() + " ");
// create DefaultTableModel and insert into row zero with the object selected
DefaultTableModel model = (DefaultTableModel) weeklyCalendar.getModel();
//insert into row, throws error
model.insertRow(0, o);
}
我想我从getSelectedItem() 得到一个字符串并尝试将其转换为Object[] 并抛出错误异常......我能做些什么来解决这个问题?
【问题讨论】:
-
答案贴在这里stackoverflow.com/questions/1018750/…只要做反转
-
getSelectedItem返回Object,而不是String[]。 -
是的,对于该行,它根本不会返回一个数组。你到底想用数组做什么?
-
显然它没有返回一个对象。告诉我的字符串
-
感谢大家的快速回复!
标签: java jtable jcombobox japplet