Page 1 of 1
Java help
Posted: March 31st, 2009, 13:40
by buzzmong
Bascially, I'm in a bit of a pickle due to the way I've structured my program and the way java handles tabs in an application.
The program has two tabbed panes, in the first of the tabbed panes I've got a String Array being generated *after* the user and program has performed a number of actions on the input they enter on that pane.
All dandy so far.
However, what I need is that array to be accessible from within the 2nd panel, and for the life of me I can't think how to access it due to it being buried a couple of levels down in the first panel.
Code: Select all
Program structure:
Main.java -> GuiTabs.java -> LexPanel (the string [] is in here)
-> SynPanel (I need to use the array here after it's been filled with data from the LexPanel).
There's also a Token class which isn't linked but used by both.
Any ideas?
Posted: March 31st, 2009, 14:07
by Fear
I'm no java guru, but i'd expect something like
Code: Select all
this.parent.LexPanel.YourStringArray
to work.
Posted: March 31st, 2009, 14:27
by buzzmong
I wish it would as I've already tried that, appears to have no way of accessing a parent (that I've found anyway) outside of visual objects.
Posted: March 31st, 2009, 14:38
by Stoat
Have you tried this?
Code: Select all
this.parent.LexPanel.YourStringArray.please
Can't hurt.
Posted: March 31st, 2009, 14:47
by deject
How is the array defined?
Posted: March 31st, 2009, 14:57
by buzzmong
deject wrote:How is the array defined?
As a return value in a public get method in LexPanel. I could define it as a seperate public String[] inside the LexPanel class, but that's not the issue.
Currently I access the get and set methods in GuiTabs, using the instances of LexPanel and SynPanel, which is fine incidently, however obivously it only runs that at compilation and not whenever I need it to at run time.
I was hoping I could just call a parent method from the SynPanel instance seeing as java doesn't do object by reference, but looking at it, I might be able to pull some checks to do with the focus of the panels and just call it then.
Posted: March 31st, 2009, 15:00
by Dr. kitteny berk
... Got any jam?
Posted: March 31st, 2009, 16:22
by deject
Wait what? Java does everything but primitives by reference.
Have you tried storing the array in the main method then passing it to the SynPanel?
Posted: March 31st, 2009, 16:28
by Fear
Pass a reference to the parent when you initialise the child. The child can then store that reference so it can talk back to the parent.
You could even pass a reference to the string array if you really wanted - but a reference to the parent should cover you in every circumstance.
I'm surprised the base object class in Java (from which all classes will inherit) doesn't contain such simple functionality as standard.
Posted: March 31st, 2009, 18:52
by HereComesPete
Fear wrote:a reference to the parent should cover you in every circumstance.
Even if the circumstance is
"They knew what I was doing! They
let me touch him in his naughty place!"
?
/unhelpful
Posted: March 31st, 2009, 23:26
by buzzmong
Well, I've manged to get the information I need across, as noted by my extremely dodgy use of JOptionPanes in the debugging process.
The JList doens't want to update properly, but that's a minor issue and I'm sure I can make it bend to my will.
As a note, I ended up sending the instance of the LexPanel class into the SynPanel when I initialised it a few lines down. I get the data, it's all that matters.
Cheers Fear and Deej.