The following example demonstrates use of the ListButton component. Selecting an image name from the drop-down shows the corresponding image file in the image view on the right.
The WTKX source for the example is below. Like the previous example, list data is specified as an attribute containing a JSON array of strings; this value is used to load the image displayed to the right of the list button. Like ListView, ListButtons can also be populated programmatically using instances of ListItem.
<Window title="List Buttons" maximized="true" xmlns:wtkx="http://pivot.apache.org/wtkx" xmlns="org.apache.pivot.wtk"> <content> <TablePane styles="{showHorizontalGridLines: true, showVerticalGridLines:true, horizontalSpacing:1, verticalSpacing:1}"> <columns> <TablePane.Column width="-1"/> <TablePane.Column width="1*"/> </columns> <rows> <TablePane.Row height="340"> <BoxPane orientation="vertical" styles="{verticalAlignment:'top', padding: 4}"> <Label text="Picture:"/> <ListButton wtkx:id="listButton" listData="['IMG_0725_2.jpg', 'IMG_0735_2.jpg', 'IMG_0767_2.jpg']"/> </BoxPane> <ImageView wtkx:id="imageView" styles="{backgroundColor:'#404040'}"/> </TablePane.Row> </rows> </TablePane> </content> </Window>
The Java source for the example is below:
package org.apache.pivot.tutorials.lists; import java.net.URL; import org.apache.pivot.collections.Map; import org.apache.pivot.util.ThreadUtilities; import org.apache.pivot.util.concurrent.TaskExecutionException; import org.apache.pivot.wtk.Application; import org.apache.pivot.wtk.ApplicationContext; import org.apache.pivot.wtk.DesktopApplicationContext; import org.apache.pivot.wtk.Display; import org.apache.pivot.wtk.ImageView; import org.apache.pivot.wtk.ListButton; import org.apache.pivot.wtk.ListButtonSelectionListener; import org.apache.pivot.wtk.Window; import org.apache.pivot.wtk.media.Image; import org.apache.pivot.wtkx.WTKXSerializer; public class ListButtons implements Application { private Window window = null; private ListButton listButton = null; private ImageView imageView = null; private ListButtonSelectionListener listButtonSelectionListener = new ListButtonSelectionListener() { public void selectedIndexChanged(ListButton listButton, int previousIndex) { int index = listButton.getSelectedIndex(); if (index != -1) { String item = (String)listButton.getListData().get(index); // Get the image URL for the selected item ClassLoader classLoader = ThreadUtilities.getClassLoader(); URL imageURL = classLoader.getResource("org/apache/pivot/tutorials/" + item); // If the image has not been added to the resource cache yet, // add it Image image = (Image)ApplicationContext.getResourceCache().get(imageURL); if (image == null) { try { image = Image.load(imageURL); } catch (TaskExecutionException exception) { throw new RuntimeException(exception); } ApplicationContext.getResourceCache().put(imageURL, image); } // Update the image imageView.setImage(image); } } }; public void startup(Display display, Map<String, String> properties) throws Exception { WTKXSerializer wtkxSerializer = new WTKXSerializer(); window = (Window)wtkxSerializer.readObject(this, "list_buttons.wtkx"); listButton = (ListButton)wtkxSerializer.get("listButton"); imageView = (ImageView)wtkxSerializer.get("imageView"); listButton.getListButtonSelectionListeners().add(listButtonSelectionListener); listButton.setSelectedIndex(0); window.open(display); } public boolean shutdown(boolean optional) { if (window != null) { window.close(); } return false; } public void suspend() { } public void resume() { } public static void main(String[] args) { DesktopApplicationContext.main(ListButtons.class, args); } }
Note that this example makes use of the global resource cache to store the loaded images. This cache can be used to store any kind of application-specific data that may be expensive to load. Entries are keyed by the URL from which they were retrieved, and, once placed in the cache, are available to all code within the application. When the list button's selection changes, the application first looks for an image in the resource cache; if it is not found, it is loaded and added to the cache. It is then set as the "image" property of the image view and displayed.
Next: Text