Skip to content

Instantly share code, notes, and snippets.

@codenameone
codenameone / MediaPlayerSample.java
Created February 13, 2016 20:20
Demo of the MediaPlayer class from Codename One
final Form hi = new Form("MediaPlayer", new BorderLayout());
hi.setToolbar(new Toolbar());
Style s = UIManager.getInstance().getComponentStyle("Title");
FontImage icon = FontImage.createMaterial(FontImage.MATERIAL_VIDEO_LIBRARY, s);
hi.getToolbar().addCommandToRightBar(new Command("", icon) {
@Override
public void actionPerformed(ActionEvent evt) {
Display.getInstance().openGallery((e) -> {
if(e != null && e.getSource() != null) {
String file = (String)e.getSource();
@codenameone
codenameone / TabsCarouselSample.java
Created February 12, 2016 08:14
Sample covering building a Carousel effect using the Tabs component in Codename One
Form hi = new Form("Swipe Tabs", new LayeredLayout());
Tabs t = new Tabs();
t.hideTabs();
Style s = UIManager.getInstance().getComponentStyle("Button");
FontImage radioEmptyImage = FontImage.createMaterial(FontImage.MATERIAL_RADIO_BUTTON_UNCHECKED, s);
FontImage radioFullImage = FontImage.createMaterial(FontImage.MATERIAL_RADIO_BUTTON_CHECKED, s);
((DefaultLookAndFeel)UIManager.getInstance().getLookAndFeel()).setRadioButtonImages(radioFullImage, radioEmptyImage, radioFullImage, radioEmptyImage);
Container container1 = BoxLayout.encloseY(new Label("Swipe the tab to see more"),
@codenameone
codenameone / TabsSample.java
Created February 12, 2016 08:09
Simple Tabs component sample from Codename One
Form hi = new Form("Tabs", new BorderLayout());
Tabs t = new Tabs();
Style s = UIManager.getInstance().getComponentStyle("Tab");
FontImage icon1 = FontImage.createMaterial(FontImage.MATERIAL_QUESTION_ANSWER, s);
Container container1 = BoxLayout.encloseY(new Label("Label1"), new Label("Label2"));
t.addTab("Tab1", icon1, container1);
t.addTab("Tab2", new SpanLabel("Some text directly in the tab"));
@codenameone
codenameone / ShareButtonSample.java
Created February 11, 2016 20:51
Sample showing off the share button in Codename One and a beavy of other API's
Form hi = new Form("ShareButton");
ShareButton sb = new ShareButton();
sb.setText("Share Screenshot");
hi.add(sb);
Image screenshot = Image.createImage(hi.getWidth(), hi.getHeight());
hi.revalidate();
hi.setVisible(true);
hi.paintComponent(screenshot.getGraphics(), true);
@codenameone
codenameone / XMLTreeSample.java
Created February 11, 2016 11:52
Sample showing off XML Tree UI and XML parsing in Codename One
class XMLTreeModel implements TreeModel {
private Element root;
public XMLTreeModel(Element e) {
root = e;
}
public Vector getChildren(Object parent) {
if(parent == null) {
Vector c = new Vector();
c.addElement(root);
@codenameone
codenameone / TreeSample.java
Last active July 8, 2020 09:57
Simple Tree Model and Tree component from Codename One
class StringArrayTreeModel implements TreeModel {
String[][] arr = new String[][] {
{"Colors", "Letters", "Numbers"},
{"Red", "Green", "Blue"},
{"A", "B", "C"},
{"1", "2", "3"}
};
public Vector getChildren(Object parent) {
if(parent == null) {
@codenameone
codenameone / TableSample.java
Last active July 8, 2020 09:57
Sample for using the Table API with pinstripes, spanning and custom editors for a cell
Form hi = new Form("Table", new BorderLayout());
TableModel model = new DefaultTableModel(new String[] {"Col 1", "Col 2", "Col 3"}, new Object[][] {
{"Row 1", "Row A", "Row X"},
{"Row 2", "Row B can now stretch", null},
{"Row 3", "Row C", "Row Z"},
{"Row 4", "Row D", "Row K"},
}) {
public boolean isCellEditable(int row, int col) {
return col != 0;
}
@codenameone
codenameone / MultiListSample.java
Last active July 8, 2020 09:57
Sample for creating a simple MultiList UI in Codename One
public void showForm() {
Form hi = new Form("MultiList", new BorderLayout());
int mm = Display.getInstance().convertToPixels(3);
EncodedImage placeholder = EncodedImage.createFromImage(Image.createImage(mm * 3, mm * 4, 0), false);
Image icon1 = URLImage.createToStorage(placeholder, "icon1", "http://www.georgerrmartin.com/wp-content/uploads/2013/03/GOTMTI2.jpg");
Image icon2 = URLImage.createToStorage(placeholder, "icon2", "http://www.georgerrmartin.com/wp-content/uploads/2012/08/clashofkings.jpg");
Image icon3 = URLImage.createToStorage(placeholder, "icon3", "http://www.georgerrmartin.com/wp-content/uploads/2013/03/stormswordsMTI.jpg");
Image icon4 = URLImage.createToStorage(placeholder, "icon4", "http://www.georgerrmartin.com/wp-content/uploads/2012/08/feastforcrows.jpg");
Image icon5 = URLImage.createToStorage(placeholder, "icon5", "http://georgerrmartin.com/gallery/art/dragons05.jpg");
@codenameone
codenameone / GRMMModel.java
Created February 10, 2016 20:36
A Codename One ListModel that displays a million GRMM books
class GRMMModel implements ListModel<Map<String,Object>> {
@Override
public Map<String, Object> getItemAt(int index) {
int idx = index % 7;
switch(idx) {
case 0:
return createListEntry("A Game of Thrones " + index, "1996");
case 1:
return createListEntry("A Clash Of Kings " + index, "1998");
case 2:
@codenameone
codenameone / ListCellRendererSample.java
Last active July 8, 2020 09:57
A trivial sample of the list cell renderer in Codename One
class MyYesNoRenderer extends Label implements ListCellRenderer {
Label label = new Label(" ");
public Component getListCellRendererComponent(List list, Object value, int index, boolean isSelected) {
if( ((Boolean)value).booleanValue() ) {
setText("Yes");
} else {
setText("No");
}
return this;
}