crAlexander
crAlexander

Reputation: 386

Remove Item from Component[] in java

I need to know how i can remove an item or items from a Component[ ] array in java.I don't see any methods to do it fast.I have to use java Arrays to achive that?

For Example: I have a Jpanel with 30 same buttons and i do this:

   Component[] comp = panel.getComponents();

How i can remove one or all the items from comp?

And something more if i set comp=null; it will clear all the items?

What i do:

i try to sort the Items of JPanel by name and i use Comparator adding all the items to comp and then after comp is sorted i add them again back to panel(All the items i sort are same buttons with different names) Is there a best way to do this?

A picture with the code i use:

A picture

Also the JButtons are part of app(let's say they are Tracks they have right click function and others)

Part 2: My main goal is to Dealocate the memory is used by Array Components[] (sometimes it big enough like 150 mbs) and after sorting i just don't need these Components anymore.I want Array Components[] to be alocated from memory by java garbage Collector.

Upvotes: 0

Views: 266

Answers (1)

jdb1015
jdb1015

Reputation: 145

Ignoring your questionable design, put the getComponents() value in a List where you can dynamically remove elements and it will automatically resize itself:

List<Component> compList = new ArrayList<Component>(Arrays.asList(panel.getComponents()));
// Let's assume compList has a size() value of 10
compList.remove(4);
compList.remove(7);

Upvotes: 1

Related Questions