wood burning stoves 2.0*
The moose likes Beginning Java and the fly likes accessing single linked list from another class Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "accessing single linked list from another class" Watch "accessing single linked list from another class" New topic
Author

accessing single linked list from another class

e leblanc
Greenhorn

Joined: Oct 06, 2010
Posts: 1
hey there,
i'm trying to access the information in a single linked list from another class under the same package, not sure how to or if i even can. i can't seem to find a direct answer. i hope its not something silly, so bear with me if i'm overlooking a detail that i should have seen. i think i just need another perspective on it.

so the point of the program thus far is to read in a series of 400 coordinates from a test file and draw the resulting polygon in a gui.

so here's the read in/link creation:



and here's relevant portion of the display class:



so that obviously doesn't compile because the linked list is instantiated in the FileIn class.

i want to access the variables stored in the points list that was created so that i can draw lines between the points as the list is traversed. i'm actually getting an idea as i'm typing this but if anyone can take a look i'd surely appreciate it.

i've hit a wall and any help would be greatly appreciated.

Thank you!
Emily
Kurt Van Etten
Ranch Hand

Joined: Sep 07, 2010
Posts: 98
Hi Emily,

I'm not sure I understand the flow of control in your program--it seems a little odd having your main method inside the FileIn class. But in any event, the paintComponent() method just needs to receive the SingleLinkedList points as a parameter, and you should be good to go.
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 38068
    
  22
Welcome to the Ranch Emily leBlanc
Kevin Workman
Ranch Hand

Joined: Sep 28, 2010
Posts: 151
If you want to access a variable in one class from another, you either need to give access to it via a getter function, or pass it in as a parameter in a constructor or a setter function.

In your case, you would either have a getPoints method in the FileIn class, or a setPoints method (or pass the list into the constructor) in the Display class. Which one you want depends on how you are creating these classes, and which has access to which.
Kevin Workman
Ranch Hand

Joined: Sep 28, 2010
Posts: 151
Kurt Van Etten wrote:it seems a little odd having your main method inside the FileIn class.

I could agree with that.

Kurt Van Etten wrote:But in any event, the paintComponent() method just needs to receive the SingleLinkedList points as a parameter, and you should be good to go.

No, this is almost definitely wrong. The display class is probably a subclass of JPanel, and paintComponent is a very specific method that is called by the parent class for repainting. Adding a parameter to the paintComponent method would break that fundamental functionality.

The Display class does need access to the list, but not via the paintComponent method. It either needs to call a getter function of FileIn, or more likely have a setPoints method or take the list as a parameter in the constructor.
Kevin Workman
Ranch Hand

Joined: Sep 28, 2010
Posts: 151
Ugh, please don't crosspost.
Carefully Choose One Forum
Crossposted here: http://www.java-forums.org/new-java/33218-accessing-single-linked-list-another-class.html
Kurt Van Etten
Ranch Hand

Joined: Sep 07, 2010
Posts: 98
No, this is almost definitely wrong.


Kevin, you are absolutely correct, that was pretty brain-dead of me.

Now, I have what is probably a really dumb (and off-topic) question for you, but how do you quote upstream replies and get the author <cite> tag in there?
Kurt Van Etten
Ranch Hand

Joined: Sep 07, 2010
Posts: 98
Kurt Van Etten wrote:Now, I have what is probably a really dumb (and off-topic) question for you, but how do you quote upstream replies and get the author <cite> tag in there?


Okay, nevermind... I see it now!
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: accessing single linked list from another class
 
Similar Threads
drawing arrows
Drawing a Diamond Shape (like Rectangle, Circle)
Calculating the distance between two points problem
flicker in my program
Using mouse clicks to draw on a JFrame, problem probably with coordinates