我的代码无效。我想在JTable中解析我的rss新闻,但是我不能这样做,因为我不知道如何为JTable制作数据代码
public class News {
public static String[] col = {"№", "Smi", "Title", "Link", "Description"};
public static String[][] data;
public static void main(String[] args) throws IOException {
new Window();
}
public static void readRSS() {
try {
for (i = 0; i < sources.length; i++) {
RSSFeedParser parser = new RSSFeedParser(sources[i]);
if (isStop.get()) return;
Feed feed = parser.readFeed();
for (FeedMessage message : feed.getMessages()) {
j++;
if (message.toString().indexOf(Window.find_word) >= 1) {
titleTable = message.getTitle();
linkTable = message.getLink();
descrTable = message.getDescription();
data = new String[feed.getMessages().size()][col.length];
for (int x = 0; x < feed.getMessages().size(); x++){
data[x][0] = String.valueOf(x);
data[x][1] = smi;
data[x][2] = titleTable;
data[x][3] = linkTable;
data[x][4] = descrTable;
}
带有Window的另一个类。当我使表格=新的JTable(News.data,News.col); -我有NullPointerException ..
public class Window extends JFrame {
//Table
JScrollPane scrollPane = new JScrollPane();
scrollPane.setBounds(10, 40, 874, 300);
getContentPane().add(scrollPane);
String[][] data = { {null, null, null, null, null}
};
table = new JTable(data, News.col);
table.setBackground(new Color(194, 239, 194));
scrollPane.setViewportView(table);
这是我从rss来源获取数据的数组。
public class Feed {
final List<FeedMessage> entries = new ArrayList<FeedMessage>();
public Feed(String title, String link, String description) {
}
public List<FeedMessage> getMessages() {
return entries;
}
}
class FeedMessage {
String title;
String link;
String description;
public void setTitle(String title) {
this.title = title;
}
public void setLink(String link) {
this.link = link;
}
public void setDescription(String description) {
this.description = description;
}
public String getLink() {
return link;
}
public String getTitle() {
return title;
}
public String getDescription() {
return description;
}
非常感谢!!! :))
JScrollPane scrollPane = new JScrollPane();
scrollPane.setBounds(10, 40, 874, 300);
getContentPane().add(scrollPane);
String[] columns = {"Title", "Author", "Description", "Date", "Feed"};
model = new DefaultTableModel(null, columns);
table = new JTable(model);
scrollPane.setViewportView(table);
String[] row = new String[]{
message.getTitle(),
message.getLink(),
message.getDescription()
};
Window.model.addRow(row);