Hi, I've been working on something during my spare time at work (i'm not a java programmer at work!!), and this snippet of code works perfectly well, although I didn't expect it to, and would be grateful if someone could explain why it works, and why I am clearly wrong about how I think it should work!
Here's the code:
The bit I don't get is this: jTabbedPane1.add(choose.getDescription(files[i]),new JScrollPane(area = new JTextArea())); , and it's because I define a new JTextArea called area, and then do it several times, so in my mind, this should mean when I open several files, they should display the contents of the last file to go through the for loop iteration...but I was wrong, and I don't understand why!!
Each time you call "area = new JTextArea()" you create a new JTextArea on the heap and assign a reference to it to the local variable area. Then you add that JTextArea to the TabbedPane. The tabbed pane gets a new reference to this same JTextArea on the heap. While area is still in scope - hence referencing the newly created JTextArea you set its text.
Each iteration through the loop, area references a new JTextArea. so calling area.setText in one iteration has no effect on any of the other JTextAreas.
David J Evans
Joined: Apr 28, 2006
Tim, thanks for your reply. Sorry Jesper, but I really don't see how what you said explained what I was seeing.
Tim (or anyone else) - is what I've done safe? I mean, can I reference each instance of area, and if so, what name would I use? [ October 19, 2006: Message edited by: David J Evans ]
Joined: Aug 31, 2006
What you have done is completely safe (well as far as the JTextAreas, I'm not sure what would happen if the user selected like 200 files ) but after each iteration you don't have a reference to each JTextArea created. Depending on what you are trying to do you may or may not need them. You could stick each one in a HashMap with the Key as the name of the file? Or just an ArrayList? Impossible to say what you want to do. [ October 19, 2006: Message edited by: Tim LeMaster ]
Originally posted by David J Evans: just feel like I should maintain control over them in some way lol!
I think the most effective way to deal with it is to try to get rid of that feeling...
Seriously, to a beginner object oriented code often looks like there is some form of "magic" involved - things just seem to happen magically, without directly controlling it.
But that's actually a good thing - once you get rid of the need to control everything to the detail, and you begin to understand what's actually happening behind the curtains, what is making all that magic work - it will make you a much more effective programmer!
The soul is dyed the color of its thoughts. Think only on those things that are in line with your principles and can bear the light of day. The content of your character is your choice. Day by day, what you do is who you become. Your integrity is your destiny - it is the light that guides your way. - Heraclitus
subject: this is working but i don't understand why!!