The Artima Developer Community
Sponsored Link

Java Answers Forum
How can I access state information from different Classes in a list?

3 replies on 1 page. Most recent reply: Jan 5, 2006 5:01 AM by Dave Hinton

Welcome Guest
  Sign In

Go back to the topic listing  Back to Topic List Click to reply to this topic  Reply to this Topic Click to search messages in this forum  Search Forum Click for a threaded view of the topic  Threaded View   
Previous Topic   Next Topic
Flat View: This topic has 3 replies on 1 page
Bob W

Posts: 3
Nickname: bobw
Registered: Jan, 2006

How can I access state information from different Classes in a list? Posted: Jan 3, 2006 11:07 PM
Reply to this message Reply
Advertisement
I need to write monitors (tables, reports and graphs) that display information about groups of related variables in a simulation. My thought was to store the sources of the monitors information in an ArrayList. The sources will inevitably be different Classes (Stock, Flow or Auxiliary), however, and I don’t see how to “cast” them properly so that I can retrieve the needed state information.

The following code works for a single Class. Is there a way it could be enhanced to handle several different Classes?

import javax.swing.JInternalFrame;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;

public class Mtr extends JInternalFrame {
String name;
private JTextArea ta;
private JScrollPane sp;
private ArrayList srcLst;

Mtr(String name, int x, int y, int hte, int wth) {
super(name, false, false, true, true);
srcLst = new ArrayList();
ta = new JTextArea();
sp = new JScrollPane(ta,
JScrollPane.VERTICAL_SCROLLBAR_ALWAYS,
JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED);
add(sp);
setBounds(x,y,wth,hte);
}

public void stepFwd(int dt) {
ta.append("\n " + dt);
ListIterator itor = srcLst.listIterator();
while(itor.hasNext()) {
Stok tmpStk = (Stok) itor.next();
ta.append(" " + tmpStk.getLast());

}
}

public void stepBak(int dt) {
Stok tmpStk = new Stok("tmpStk");
ArrayList tmpAL = new ArrayList();
ta.setText("");
for(int i = 0; i < dt; i++) {
ta.append("\n " + i);
for(int j = 0; j < srcLst.size(); j++) {
tmpStk = (Stok) srcLst.get(j);
tmpAL = tmpStk.getLevels();
ta.append(" " + tmpAL.get(i));
}
}
}

public void addSrc(Stok stk) {
srcLst.add(stk);
}

}// Mtr


Dave Hinton

Posts: 42
Nickname: catbells
Registered: Oct, 2003

Re: How can I access state information from different Classes in a list? Posted: Jan 4, 2006 6:35 AM
Reply to this message Reply
Add a new method retrieveState() to the Stock, Flow and Auxiliary classes (actually, to their parent class, or to an interface they all implement). You then cast to the parent class / interface when you want to retrieve the state.

Bob W

Posts: 3
Nickname: bobw
Registered: Jan, 2006

Re: How can I access state information from different Classes in a list? Posted: Jan 4, 2006 12:54 PM
Reply to this message Reply
Thanks for replying. This is my first time on the forum, and its encouraging to get a reply so straight away.

I was curious if there might be some other way but, as you recognize, the problem calls for inheritance. The three mentioned elements are all Nodes in an interlinked network. I wrote a sketch of this solution that worked as you suggested. Plan to attack the larger problem tonight.

Not to ramble on, but by get my vote for best “Brief Bio”.

Thanks again.

Dave Hinton

Posts: 42
Nickname: catbells
Registered: Oct, 2003

Re: How can I access state information from different Classes in a list? Posted: Jan 5, 2006 5:01 AM
Reply to this message Reply
You’re welcome :-)

Flat View: This topic has 3 replies on 1 page
Topic: Date Comparison using Simple Date Format Previous Topic   Next Topic Topic: Automatically inserting into MS Outlook email fields

Sponsored Links



Google
  Web Artima.com   

Copyright © 1996-2019 Artima, Inc. All Rights Reserved. - Privacy Policy - Terms of Use