I have 2 JPanel: Panel_S, Panel_P. The 1st listens to Station events and the 2nd listens to Passenger events.
When new passenger is created it is added to JList in Panel_P. When passenger finished “going” to station by goToStation and getting in line by station.addPassenger(this); the station’s event fireAddPassengerEvent is fired and the passenger is added to JList in Panel_S. Now, I need some how to remove the passenger from JList in Panel_P because it finished “going” and entered the queue in station. What is the correct way to implement this? Does Panel_S should “tell” Panel_P to remove the passenger from its list once Panel_S adds passenger to its list?
If you didn’t understood my question please tell me and I’ll try to explain myself better.
public class Passenger extends Thread {
private Station station;
private Vector<PassengerEventsViewListener> viewListeners;
public Passenger(Station station)
{
this.station = station;
}
private void getInLineInStation()
{
station.addPassenger(this);
}
private void fireArrivedToStationEvent(EventArgs<Passenger> args)
{
for (PassengerEventsViewListener l : viewListeners) {
l.arrivedToStationEvent(args);
}
}
private void goToStation()
{
//going
fireArrivedToStationEvent(this);
}
@Override
public void run()
{
goToStation();
getInLineInStation();
}
}
public class Station {
private Vector<StationEventsViewListener> viewListeners;
private void fireAddPassengerEvent(Passenger passenger)
{
for (StationEventsViewListener l : viewListeners) {
l.addPassengerEvent(passenger);
}
}
public synchronized boolean addPassenger(Passenger p)
{
passengersInQueue.addLast(passenger);
fireAddPassengerEvent(passenger);
}
}
No, your views should not communicate and should not be coupled. Only the models talk and see each other. Each model’s view changes accordingly.
Back to your problem.
Panel_Pshould be linked to a model that contains information about passengers. In the other hand,Panel_Sshould be linked to a model that contains information about stations. Now when a new passenger arrives to a station, thepassengermodel should notify thestationmodel of the new arrival.Panel_Scan then be updated fromstationmodel without caring aboutpassengermodel nor its view (Panel_P)