Java: new panelS in runtime...

First of all I already googled this. I know the community here are active so getting a reply here would be fairly quick.

Are there any ways for me to be able to add panels and Text Fields indefinitely during run time of my program? Much like a loop that created panels during run time.

Why would I need to? Let’s say my program creates dragons. Each dragon has to have damage outputs to each other individual dragons created by my program. I need to have an option that launches ONE frame and let’s me input damage outputs, of a single dragon, to each of the other created dragons.

Thanks…

I’m deeply confused, why don’t you just paint everything instead of using crap panels Also java has a forgetful bitmap, you’re not going to just keep static images on the screen, no matter what you’re going to have to redraw them.

you know what… scrap that… is there a way for me to add new values to a one dimensional array of String? The ArrayIndexOutOfBounds exception is giving me a headache… I need my editable JComboBox to include a unique user input to be added in its list…

I am not 100% sure about java, but most of this goes for C.

When using dynamic memory allocation, a finite size is allocated on the heap. If you need more size than was allocated, there is a realloc command, that in essence sees if there is space left to simply resize, or if there isn’t enough space, finds the next available block with enough space for the array, plus the size needed to resize, and copies all the data. This makes it a very slow procedure.

In Java, being a slightly more abstracted language, I am given to understand you can do this (almost) by using the Vector object (deprecated) or Set, List, which support generics and will give you better control over Strings added to them.

EDIT: It’s late, and the above might not be coherent. While I am too bored to correct it, the gist of what I am saying is that: “Simple array re-sizing isn’t possible at all.” so you either create a new array with a new size, and copy the old data and destroy the old array, OR use available java classes like the deprecated Vector, Set, List etc that will do this for you transparently. In general, realize that this is very bad if it has to happen, and it is best to use some kind of smart resizing. (For example doubling the size of the array each time it needs to be re-sized so as to limit (after a while) the amount of copying that needs to be done.

Use an [url=https://docs.oracle.com/javase/6/docs/api/java/util/ArrayList.html]ArrayList[/url]<String> instead of an array.

Thanks… it works!

I have another problem… I pass an argument of Strings to another class and store it in an array and when I try to print the array in that same class, “null” is printed. Why’s that?

If I can understand what you are saying, you are trying to pass an arraylist as an argument.

If the arraylist was declared locally, then this will not work. While java doesn’t support “explicit” pointers, technically it is a pointer issue. When you declare an instance of a class, and pass it as an argument, it is passed by reference. Your parameter is merely a reference to the existing instance. You probably the proceed to copy this reference to a new variable, copying the pointer. When the original arraylist is destroyed, all the references are Null.

If the reference is still valid, try instantiating the destination arraylist, and then explictly copy each member to the new arraylist. This should solve your problem.

Founded in 2004, Leakfree.org became one of the first online communities dedicated to Valve’s Source engine development. It is more famously known for the formation of Black Mesa: Source under the 'Leakfree Modification Team' handle in September 2004.