我很难将行添加到位于不同类的表中。类之间的关系
以下是类结构:
虚线箭头所期望的关系,我不管理有
在AddPanel
类我有一个的Fileds和Add按钮。
单击addButton时,我首先创建Product(位于Logic Package中的类)的实例。然后我需要添加行到表格中(使用TableModel.AddRow
方法)。
以下是GUI外貌(聚焦选项卡AddPannel
):
我尝试不同approches但他们不被successed。
我最后的尝试是在Table
类创建以下方法:
public void AddRow(Product p) {
tbmStock.addRow(new Object[] { p.getExternalId(), p.getName(),
p.getAmount(), p.getPriceForMe(), p.getPriceForCustomer() });
}
此外,在AddPanel
类我尝试添加下面的方法:
private void AddButtonAction() {
btnAddProduct.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
try {
Product p = new Product(txtName.getText(), txtExternalID
.getText(), Integer.parseInt(txtAmount.getText()),
Double.parseDouble(txtPriceForMe.getText()),
Double.parseDouble(txtPriceForCustomer.getText()),
Integer.parseInt(txtFromYear.getText()), Integer
.parseInt(txtToYear.getText()), Integer
.parseInt(txtSupplier.getText()), Integer
.parseInt(txtCarType.getText()));
AddRow(p); //This call doesn't compiles
}
catch (Exception e1){
JOptionPane.showMessageDialog(null,"Error");
}
}
});
}
有什么建议? (实际上我甚至不确定我的结构是否良好:S)
你可以简单地表模型的引用添加到AddPanel – MadProgrammer 2013-02-16 10:27:21
实际上“这不起作用“表示由于以下原因它不编译:'AddRow(Product)方法是undefin编辑为新的ActionListener类型(){}' – 2013-02-16 10:39:44
@Gil Peretz在这种情况下,MadProgrammer大部分都是正确的,你需要在AddPanel中引用你的表,然后在你的ActionListener中调用table.AddRow()。不过,我建议创建一个自定义的'TableModel',它可以处理'Product'集合。 – annih 2013-02-16 10:50:10