• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

accessing single linked list from another class

 
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Ranch Hand
Posts: 98
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Marshal
Posts: 79177
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Welcome to the Ranch Emily leBlanc
 
Ranch Hand
Posts: 151
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 151
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Posts: 151
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 98
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Posts: 98
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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!
 
Their achilles heel is the noogie! Give them noogies tiny ad!
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic