Skip to content

Instantly share code, notes, and snippets.

@codenameone
codenameone / GenericListCellRendererSample.java
Last active July 8, 2020 09:57
Sample of usage for Codename One's GenericListCellRenderer
public void showForm() {
com.codename1.ui.List list = new com.codename1.ui.List(createGenericListCellRendererModelData());
list.setRenderer(new GenericListCellRenderer(createGenericRendererContainer(), createGenericRendererContainer()));
Form hi = new Form("GenericListCellRenderer", new BorderLayout());
hi.add(BorderLayout.CENTER, list);
hi.show();
}
private Container createGenericRendererContainer() {
@codenameone
codenameone / InfiniteContainerSample.java
Last active July 8, 2020 09:57
Sample Usage of InfiniteContainer from Codename One fetches data dynamically from a webservice to scroll down infinitely
public void showForm() {
Form hi = new Form("InfiniteContainer", new BorderLayout());
Style s = UIManager.getInstance().getComponentStyle("MultiLine1");
FontImage p = FontImage.createMaterial(FontImage.MATERIAL_PORTRAIT, s);
EncodedImage placeholder = EncodedImage.createFromImage(p.scaled(p.getWidth() * 3, p.getHeight() * 3), false);
InfiniteContainer ic = new InfiniteContainer() {
@Override
public Component[] fetchComponents(int index, int amount) {
@codenameone
codenameone / InfiniteScrollAdapterSample.java
Last active July 8, 2020 09:57
Sample Usage of InfiniteScrollAdapter from Codename One fetches data dynamically from a webservice to scroll down infinitely
public void showForm() {
Form hi = new Form("InfiniteScrollAdapter", new BoxLayout(BoxLayout.Y_AXIS));
Style s = UIManager.getInstance().getComponentStyle("MultiLine1");
FontImage p = FontImage.createMaterial(FontImage.MATERIAL_PORTRAIT, s);
EncodedImage placeholder = EncodedImage.createFromImage(p.scaled(p.getWidth() * 3, p.getHeight() * 3), false);
InfiniteScrollAdapter.createInfiniteScroll(hi.getContentPane(), () -> {
java.util.List<Map<String, Object>> data = fetchPropertyData("Leeds");
MultiButton[] cmps = new MultiButton[data.size()];
@codenameone
codenameone / FetchFromPropertyCross.java
Created February 10, 2016 07:08
This is a simple block of code used by some samples in Codename One it fetches housing listings
int pageNumber = 1;
java.util.List<Map<String, Object>> fetchPropertyData(String text) {
try {
ConnectionRequest r = new ConnectionRequest();
r.setPost(false);
r.setUrl("http://api.nestoria.co.uk/api");
r.addArgument("pretty", "0");
r.addArgument("action", "search_listings");
r.addArgument("encoding", "json");
r.addArgument("listing_type", "buy");
@codenameone
codenameone / CheckBoxAndRadioButtonSample.java
Created February 8, 2016 13:23
Simple sample of using RadioButton & CheckBox in Codename One
CheckBox cb1 = new CheckBox("CheckBox No Icon");
cb1.setSelected(true);
CheckBox cb2 = new CheckBox("CheckBox With Icon", icon);
CheckBox cb3 = new CheckBox("CheckBox Opposite True", icon);
CheckBox cb4 = new CheckBox("CheckBox Opposite False", icon);
cb3.setOppositeSide(true);
cb4.setOppositeSide(false);
RadioButton rb1 = new RadioButton("Radio 1");
RadioButton rb2 = new RadioButton("Radio 2");
RadioButton rb3 = new RadioButton("Radio 3", icon);
@codenameone
codenameone / LabelAlignmentSample.java
Created February 8, 2016 09:22
Label icon/text alignment in Codename One
Label left = new Label("Left", icon);
left.setTextPosition(Component.LEFT);
Label right = new Label("Right", icon);
right.setTextPosition(Component.RIGHT);
Label bottom = new Label("Bottom", icon);
bottom.setTextPosition(Component.BOTTOM);
Label top = new Label("Top", icon);
top.setTextPosition(Component.TOP);
hi.add(left).add(right).add(bottom).add(top);
@codenameone
codenameone / AnimateUnlayoutSample.java
Last active July 8, 2020 09:57
Demonstrates the unlayout API in Codename One layout animations
Form hi = new Form("Layout Animations", new BoxLayout(BoxLayout.Y_AXIS));
Button fall = new Button("Fall");
fall.addActionListener((e) -> {
if(hi.getContentPane().getComponentCount() == 1) {
fall.setText("Rise");
for(int iter = 0 ; iter < 10 ; iter++) {
Label b = new Label ("Label " + iter);
b.setWidth(fall.getWidth());
b.setHeight(fall.getHeight());
b.setY(-fall.getHeight());
@codenameone
codenameone / FallAnimation.java
Created January 31, 2016 12:23
Animates a "falling" effect using animate layout from Codename One
Form hi = new Form("Layout Animations", new BoxLayout(BoxLayout.Y_AXIS));
Button fall = new Button("Fall");
fall.addActionListener((e) -> {
for(int iter = 0 ; iter < 10 ; iter++) {
Label b = new Label ("Label " + iter);
b.setWidth(fall.getWidth());
b.setHeight(fall.getHeight());
b.setY(-fall.getHeight());
hi.add(b);
}
@codenameone
codenameone / MigLayoutSample.java
Created January 31, 2016 06:47
Sample MigLayout Codename One usage
Form hi = new Form("MigLayout", new MigLayout("fillx,insets 0"));
hi.add(new Label("First")).
add("span 2 2", new Label("Second")). // The component will span 2x2 cells.
add("wrap", new Label("Third")). // Wrap to next row
add(new Label("Forth")).
add("wrap", new Label("Fifth")). // Note that it "jumps over" the occupied cells.
add(new Label("Sixth")).
add(new Label("Seventh"));
hi.show();
@codenameone
codenameone / GroupLayoutSample.java
Created January 30, 2016 21:40
Sample using the group layout in Codename One generated by the NetBeans GUI builder
Form hi = new Form("GroupLayout");
Label label1 = new Label();
Label label2 = new Label();
Label label3 = new Label();
Label label4 = new Label();
Label label5 = new Label();
Label label6 = new Label();
Label label7 = new Label();