Skip to content

Instantly share code, notes, and snippets.

@codenameone
codenameone / MultipartClientSample.java
Created March 2, 2016 07:03
Sample code for client/server MultipartRequest usage in Codename One
MultipartRequest request = new MultipartRequest();
request.setUrl(url);
request.addData("myFileName", fullPathToFile, "text/plain")
NetworkManager.getInstance().addToQueue(request);
@codenameone
codenameone / CSVParserSample.java
Last active July 8, 2020 09:57
Sample usage of the CSVParser class from Codename One designed to parse CSV files
Form hi = new Form("CSV Parsing", new BorderLayout());
CSVParser parser = new CSVParser();
try(Reader r = new com.codename1.io.CharArrayReader("1997,Ford,E350,\"Super, \"\"luxurious\"\" truck\"".toCharArray())) {
String[][] data = parser.parse(r);
String[] columnNames = new String[data[0].length];
for(int iter= 0 ; iter < columnNames.length ; iter++) {
columnNames[iter] = "Col " + (iter + 1);
}
TableModel tm = new DefaultTableModel(columnNames, data);
hi.add(BorderLayout.CENTER, new Table(tm));
@codenameone
codenameone / PreferencesSample.java
Created February 29, 2016 12:49
Sample code for the Codename One Preferences API
// save a token to storage
Preferences.set("token", myToken);
// get the token from storage or null if it isn't there
String token = Preferences.get("token", null);
@codenameone
codenameone / StorageManagerSample.java
Last active July 8, 2020 09:57
A simple Storage manager demo listing the files within the Codename One storage and allowing us to add/view/remove them.
public void showForm() {
Toolbar.setGlobalToolbar(true);
Form hi = new Form("Storage", new BoxLayout(BoxLayout.Y_AXIS));
hi.getToolbar().addCommandToRightBar("+", null, (e) -> {
TextField tf = new TextField("", "File Name", 20, TextField.ANY);
TextArea body = new TextArea(5, 20);
body.setHint("File Body");
Command ok = new Command("OK");
Command cancel = new Command("Cancel");
Command result = Dialog.show("File Name", BorderLayout.north(tf).add(BorderLayout.CENTER, body), ok, cancel);
public Main {
public void init(Object o) {
theme = UIManager.initFirstTheme("/theme");
// IMPORTANT: Notice we don't use MyClass.class.getName()! This won't work due to obfuscation!
Util.register("MyClass", MyClass.class);
}
public void start() {
//...
@codenameone
codenameone / ActionListenerSupportThruEventDisplatcherSample.java
Created February 29, 2016 05:08
Usage of the Codename One EventDispatcher class to fire/manager event listeners
private final EventDispatcher listeners = new EventDispatcher();
public void addActionListener(ActionListener a) {
listeners.addListener(a);
}
public void removeActionListener(ActionListener a) {
listeners.removeListener(a);
}
private void fireEvent(ActionEvent ev) {
listeners.fireActionEvent(ev);
@codenameone
codenameone / CreateMaskAdapterSample.java
Created February 28, 2016 12:53
Demonstrates the URLImage mask adapter usage in Codename One. This mask adapter will turn all downloaded images into circular images
Image roundMask = Image.createImage(placeholder.getWidth(), placeholder.getHeight(), 0xff000000);
Graphics gr = roundMask.getGraphics();
gr.setColor(0xffffff);
gr.fillArc(0, 0, placeholder.getWidth(), placeholder.getHeight(), 0, 360);
URLImage.ImageAdapter ada = URLImage.createMaskAdapter(roundMask);
Image i = URLImage.createToStorage(placeholder, "fileNameInStorage", "http://xxx/myurl.jpg", ada);
@codenameone
codenameone / ImageMaskingSample.java
Created February 28, 2016 12:23
Example for using the Image masking and camera capture API's in Codename One
Toolbar.setGlobalToolbar(true);
Form hi = new Form("Rounder", new BorderLayout());
Label picture = new Label("", "Container");
hi.add(BorderLayout.CENTER, picture);
hi.getUnselectedStyle().setBgColor(0xff0000);
hi.getUnselectedStyle().setBgTransparency(255);
Style s = UIManager.getInstance().getComponentStyle("TitleCommand");
Image camera = FontImage.createMaterial(FontImage.MATERIAL_CAMERA, s);
hi.getToolbar().addCommandToRightBar("", camera, (ev) -> {
try {
@codenameone
codenameone / ShorthandMaterialSyntax.java
Created February 28, 2016 10:17
Usage of the FontImage class with the Material design icons. This allows integrating material design icons into Codename One without any additional code
Form hi = new Form("Icon Font");
Button myButton = new Button("Save");
FontImage.setMaterialIcon(myButton, FontImage.MATERIAL_SAVE);
hi.add(myButton);
@codenameone
codenameone / MaterialIconFont.java
Created February 28, 2016 10:16
Usage of the FontImage class with the Material design icons. This allows integrating material design icons into Codename One without any additional code
Form hi = new Form("Icon Font");
Button myButton = new Button("Save");
myButton.setIcon(FontImage.createMaterial(FontImage.MATERIAL_SAVE, myButton.getUnselectedStyle()));
hi.add(myButton);