setSelectedIndex in selectionListener (Tabs)

31 views
Skip to first unread message

Maaike

unread,
Aug 13, 2014, 11:01:34 AM8/13/14
to codenameone...@googlegroups.com
I tried to swipe between tabs and add tabs 'on the fly', like mentioned here: Swipe to replace container.

When swiping to the previous tab, I add a new tab with 'insertTab' with index 0. Then I change the current tab with 'setSelectedIndex' (because current index is now 1). This is going ok when doing this for the first time (so when first showing the Tabs, I show the current one with current date, make a new one on index 0, change the index to 1 and I can swipe 'back' to the newly added tab with index 0).

I'm trying to add new tabs in the SelectionListener: when I'm on tab with index 0, I add a new one on index 0. The problem is: setSelectedIndex is not going well here. I'm always staying on the newly added tab at index 0, but I want to go to the 'current' tab, with new index 1. I hope you understand what I mean. 

Here is my code:

    public class SelectTabListener implements SelectionListener
   
{


       
public void selectionChanged(int oldSelected, int newSelected)
       
{
           
DateContainer dc = (DateContainer)swipeContainer.getTabComponentAt(newSelected);
           
Date currentDate = dc.getDate();
           
            dateLabel
.setText(dateFunctions.getDateString(currentDate));
           
           
if (newSelected == 0)
           
{
               
Calendar c = Calendar.getInstance();
                c
.setTime(currentDate);
                c
.add(Calendar.DAY_OF_MONTH, -1);
               
Date newDate = c.getTime();
               
DateContainer newContainer = getDayListContainer(newDate);
                swipeContainer
.insertTab("", null, newContainer, 0);
                swipeContainer
.removeSelectionListener(selectTabListener);
                swipeContainer
.setSelectedIndex(newSelected + 1);
                mainContainer
.revalidate();


                swipeContainer
.revalidate();


                swipeContainer
.addSelectionListener(selectTabListener);
           
}
       
}

   
}

Maaike

unread,
Aug 13, 2014, 11:32:50 AM8/13/14
to codenameone...@googlegroups.com
I'm sorry I didn't search better. I already found the (working!) answer:


Just wrap the code in the body with 
Display.getInstance().callSerially(new Runnable() {
   
public void run() {
        
... your code here
   
}
});

Normally this is used to send data back to the EDT but in this case it can be useful to deffer a call to the next EDT cycle and let this event chain complete. 
Reply all
Reply to author
Forward
0 new messages