Codename One Developer Guide part 1

Page 209

The Components Of Codename One Then we invoke Label.getAllStyles().setBgTransparency(100) to give the selection semi transparency, and 0 for full transparency if not selected. That is still not very efficient because we create a new Label each time the method is invoked. To make the code tighter, keep a reference to the Component or extend it as 65 DefaultListCellRenderer does. class MyRenderer extends Label implements ListCellRenderer { public Component getListCellRendererComponent(List list, Object value, int index, boolean isSelected){ setText(value.toString()); if (isSelected) { setFocus(true); getAllStyles().setBgTransparency(100); } else { setFocus(false); getAllStyles().setBgTransparency(0); } return this; } } }

Now Let’s have a look at a more advanced Renderer. class ContactsRenderer extends Container implements ListCellRenderer { private Label name = new Label(""); private Label email = new Label(""); private Label pic = new Label(""); private Label focus = new Label(""); public ContactsRenderer() { setLayout(new BorderLayout()); addComponent(BorderLayout.WEST, pic); Container cnt = new Container(new BoxLayout(BoxLayout.Y_AXIS)); name.getAllStyles().setBgTransparency(0); name.getAllStyles().setFont(Font.createSystemFont(Font.FACE_SYSTEM, Font.STYLE_BOLD, Font.SIZE_MEDIUM)); 65

https://www.codenameone.com/javadoc/com/codename1/ui/list/DefaultListCellRenderer.html

192


Issuu converts static files into: digital portfolios, online yearbooks, online catalogs, digital photo albums and more. Sign up and create your flipbook.