Gil Peretz
Gil Peretz

Reputation: 2419

relations between classes

I'm having a difficults to add rows to a table that located in different class.

Following are the classes structure:

enter image description here The dashed arrow is the desired relation that I dont manage to have

in the AddPanel class I have a fileds and Addbutton.

when clicking the addButton I first creating an instance of Product (class located in Logic Package). Then I need to add row to the table (Using the TableModel.AddRow method).

Following are the GUI Looks (the focused tab is AddPannel):

enter image description here

I tried different approches but non of them were successed.

My last attempt was to create in the Table class the following method:

public void AddRow(Product p) {
    tbmStock.addRow(new Object[] { p.getExternalId(), p.getName(),
            p.getAmount(), p.getPriceForMe(), p.getPriceForCustomer() });
}

In addition, in the AddPanel class I tried to add the following method:

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");
            }       
        }
    });

}

Any suggestions ? (actually I'm not sure even that my structure is good :S )

Upvotes: 1

Views: 108

Answers (1)

Puce
Puce

Reputation: 38132

Provide a custom event and event listener. If you create a new product fire the event and notify all listeners. The MainPanel (assuming that's where you create the instance of AddPanel), should register a listener. In this listener you then can update the table as the MainPanel has access to the table.

This is also known as the Mediator pattern.

Some pointers:

Create an event class, e.g. ProductCreatedEvent extends EventObject. Pass the Product as an argument to the constructor and make it accessible with a getter.

Create an event listener class: interface ProductCreatedEventListener extends EventListener. Provide a method such as productCreated(ProductCreatedEvent productCreatedEvent).

In the AddPanel add something like:

private final List<ProductCreatedEventListener> productCreatedEventListeners = new ArrayList<>();
...

public void addProductCreatedEventListener(ProductCreatedEventListener productCreatedEventListener){
    productCreatedEventListeners.add(productCreatedEventListener);
}

public void removeProductCreatedEventListener(ProductCreatedEventListener productCreatedEventListener){
    productCreatedEventListeners.remove(productCreatedEventListener);
}

private void fireProductCreatedEvent(ProductCreatedEvent event){
    for (ProductCreatedEventListener productCreatedEventListener : productCreatedEventListeners){
        productCreatedEventListener.productCreated(event);
    }
}

Replace:

AddRow(p); //This isn't working

with

   fireProductCreatedEvent(new ProductCreatedEvent(AddPanel.this, p));

Upvotes: 3

Related Questions