每当用户在JOptionPane确认对话框的帮助下被询问是否将产品添加到购物车时选择“是”,我都会尝试将行添加到我的表中。我的代码如下:
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.Statement;
import javax.swing.*;
import javax.swing.table.DefaultTableModel;
public class Home {
final static int WINDOW_WIDTH = 500;
final static int WINDOW_HEIGHT = 200;
JFrame window;
public Home() {
window = new JFrame();
window.setTitle("Home");
window.setSize(WINDOW_WIDTH, WINDOW_HEIGHT);
window.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JLabel title = new JLabel("Please select a book for more information", JLabel.CENTER);
title.setHorizontalTextPosition(JLabel.CENTER);
Font plainFont = new Font("Serif", 0, 24);
title.setFont(plainFont);
title.setBounds(180, 230, 97, 29);
//====================================================================//
String [] bookStrings = {"[Select a book]", "The Host", "Ruby", "Divergent", "The Secret Garden", "Hunger Games"};
JComboBox bookList = new JComboBox(bookStrings);
//====================================================================//
JLabel bookinfo = new JLabel();
//====================================================================//
JButton search = new JButton("Search");
search.setHorizontalTextPosition(JButton.CENTER);
JButton cart = new JButton("Show Cart");
search.setHorizontalTextPosition(JButton.CENTER);
JButton exit = new JButton("Exit");
search.setHorizontalTextPosition(JButton.CENTER);
JTable cartTable;
String[] columns = {"Book Name","Author", "Quantity", "Price"};
String [][] data = {};
cartTable = new JTable(data, columns);
cartTable.setPreferredScrollableViewportSize(new Dimension(450, 63));
cartTable.setFillsViewportHeight(true);
JScrollPane jps = new JScrollPane(cartTable);
//====================================================================//
search.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
if (bookList.getSelectedIndex() == 1) {
int reply = JOptionPane.showConfirmDialog(null,
"<html>Title: The Host <br> Author: Stephenie Meyer <br> Price: 110 AED <br> " +
"\n Synopsis: The Host is a romance novel by Stephenie Meyer. The book is about Earth, " +
"\n in a post apocalyptic time, being invaded by a parasitic alien race, known as \"Souls\", and " +
"\n follows one Soul's predicament when the consciousness of her human host refuses to co-operate " +
"\n with the takeover of her body."
+ "\n \n Would you like to add this to cart?", "Book Details", JOptionPane.YES_NO_OPTION);
if (reply == JOptionPane.YES_OPTION){
String [][] data = {{"The Host", "Stephenie Meyer", "2", "110"}};
DefaultTableModel model = (DefaultTableModel)(cartTable.getModel());
model.addRow(data);
}
else {
JOptionPane.showMessageDialog(null, "GOODBYE");
}
}
});
cart.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
new Cart();
window.setVisible(false);
}
});
exit.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
System.exit(0);
}
});
//====================================================================//
JPanel panel;
panel = new JPanel();
panel.add(title);
panel.add(bookList);
panel.add(search);
panel.add(bookinfo);
panel.add(cart);
panel.add(exit);
panel.add(jps);
window.add(panel);
window.setVisible(true);
}
}
然而,我得到了这个错误:线程“AWT-EventQueue-0”java.lang.ClassCastException:javax.swing中出现异常。JTable$1无法强制转换为javax.swing.table.DefaultTableModel
有人可以帮助我解决我出错的地方吗?谢谢。
cartTable = new JTable(data, columns);
如果我没记错的话,使用匿名AbstractTableModel。
改为使用:
cartTable = new JTable(new DefaultTableModel(data, columns));
DefaultTableModel触发杂项更改事件并处理动态数据。