返回信息流运行后错误如下:
java.lang.ClassCastException: javax.swing.JTable$1 cannot be cast to javax.swing.table.DefaultTableModel at mhs.RSS.actionPerformed(RSS.java:90)
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.plaf.basic.BasicButtonListener.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)
程序代码如下:
import javax.swing.*;
import javax.swing.event.*;
import javax.swing.table.*;
import java.util.*;
import java.awt.*;
import java.awt.event.*;
import java.net.URL;
import java.util.List;
import java.util.*;
import java.text.SimpleDateFormat;
import com.sun.syndication.feed.synd.SyndCategory;
import com.sun.syndication.feed.synd.SyndContent;
import com.sun.syndication.feed.synd.SyndEnclosure;
import com.sun.syndication.feed.synd.SyndEntry;
import com.sun.syndication.feed.synd.SyndFeed;
import com.sun.syndication.io.SyndFeedInput;
import com.sun.syndication.io.XmlReader;
public class RSS extends Frame implements ActionListener
{
JLabel RSS = new JLabel("RSS:");
JTextField URL = new JTextField();
JButton JB= new JButton("确认");
Object[][]tablevalue = new Object[20][4];
String []name={"标题","作者","时间","摘要"};
JTable table = new JTable(tablevalue,name);
public RSS()
{
this.setTitle("欢迎使用RSS阅读器");
RSS.setBounds(100,570,60,60);
this.add(RSS);
URL.setBounds(170,570,400,50);
this.add(URL);
JB.setBounds(620,570,70,50);
this.add(JB);
JB.addActionListener(this);
table.setBounds(40,150,720,500);
TableColumn Column0 = table.getColumnModel().getColumn(0);
Column0.setPreferredWidth(80);
TableColumn Column1 = table.getColumnModel().getColumn(1);
Column1.setPreferredWidth(60);
TableColumn Column2 = table.getColumnModel().getColumn(2);
Column2.setPreferredWidth(60);
TableColumn Column3 = table.getColumnModel().getColumn(3);
Column3.setPreferredWidth(400);
table.setRowHeight(25);
this.add(new JScrollPane(table));
this.setBounds(100,200,800,700);
this.setVisible(true);
addWindowListener(new WindowAdapter()
{public void windowClosing(WindowEvent e)
{
System.exit(0);
}
});
}
public static void main(String args[])
{
new RSS();
}
@Override
public void actionPerformed(ActionEvent e)
{
// TODO Auto-generated method stub
if(e.getSource()==JB)
{
try {
//URL url = new URL(RSS.getText());
URL.setText("http://rss.sina.com.cn/ent/hot_roll.xml");
URL url = new URL(URL.getText());
XmlReader reader = new XmlReader(url);
SyndFeedInput input = new SyndFeedInput();
DefaultTableModel model = (DefaultTableModel)table.getModel(); //就是这句出问题了,大牛,怎么改啊 SyndFeed feed = input.build(reader);
List entries = feed.getEntries();
for (int i = 0; i < entries.size(); i++)
{ String[] arr=new String[4];
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
SyndEntry entry = (SyndEntry) entries.get(i);
arr[0]=entry.getTitle();
arr[1]=entry.getAuthor();
arr[2]=sdf.format(entry.getPublishedDate());
SyndContent description = entry.getDescription();
arr[3]=description.getValue();
model.addRow(arr);
}
table.invalidate();
}
catch (Exception error)
{
error.printStackTrace();
}
}
}
}
这是一条镜像帖。来源:北邮人论坛 / java / #13187同步于 2010/2/3
该镜像源已超过 30 天没有更新,可能在源站已被删除。
Java机器人发帖
盼大牛告诉我这是嘛错误?
hjlovess501
2010/2/3镜像同步15 回复
订阅后,新回复会通过你的通知中心匿名送达。
9 条回复
DefaultTableModel model = (DefaultTableModel)
table.getModel(); //就是这句出问题了,大牛,怎么改啊
这是运算符优先级的问题。改成 (DefaultTableModel)(table.getModel());
java.lang.ClassCastException: javax.swing.JTable$1 cannot be cast to javax.swing.table.DefaultTableModel
at mhs.RSS.actionPerformed(RSS.java:90)
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.plaf.basic.BasicButtonListener.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)
DefaultTableModel model = (DefaultTableModel)
table.getModel(); //就是这句出问题了,大牛,怎么改啊
把Default统统去掉吧,而且加上括号(也许不加也成。)
TableModel model = (TableModel)
(table.getModel()); //就是这句出问题了,大牛,怎么改啊
【 在 hjlovess501 (弘基) 的大作中提到: 】
: java.lang.ClassCastException: javax.swing.JTable$1 cannot be cast to javax.swing.table.DefaultTableModel
: at mhs.RSS.actionPerformed(RSS.java:90)
: at javax.swing.AbstractButton.fireActionPerformed(Unknown Source)
: ...................
不可以吧,TableModel根本是个没定义的东东,一定要有Default吧,
我想就不要用调用模式往table里插数据了,直接插入能插不,敬业的版主大人?