You Are Here:

Community: Wiki

This page was last modified 09:11, 8 June 2009.

J2ME Tabbed Menu

From Forum Nokia Wiki

We will build a reusable Tabbed Menu using J2ME.

Image:J2me_tabbed_menu.jpg

View it in action

You can find a midlet showing this component in action here.

Our component will support:

  • Full styling of tabs (bg color, fore color, font face, margin, paddings and corner radius)
  • Automatic horizontal scrolling, so that you can put as many tab you want, without caring about screen width

Building the tabbed menu

Let's start from the customizable variables, whose aim is self-explaining:

int background = 0xffffff;
int bgColor = 0xcccccc;
int bgFocusedColor = 0x0000ff;
int foreColor = 0x000000;
int foreFocusedColor = 0xffffff;
int cornerRadius = 4;
int padding = 2;
int margin = 2;
Font font = Font.getDefaultFont();
int scrollStep = 20;

Then we'll define some internal variables, used to mantain the tabbed menu state:

int selectedTab = 0;	//selected tab index
int[] tabsWidth = null;	//width of single tabs
int[] tabsLeft = null;	//left X coordinate of single tabs
int tabHeight = 0;		//height of tabs (equal for all tabs)
String[] tabs = null;	//tab labels
int menuWidth = 0;		//total menu width
 
int viewportWidth = 0;	//visible viewport width
int viewportX = 0;		//current viewport X coordinate

Now, we'll define a simple constructor accepting tab labels and viewport width as parameters:

public TabMenu(String[] tabs, int width)
{
	this.tabs = tabs;
	
	this.viewportWidth = width;
	
	initialize();
}
void initialize()
{
	tabHeight = font.getHeight() + cornerRadius + 2 * padding;
	
	menuWidth = 0;
	
	tabsWidth = new int[tabs.length];
	tabsLeft  = new int[tabs.length];
	
	for(int i = 0; i < tabsWidth.length; i++)
	{
		tabsWidth[i] = font.stringWidth(tabs[i]) + 2 * padding + 2 * cornerRadius;
		
		tabsLeft[i] = menuWidth;
		
		menuWidth += tabsWidth[i];
		
		if(i > 0)
		{
			menuWidth += margin;
		}
	}
}

Now, let's define an utility method to check if a tab is visible or not.

private boolean isTabVisible(int tabIndex)
{
	return tabsLeft[tabIndex] < viewportX + viewportWidth &&
		tabsLeft[tabIndex] + tabsWidth[tabIndex] >= viewportX;
}

And now we'll implement the tab switching/scrolling methods, that will use the isTabVisible method defined above:

public void goRight()
{
	go(+1);
}
public void goLeft()
{
	go(-1);
}
private void go(int delta)
{
	int newTab = Math.max(0, Math.min(tabs.length - 1, selectedTab + delta));
	
	boolean scroll = true;
	
	if(newTab != selectedTab && isTabVisible(newTab))
	{
		selectedTab = newTab;
		
		if(	(delta > 0 && tabsLeft[selectedTab] + tabsWidth[selectedTab] > viewportX + viewportWidth) || 
			(delta < 0 && tabsLeft[selectedTab] < viewportX))
		{
			scroll = true;
		}
		else
		{
			scroll = false;
		}
	}
	if(scroll)
	{
		viewportX = Math.max(0, Math.min(menuWidth - viewportWidth, viewportX + delta * scrollStep));
	}
}

And now, we're ready to paint our menu :)

public void paint(Graphics g)
{
	int currentX = - viewportX;
		
	g.setClip(0, 0, viewportWidth, tabHeight);
		
	g.setColor(background);
	g.fillRect(0, 0, viewportWidth, tabHeight);
		
	for(int i = 0; i < tabs.length; i++)
	{
		g.setColor(i == selectedTab ? bgFocusedColor : bgColor);
			
		g.fillRoundRect(currentX, 0, tabsWidth[i], tabHeight + cornerRadius, 2 * cornerRadius, 2 * cornerRadius);
			
		g.setColor(i == selectedTab ? foreFocusedColor : foreColor);
			
		g.drawString(tabs[i], currentX + cornerRadius + padding, cornerRadius + padding, Graphics.LEFT | Graphics.TOP);
		
		currentX += tabsWidth[i] + margin;
	}
}

Download source code

You can download source code of TabMenu here:

Rate This

 
Bookmark this page: DeliciousDiggFacebookGoogleYahooStumbleUponRedditFurlTechnocratiMagnoliaTwitter  Share this page Share this page Print this Page Print this page Invite a friend Invite a friend
Email Newsletters Press Terms & Conditions Privacy Policy Sitemap Contact Us © 2009 Nokia 
RDF Facets: qdcZdescriptionQSxEa0E20WikiE20javaE20symbianE5fosE20s60E20maemoE20cE2bE2bE20WikiE20HomeE20WikiE20HelpE20OverviewE20GlossaryE20CreateE20PageE20ProposeE20anE20ArticleE20SpotlightE20TopicE20E2dE20WE52TE20WidgetsE20ProgrammingE20E4canguageE20E2dE20SymbianE20CE2bE2bE20E2dE20OpenE20CE2fCE2bE2bE20E2dE20JavaE20E2dE20FlashE20E4citeE20E2dE20PythonE20WebE20TechnologiesE20E2dE20WE52TE20WidgetsE20E2dE20WidSetsE20ToolsE20andE20SE44KE20CodeE20E45E78amplesE20KnowledgeE20BaseE20TechnologyE20AreasE20SoftwareE20PlatformsE20E44evelopmentE20ProcessE20E3fE3fWikiE20ChineseE20E3fE3fE3fWikiE20JapaneseE20PortugueseE2fBrazilianE20E52ussianE20WhatE20linksE20hereE20UploadE20fileE20SpecialE20pagesE20PrintableE20versionE44ownloadE20asE20PE44FE20GoE20ToE20E2eE2eE2eX qdcZidentifierQSxhttpE3aE2fE2fwikiE2eforumE2enokiaE2ecomE2findeE78E2ephpE2fMMPE5ffileX qdcZpublisherQUxhttpE3aE2fE2fswE2enokiaE2ecomE2fidE2fc764fd1cE2d8b06E2d499aE2d9a6aE2d17c3903d5a65E2fforumE5fnokiaE5fcrawlerE5fagentX qdcZtitleQSxMMPE20fileE20E2dE20ForumE20NokiaE20WikiX qdcZtypeQUqfnZE45E78cludedFromGeneralE4cistingsQ qdcZtypeQUqfnTypeZCommunityContentQ qdcZtypeQUqfnTypeZE52esourceQ qdcZtypeQUqfnTypeZWebpageQ qdcZtypeQUqfnTypeZWikiContentQ qdcZtypeQUqmarsZManagedE52esourceQ qdcZtypeQUqwebZInformationE52esourceQ qdcZtypeQUqwebZPageQ qdcZtypeQUqwebZE52esourceQ qdcZtypeQUqrdfsZE52esourceQ qrssZdescriptionQSxEa0E20WikiE20javaE20symbianE5fosE20s60E20maemoE20cE2bE2bE20WikiE20HomeE20WikiE20HelpE20OverviewE20GlossaryE20CreateE20PageE20ProposeE20anE20ArticleE20SpotlightE20TopicE20E2dE20WE52TE20WidgetsE20ProgrammingE20E4canguageE20E2dE20SymbianE20CE2bE2bE20E2dE20OpenE20CE2fCE2bE2bE20E2dE20JavaE20E2dE20FlashE20E4citeE20E2dE20PythonE20WebE20TechnologiesE20E2dE20WE52TE20WidgetsE20E2dE20WidSetsE20ToolsE20andE20SE44KE20CodeE20E45E78amplesE20KnowledgeE20BaseE20TechnologyE20AreasE20SoftwareE20PlatformsE20E44evelopmentE20ProcessE20E3fE3fWikiE20ChineseE20E3fE3fE3fWikiE20JapaneseE20PortugueseE2fBrazilianE20E52ussianE20WhatE20linksE20hereE20UploadE20fileE20SpecialE20pagesE20PrintableE20versionE44ownloadE20asE20PE44FE20GoE20ToE20E2eE2eE2eX qfnZdistributionQUxhttpE3aE2fE2fwikiE2eforumE2enokiaE2ecomE2fX qfnZtypeQUqfnTypeZCommunityContentQ qfnZtypeQUqfnTypeZE52esourceQ qfnZtypeQUqfnTypeZWebpageQ qfnZtypeQUqfnTypeZWikiContentQ qfnZupdatedQDx2008E2d10E2d02X qfnZuserE5ftagQSxfileX qfnZuserE5ftagQSxlibpathX qfnZuserE5ftagQSxmmpX qfnZuserE5ftagQSxresourceX qmarsZdescriptionQSxEa0E20WikiE20javaE20symbianE5fosE20s60E20maemoE20cE2bE2bE20WikiE20HomeE20WikiE20HelpE20OverviewE20GlossaryE20CreateE20PageE20ProposeE20anE20ArticleE20SpotlightE20TopicE20E2dE20WE52TE20WidgetsE20ProgrammingE20E4canguageE20E2dE20SymbianE20CE2bE2bE20E2dE20OpenE20CE2fCE2bE2bE20E2dE20JavaE20E2dE20FlashE20E4citeE20E2dE20PythonE20WebE20TechnologiesE20E2dE20WE52TE20WidgetsE20E2dE20WidSetsE20ToolsE20andE20SE44KE20CodeE20E45E78amplesE20KnowledgeE20BaseE20TechnologyE20AreasE20SoftwareE20PlatformsE20E44evelopmentE20ProcessE20E3fE3fWikiE20ChineseE20E3fE3fE3fWikiE20JapaneseE20PortugueseE2fBrazilianE20E52ussianE20WhatE20linksE20hereE20UploadE20fileE20SpecialE20pagesE20PrintableE20versionE44ownloadE20asE20PE44FE20GoE20ToE20E2eE2eE2eX qmarsZlanguageQUxhttpE3aE2fE2fswE2enokiaE2ecomE2flanguageE2d1E2fenX qrdfZtypeQUqfnZE45E78cludedFromGeneralE4cistingsQ qrdfZtypeQUqfnTypeZCommunityContentQ qrdfZtypeQUqfnTypeZE52esourceQ qrdfZtypeQUqfnTypeZWebpageQ qrdfZtypeQUqfnTypeZWikiContentQ qrdfZtypeQUqmarsZManagedE52esourceQ qrdfZtypeQUqwebZInformationE52esourceQ qrdfZtypeQUqwebZPageQ qrdfZtypeQUqwebZE52esourceQ qrdfZtypeQUqrdfsZE52esourceQ