0

This seems so basic, but for some reason I can't get it to work.

I have the following code:

Component[] AddEditDelete = ToolbarPool.getDefault().findToolbar("AddEditDelete").getComponents();
for (Component component : AddEditDelete) {
    component.setEnabled(false);
}

Component[] Navigation = ToolbarPool.getDefault().findToolbar("Navigation").getComponents();
for (Component component : Navigation) {
    component.setEnabled(false);
}

Component[] ListFind = ToolbarPool.getDefault().findToolbar("ListFind").getComponents();
for (Component component : ListFind) {
    component.setEnabled(false);
}

What I would want to do is create a single Component[] c array and then into that array, add all the components from the Toolbars.

My intuitive approach of

Component[] c;
c.add(stuff);

Didn't seem to work. So I assume you do it else-how.

edit1: My most recent attempt with ArrayList > Component didn't work =(

ArrayList c = new ArrayList();
c.add(ToolbarPool.getDefault().findToolbar("AddEditDelete").getComponents());
c.add (ToolbarPool.getDefault().findToolbar("Navigation").getComponents());
Component[] cc = (Component[]) c.toArray();
for (Component component : cc) {
    component.setEnabled(false);
}

edit2: Silly me, trying to use ArrayList without a type. This works, but it will still be quite a few lines of code:

ArrayList<Component> c = new ArrayList<Component>();
for (int i = 0; i < ToolbarPool.getDefault().findToolbar("AddEditDelete").getComponents().length; i++) {
    c.add(ToolbarPool.getDefault().findToolbar("AddEditDelete").getComponent(i));
}
for (int i = 0; i < ToolbarPool.getDefault().findToolbar("Navigation").getComponents().length; i++) {
    c.add(ToolbarPool.getDefault().findToolbar("Navigation").getComponent(i));
}
for (Component component : c) {
    component.setEnabled(false);
}

Is there a way to shorten the amount of lines of code?

Metal Wing
  • 525
  • 1
  • 11
  • 22
  • 1
    In Java, methods start with lower-case letter and classes with upper-case so you should change your `AddEditDelete`, `Navigation` and `ListFind` to `addEditDelete`, `navigation` and `listFind`. This will make your code more readable – StepTNT May 18 '12 at 17:55
  • You can use addAll method of the List interface : http://docs.oracle.com/javase/7/docs/api/java/util/List.html#addAll%28java.util.Collection%29 (also see code snippet in my answer) – problemzebra May 18 '12 at 17:56
  • You are not encouraged to use Arrays unless you are learning Java. The have been largely replaced with Lists. Lists don't require you to know the number of items you will have beforehand. [link]http://docs.oracle.com/javase/7/docs/api/java/util/List.html Lists also offer easier iteration... – thejartender May 18 '12 at 18:38

3 Answers3

0

You need to create the array before you can use it (you need to know the array size in advance):

Component[] c = new Components[10];
c[0] = stuff;

It may be a better idea to use a List instead (more flexible, better API):

List<Component> components = new ArrayList<Components>
components .addAll(Arrays.asList(ToolbarPool.getDefault().findToolbar("AddEditDelete").getComponents()));
components .addAll(Arrays.asList(ToolbarPool.getDefault().findToolbar("Navigation").getComponents()));
for (Component component : components) {
    component.setEnabled(false);
}
problemzebra
  • 511
  • 6
  • 17
  • Actually there's no `array.add()` in Java, and you can iterate on a List so you don't need to make this `Component[] cc = (Component[]) c.toArray();` – StepTNT May 18 '12 at 17:51
  • Yes, I was going to note that too, because that was one of the previous things I've tried. Other than that, your code as you edited it now, is perfect! the .addAll is exactly what I needed, but I didn't know what were the right things to pass for the Collection. Thank You! – Metal Wing May 18 '12 at 18:02
0

Easiest way would be to use the code you presented first, then create a new Component[] with size = to the sum of the 3 arrays you got programmatically. Then manually add all elements in each of the 3 arrays to the unified array

ControlAltDel
  • 33,923
  • 10
  • 53
  • 80
0

In old approach as mentioned by others you need to create the array first.

In your new approach define arraylist as ArrayList<Component> c = new ArrayList<Component>();. Also please check whether ToolbarPool.getDefault().findToolbar("AddEditDelete").getComponents()) is returning a proper non null Component object.

raddykrish
  • 1,866
  • 13
  • 15