I am Deborah.
Check out my work
or read my blog.

Wicket-Bootstrap – Navbar and Pageparameters – setting the active item

Apache Wicket is a Java-Framework to create web applications. Wicket-Bootstrap combines it with Bootstrap to ease the process of GUI implementation.

Navbars in Wicket-Bootstrap

You can add a Navbar to your page with just a few Java lines:

Navbar navbar = new Navbar("navbar");

navbar.addComponents(NavbarComponents.transform(
	Navbar.ComponentPosition.LEFT,
	new NavbarButton(ExamplePage1.class, "page1")));
navbar.addComponents(NavbarComponents.transform(
	Navbar.ComponentPosition.LEFT,
	new NavbarButton(ExamplePage2.class, "page2")));
navbar.addComponents(NavbarComponents.transform(
	Navbar.ComponentPosition.LEFT,
	new NavbarButton(ExamplePage3.class, "page3")));

add(navbar);

The corresponding HTML line in the page would be

<div wicket:id="navbar"></div>

You can click the buttons in the Navbar and the active page will be highlighted automatically.

Navbar and PageParameters

So far, so good. I stumbled when I tried to implement navigations generated by Lists. All Links call the same page and chance the content on the page depending on the PageParameters. The Java code would look like this:

Navbar navbar = new Navbar("navbar");

List pages = new ArrayList();
pages.add("page1");
pages.add("page2");
pages.add("page3");
		
for (final String page : pages) {
	PageParameters params = new PageParameters();
	params.set("page", page);
	
	navbar.addComponents(NavbarComponents.transform(
		Navbar.ComponentPosition.LEFT,
		new NavbarButton(ExamplePage.class, params, Model.of(page))));

}

add(navbar);

In this example all Navbar buttons get active and highlighted when I click on one button, because they all link to the same page.

Solution: Highlight active item depending on PageParameters

In order to change this, I had to add the following Override function:

navbar.addComponents(NavbarComponents.transform(
	Navbar.ComponentPosition.LEFT,
	new NavbarButton(ExamplePage.class, params, Model.of(page)){

		@Override
		public boolean isActive(final Component button) {
			return super.isActive(button) && 
		           button.getPage().getPageParameters()
		           .equals(getPageParameters());
		}
	}
));

Say something

Your email address will not be published.

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong>

Posts from "Blog":

How to print (almost) invisible IR markers using a standard printerQuick, Draw! Generic drawings part 2The Art of CodingImperfect MandalasAutumn LeafsColor Explosion #25 years of KAZOOSH!Color explosionUbuntu 15.10, MongoDB and node.jsKaleidoscope of Fencinggit cheat sheetFractalsofxSortableListCrocket: hairband with jeans buttonWall painting for Marias birthdayLinux / Ubuntu + Serato Vinyl Control System + Pure DataWicket-Bootstrap – Navbar and Pageparameters – setting the active itemLinux Console SnippetsCrawling Traces @German award for multimedia mb21Crawling Traces History ViewerStreaming new.livestream.com with VLC and UbuntuCrawling Traces at Hechtfest / hechtgruenLinks: Data Security, Social Networks and Secure MailingFix: VMware Player crash on Ubuntu 13.04Xubuntu: Fix for Dockbarx bug not starting Thunar or TerminalUbuntu 12.10, libwebsockets and OFSetting up Meteor with Ubuntu 12.10 on an NTFS share partitionWebsite: KAZOOSH!Intermediate IsolinesThe barn door gets coloredSkype on Ubuntu 10.10 or 11.04 64bitElitebook 8440p vs UbuntuKryptografie & Datensicherheit – NotizenRandom Compilation IIRandom Compilation IAll You Can Eat in DresdenWebauftritt: ESE 2010