title: Codename One - How to close all Tabs (via code)
body: <p>I have <code>Tabs</code> component in my form. Tabs are used for navigation.
  There are situations, where I need to close all tabs and open new one at index 0. To close all tabs I use this method:</p>
  
  <pre><code>void close(int from) {
      int to = tabs.getTabCount() - 1;
      for (int i = to; i &gt;= from; i--) {
          tabs.removeTabAt(i);
      }
      if (from &gt; 0)
          tabs.setSelectedIndex(from - 1);
  }
  
  void closeAll() {
      close(0);
  }
  </code></pre>
  
  <p>This works, but not always. Sometimes I recieve two exceptions:</p>
  
  <pre><code>[EDT] 0:0:8,436 - Exception: java.lang.IndexOutOfBoundsException - Index: 1, Size: 0
  java.lang.IndexOutOfBoundsException: Index: 1, Size: 0
      at java.util.ArrayList.rangeCheck(ArrayList.java:653)
      at java.util.ArrayList.get(ArrayList.java:429)
      at com.codename1.ui.Container.getComponentAt(Container.java:1551)
      at com.codename1.ui.Container.doLayout(Container.java:1520)
      at com.codename1.ui.Container.layoutContainer(Container.java:1509)
      at com.codename1.ui.Container.doLayout(Container.java:1522)
      at com.codename1.ui.Container.layoutContainer(Container.java:1509)
      at com.codename1.ui.Container.paint(Container.java:1406)
      at com.codename1.ui.Component.internalPaintImpl(Component.java:1333)
      at com.codename1.ui.Component.paintInternalImpl(Component.java:1307)
      at com.codename1.ui.Component.paintInternal(Component.java:1282)
      at com.codename1.ui.Component.paintInternal(Component.java:1250)
      at com.codename1.ui.Component.paintComponent(Component.java:1535)
      at com.codename1.ui.Component.paintComponent(Component.java:1482)
      at com.codename1.impl.CodenameOneImplementation.paintDirty(CodenameOneImplementation.java:588)
      at com.codename1.impl.javase.JavaSEPort.paintDirty(JavaSEPort.java:1279)
      at com.codename1.ui.Display.edtLoopImpl(Display.java:1077)
      at com.codename1.ui.Display.mainEDTLoop(Display.java:999)
      at com.codename1.ui.RunnableWrapper.run(RunnableWrapper.java:120)
      at com.codename1.impl.CodenameOneThread.run(CodenameOneThread.java:176)
  java.lang.ArrayIndexOutOfBoundsException: -1
      at java.util.ArrayList.elementData(ArrayList.java:418)
      at java.util.ArrayList.get(ArrayList.java:431)
      at com.codename1.ui.Container.getComponentAt(Container.java:1551)
      at com.codename1.ui.Tabs$1.actionPerformed(Tabs.java:573)
      at com.codename1.ui.util.EventDispatcher.fireActionSync(EventDispatcher.java:459)
      at com.codename1.ui.util.EventDispatcher.access$100(EventDispatcher.java:45)
      at com.codename1.ui.util.EventDispatcher$CallbackClass.run(EventDispatcher.java:95)
      at com.codename1.ui.Display.processSerialCalls(Display.java:1154)
      at com.codename1.ui.Display.edtLoopImpl(Display.java:1098)
      at com.codename1.ui.Display.flushEdt(Display.java:886)
      at com.codename1.ui.Form.showModal(Form.java:1762)
      at com.codename1.ui.Dialog.showModal(Dialog.java:1085)
      at com.codename1.ui.Dialog.show(Dialog.java:553)
      at com.codename1.ui.Dialog.showPackedImpl(Dialog.java:1389)
      at com.codename1.ui.Dialog.showPacked(Dialog.java:1297)
      at com.codename1.ui.Dialog.showImpl(Dialog.java:1041)
      at com.codename1.ui.Dialog.show(Dialog.java:1019)
      at com.codename1.ui.Dialog.show(Dialog.java:975)
      at com.codename1.ui.Dialog.show(Dialog.java:764)
      at com.codename1.ui.Dialog.show(Dialog.java:717)
      at com.codename1.ui.Dialog.show(Dialog.java:682)
      at com.codename1.ui.Dialog.show(Dialog.java:623)
      at com.codename1.ui.Dialog.show(Dialog.java:778)
      at com.codename1.ui.Display.mainEDTLoop(Display.java:1012)
      at com.codename1.ui.RunnableWrapper.run(RunnableWrapper.java:120)
      at com.codename1.impl.CodenameOneThread.run(CodenameOneThread.java:176)
  </code></pre>
  
  <p>So what is <strong>the right way to close all tabs</strong>?</p>
  
  <p><strong>ADDED</strong></p>
  
  <p>Exceptions are thrown only if <code>closeAll()</code> is invoked from sheduled <code>Runnable</code>. I use <code>Runnable</code> to close tabs after tabs slide end.</p>