tags:

views:

23

answers:

1

I'm looking at a GUI program made using MVC and they have this method. What does this method do and when will you need to use it?

attach(this);

Here is one of the classes with the method.

import model.*;

import java.awt.*;
import java.text.*;
import javax.swing.*;

public class IncomePanel extends JPanel implements View
{   private Stadium stadium;
    private JLabel label;

public IncomePanel(Stadium stadium)
{   this.stadium = stadium;
    for (Group group: stadium.seats())
        group.attach(this);
    setup();
    build(); }

private void setup()
{   setBorder(BorderFactory.createLineBorder(Color.blue));
    setLayout(new FlowLayout());    }

private void build()
{   label = new JLabel(income());
    label.setForeground(Color.blue);
    add(label); }

public void update()
{   label.setText(income());  }

private String income()
{   return "Income is $" + twoDec(stadium.income());    }

private String twoDec(double value)
{   DecimalFormat twoD = new DecimalFormat("#0.00");
    return twoD.format(value); }
}
A: 

Look inside the model package which is part of your project. In there you'll find a file named group.java. It contains the source for the Group class or interface.

All this snippet tells us is that Group's attach method takes an IncomePanel as an argument. If you want to find out what it does with it you have to look at the Group file.

JRL