Last active
July 8, 2020 09:57
-
-
Save codenameone/dce6598a226aaf9a3157 to your computer and use it in GitHub Desktop.
Search a set of components dynamically right from the title of a Codename One application
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Toolbar.setGlobalToolbar(true); | |
Style s = UIManager.getInstance().getComponentStyle("Title"); | |
Form hi = new Form("Toolbar", new BoxLayout(BoxLayout.Y_AXIS)); | |
TextField searchField = new TextField("", "Toolbar Search"); // <1> | |
searchField.getHintLabel().setUIID("Title"); | |
searchField.setUIID("Title"); | |
searchField.getAllStyles().setAlignment(Component.LEFT); | |
hi.getToolbar().setTitleComponent(searchField); | |
FontImage searchIcon = FontImage.createMaterial(FontImage.MATERIAL_SEARCH, s); | |
searchField.addDataChangeListener((i1, i2) -> { // <2> | |
String t = searchField.getText(); | |
if(t.length() < 1) { | |
for(Component cmp : hi.getContentPane()) { | |
cmp.setHidden(false); | |
cmp.setVisible(true); | |
} | |
} else { | |
t = t.toLowerCase(); | |
for(Component cmp : hi.getContentPane()) { | |
String val = null; | |
if(cmp instanceof Label) { | |
val = ((Label)cmp).getText(); | |
} else { | |
if(cmp instanceof TextArea) { | |
val = ((TextArea)cmp).getText(); | |
} else { | |
val = (String)cmp.getPropertyValue("text"); | |
} | |
} | |
boolean show = val != null && val.toLowerCase().indexOf(t) > -1; | |
cmp.setHidden(!show); // <3> | |
cmp.setVisible(show); | |
} | |
} | |
hi.getContentPane().animateLayout(250); | |
}); | |
hi.getToolbar().addCommandToRightBar("", searchIcon, (e) -> { | |
searchField.startEditingAsync(); // <4> | |
}); | |
hi.add("A Game of Thrones"). | |
add("A Clash Of Kings"). | |
add("A Storm Of Swords"). | |
add("A Feast For Crows"). | |
add("A Dance With Dragons"). | |
add("The Winds of Winter"). | |
add("A Dream of Spring"); | |
hi.show(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Sample usage of Toolbar & TextField.
From the Codename One project