尝试向JTable添加行时出错

时间:2018-04-06 08:32:07

标签: java

当用户在JOptionPane确认对话框的帮助下要求将产品添加到购物车时,我尝试向表中添加行。我的代码如下:

namespace Lab1_OrderCake
{
public partial class frmCakeOrder : Form
{

    Order cakeOrder;
    public List<Cake> cakeList;
    public frmCakeOrder()
    {
        InitializeComponent();
        cmbTraditionalCake.SelectedIndex = 0;
        cakeOrder = new Order();
        gbCustomCake.Visible = false;
        this.Size = new Size(700,360);
        cakeList = new List<Cake>();

    } 

    private void bttnOrder_Click(object sender, EventArgs e)
    {
        DialogResult dlgMsg;
        dlgMsg = MessageBox.Show(cakeOrder.ToString(), "Confirm Order", MessageBoxButtons.YesNo, MessageBoxIcon.Information);

        if (dlgMsg == DialogResult.Yes)
        {  
            MessageBox.Show(cakeOrder.PrintConfirmation());

        }
        else
        { 
            MessageBox.Show ("The order has not been placed");
        }
        bttnReset.Focus();
        cakeOrder.ClearCart();

    }

    private void radCustom_CheckedChanged(object sender, EventArgs e)
    {
        if (radCustom.Checked)
        {
            cmbTraditionalCake.Enabled = false;
            gbCustomCake.Visible = true;
        }
        else
        {
            cmbTraditionalCake.Enabled = true;
            gbCustomCake.Visible = false;
        }
    }

    private void btnView_Click(object sender, EventArgs e)
    {
        DialogResult dlgMsg;

        cakeOrder.NumOfCakes=1;
        dlgMsg = MessageBox.Show(cakeOrder.ToString(), "Your order: ", MessageBoxButtons.YesNo , MessageBoxIcon.Information);
        if (dlgMsg == DialogResult.No)
        {
            cakeOrder.ClearCart();
            MessageBox.Show("Please enter and confirm your order!");
  }



    private void btnAdd_Click(object sender, EventArgs e)
    {

        if (radCustom.Checked)
        {
            string flavour, occasion;
            flavour = occasion = "";
            int layers;

            //for flavor
            if (radBanana.Checked)
                flavour = "Banana";
            else if (radChocolate.Checked)
                flavour = "Chocolate";
            else if (radVanilla.Checked)
                flavour = "Vanilla";

            if (radTier2.Checked)
                layers = 2;
            else if (radTier3.Checked)
                layers = 3;
            else
                layers = 1;

            if (radGraduation.Checked)
                occasion = radGraduation.Text.TrimStart(new char[] { '&' });
            else if (radWedding.Checked)
                occasion = radWedding.Text.TrimStart(new char[] { '&' });
            else occasion = radAnniversary.Text.TrimStart(new char[] { '&' });
            cakeOrder.AddCake(new Custom(flavour, occasion, layers));
        }
        else
        {
            cakeOrder.AddCake(new Traditional(cmbTraditionalCake.SelectedItem.ToString()));
        }
        cakeList.Add(cakeOrder);
    }


}
}

但是我收到此错误: 线程中的异常&#34; AWT-EventQueue-0&#34; java.lang.ClassCastException:javax.swing.JTable $ 1无法强制转换为javax.swing.table.DefaultTableModel

有人可以帮助我解决我出错的地方吗?谢谢。

1 个答案:

答案 0 :(得分:0)

cartTable = new JTable(data, columns);
如果我没记错的话,

使用匿名的AbstractTableModel。

改为使用:

cartTable = new JTable(new DefaultTableModel(data, columns));

DefaultTableModel触发其他更改事件并处理动态数据。