Is it possible to add more than 500 components to a panel at runtime in Java? – Education Career Blog

Is it possible to add more than 500 components to a panel at runtime in Java?

,

Possible – yes.

But why on earth would you want to do such a thing? I see no reason whatsoever for a panel to have 500 components.

,

Depending on what you are doing there are probably better alternatives.

For example JTables use a limited number of CellRenderers that draw themselves at multiple locations, giving the appearance of multiple components whilst dramatically reducing the memory usage (and increasing performance).

Can you provide any more information on your particular situation?

,

You may run into trouble on certain platforms if you are using heavyweight (AWT) components. The AWT component implementations may well be using OS/windowing system resources. On some platforms these may be limited. Back in the olden days, even NT 4 could have GDI problems across the system when an application attempted to create two many components (for instance, for each item in a tree).

In Swing, although lightweight, components are surprisingly large. Therefore, the renderer idiom is preferred for the likes of lists, trees and tables – see the API docs.

Leave a Comment