Codenameone - TextField & Keyboard - codenameone

I have problem with my TextField for getting user's input. I followed examples available (developer guide, stackoverflow etc etc), but somehow the keyboard doesn't appear. Attached is the code ( I deleted the rest of my codes for t'shooting purposes) & the screenshot.
Need help on how can I make the virtual keyboard appear.
TQ in advance.
import com.codename1.ui.Form;
import com.codename1.ui.plaf.UIManager;
import com.codename1.ui.util.Resources;
import com.codename1.ui.Toolbar;
import com.codename1.io.Log;
import com.codename1.ui.TextField;
import com.codename1.ui.layouts.BorderLayout;
import com.codename1.ui.layouts.BoxLayout;
/**
* This file was generated by Codename One for the purpose
* of building native mobile applications using Java.
*/
public class celebriesta {
private Form current;
private Resources theme;
private Form home, allEvent, specEvent, picEvent;
public void init(Object context) {
theme = UIManager.initFirstTheme("/theme");
// Enable Toolbar on all Forms by default
Toolbar.setGlobalToolbar(true);
// Pro only feature
Log.bindCrashProtection(true);
}
public void start() {
if (current != null) {
current.show();
return;
}
home = new Form("Home", BoxLayout.y());
home.setScrollableY(true);
TextField txt = new TextField();
txt.setFocusable(true);
txt.setConstraint(TextField.NUMERIC);
txt.startEditingAsync();
home.addComponent(txt);
home.show();
}
public void stop() {
current = getCurrentForm();
}
public void destroy() {
}
}

The keyboard doesn't appear in the simulator. Just use your computers keyboard to type into the text field.
When running on the device the native OS virtual keyboard will appear as expected.

Related

Error in a Hello World CodenameOne Java program

I'm trying to run a simple Hello World program with CodenameOne in Java. I'm trying to add a button to the app. I get two compiler errors, both of them:
java: cannot find symbol
symbol: class Button
Here is the code:
//ORIGIONAL CODE
package CodenameOneHelloWorld;
import static com.codename1.ui.CN.*;
import com.codename1.ui.Display;
import com.codename1.ui.Form;
import com.codename1.ui.Dialog;
import com.codename1.ui.Label;
import com.codename1.ui.plaf.UIManager;
import com.codename1.ui.util.Resources;
import com.codename1.io.Log;
import com.codename1.ui.Toolbar;
import java.io.IOException;
import com.codename1.ui.layouts.BoxLayout;
import com.codename1.io.NetworkEvent;
/**
* This file was generated by Codename One for the purpose
* of building native mobile applications using Java.
*/
public class MyApplication {
private Form current;
private Resources theme;
public void init(Object context) {
// use two network threads instead of one
updateNetworkThreadCount(2);
theme = UIManager.initFirstTheme("/theme");
// Enable Toolbar on all Forms by default
Toolbar.setGlobalToolbar(true);
// Pro only feature
Log.bindCrashProtection(true);
addNetworkErrorListener(err -> {
// prevent the event from propagating
err.consume();
if(err.getError() != null) {
Log.e(err.getError());
}
Log.sendLogAsync();
Dialog.show("Connection Error", "There was a networking error in the connection to " + err.getConnectionRequest().getUrl(), "OK", null);
});
}
public void start() {
if(current != null){
current.show();
return;
}
Form hi = new Form("Hi World", BoxLayout.y());
hi.add(new Label("Hi World"));
Button b = new Button("Show Dialog"); // LINE HAND-TYPED ACCORDING TO TUTORIAL
hi.add(b); // LINE HAND-TYPED ACCORDING TO TUTORIAL
b.addActionListener((e) -> Dialog.show("Dialog Title", "Hi", "OK", null)); // LINE HAND-TYPED ACCORDING TO TUTORIAL
hi.show();
}
public void stop() {
current = getCurrentForm();
if(current instanceof Dialog) {
((Dialog)current).dispose();
current = getCurrentForm();
}
}
public void destroy() {
}
}
After following the compiler's directions, I have the new following code:
// CODE MODIFIED BY SUGGESTION FROM COMPILER
package CodenameOneHelloWorld;
import static com.codename1.ui.CN.*;
import com.codename1.ui.Display;
import com.codename1.ui.Form;
import com.codename1.ui.Dialog;
import com.codename1.ui.Label;
import com.codename1.ui.plaf.UIManager;
import com.codename1.ui.util.Resources;
import com.codename1.io.Log;
import com.codename1.ui.Toolbar;
import java.awt.*; // ADDED IMPORT LINE
import java.io.IOException;
import com.codename1.ui.layouts.BoxLayout;
import com.codename1.io.NetworkEvent;
/**
* This file was generated by Codename One for the purpose
* of building native mobile applications using Java.
*/
public class MyApplication {
private Form current;
private Resources theme;
public void init(Object context) {
// use two network threads instead of one
updateNetworkThreadCount(2);
theme = UIManager.initFirstTheme("/theme");
// Enable Toolbar on all Forms by default
Toolbar.setGlobalToolbar(true);
// Pro only feature
Log.bindCrashProtection(true);
addNetworkErrorListener(err -> {
// prevent the event from propagating
err.consume();
if(err.getError() != null) {
Log.e(err.getError());
}
Log.sendLogAsync();
Dialog.show("Connection Error", "There was a networking error in the connection to " + err.getConnectionRequest().getUrl(), "OK", null);
});
}
public void start() {
if(current != null){
current.show();
return;
}
Form hi = new Form("Hi World", BoxLayout.y());
hi.add(new Label("Hi World"));
Button b = new Button("Show Dialog");
hi.add(String.valueOf(b)); // CHANGED LINE AS SUGGESTED BY COMPILER
b.addActionListener((e) -> Dialog.show("Dialog Title", "Hi", "OK", null));
hi.show();
}
public void stop() {
current = getCurrentForm();
if(current instanceof Dialog) {
((Dialog)current).dispose();
current = getCurrentForm();
}
}
public void destroy() {
}
}
The new code compiles clean, but when run in the simulator, the app displays an error message instead of the button.
Does anybody have any idea on how to get the button to display properly in the Hello World app?
AWT won't work here. The IDE offers more than one option when you try to import and the other option should be import com.codename1.ui.Button;. You can see the full list of supported classes in the JavaDocs here: https://www.codenameone.com/javadoc/

Codenameone BrowserComponent.setProperty giving NullPointerException

Until today, my code has been working great. However, I've just started getting NPE when using the Crisp CN1lib. It turns out that BrowserComponent.setProperty() is the culprit. Here is my stacktrace
java.lang.NullPointerException
at com.codename1.impl.javase.JavaSEPort.setBrowserProperty(JavaSEPort.java:11340)
at com.codename1.ui.BrowserComponent.setProperty(BrowserComponent.java:607)
Looks like the JavaSEPort.setBrowserProperty() is causing it. Github shows the code was edited 2 days ago so maybe something broke.
My code is pretty basic:
import static com.codename1.ui.CN.*;
import com.codename1.ui.Form;
import com.codename1.ui.Dialog;
import com.codename1.ui.plaf.UIManager;
import com.codename1.ui.util.Resources;
import com.codename1.io.Log;
import com.codename1.ui.BrowserComponent;
import com.codename1.ui.Toolbar;
import com.codename1.ui.events.ActionEvent;
import com.codename1.ui.events.ActionListener;
import com.codename1.ui.layouts.BorderLayout;
/**
* This file was generated by Codename One for the purpose
* of building native mobile applications using Java.
*/
public class MyApplication {
private Form current;
private Resources theme;
public void init(Object context) {
// use two network threads instead of one
updateNetworkThreadCount(2);
theme = UIManager.initFirstTheme("/theme");
// Enable Toolbar on all Forms by default
Toolbar.setGlobalToolbar(true);
// Pro only feature
Log.bindCrashProtection(true);
addNetworkErrorListener(err -> {
// prevent the event from propagating
err.consume();
if(err.getError() != null) {
Log.e(err.getError());
}
Log.sendLogAsync();
Dialog.show("Connection Error", "There was a networking error in the connection to " + err.getConnectionRequest().getUrl(), "OK", null);
});
}
public void start() {
if(current != null){
current.show();
return;
}
BrowserComponent browser = new BrowserComponent();
browser.addWebEventListener(BrowserComponent.onLoad, new ActionListener() {
#Override
public void actionPerformed(ActionEvent evt) {
}
});
browser.setProperty("UseWideViewPort", true);
browser.setProperty("LoadWithOverviewMode", true);
browser.setProperty("DatabaseEnabled", true);
browser.setProperty("BuiltInZoomControls", true);
browser.setProperty("DisplayZoomControls", false);
browser.setProperty("WebContentsDebuggingEnabled", true);
browser.setFireCallbacksOnEdt(true);
browser.setURL("https://www.instagram.com/brianabette/");
Form hi = new Form("Hi World", new BorderLayout());
hi.add(BorderLayout.CENTER, browser);
hi.show();
}
public void stop() {
current = getCurrentForm();
if(current instanceof Dialog) {
((Dialog)current).dispose();
current = getCurrentForm();
}
}
public void destroy() {
}
}
Any pointers? Bug maybe?
We just made some performance improvements to BrowserComponent. Looks like we missed a spot here, so there is a regression. It is already fixed in Git and will be included in the next update next Friday.

MapContainer show only on click/touch

I am trying to use naive map.
I have places a MapContainer insider a Form Component but the From shows blank without the map inside it, on the simulator. When I click on the viewport the map shows as long as the mouse button remains down. When I release it disappear again.
Is it a real problem or is it a misfunction of the simulator? If it is a real problem what am I doing wrong?
below is the class I am using:
package com.mainsys.zappeion;
import com.codename1.googlemaps.MapContainer;
import com.codename1.maps.Coord;
import com.codename1.ui.Form;
import com.codename1.ui.layouts.BorderLayout;
/**
*
* #author Christoforos
*/
public class ZappeionMap extends com.codename1.ui.Form {
private Form current;
public ZappeionMap() {
super("Ζάππειον", new BorderLayout());
}
#Override
public void show() {
if(current != null){
current.show();
return;
}
final MapContainer cnt = new MapContainer();
this.addComponent(BorderLayout.CENTER, cnt);
cnt.setCameraPosition(new Coord(41.889, -87.622));
super.show();
}
}
/********** Implementing Shai's answer ******************/
I changed my code to what Shai suggested show my class now is:
package com.mainsys.zappeion;
import com.codename1.googlemaps.MapContainer;
import com.codename1.location.Location;
import com.codename1.location.LocationManager;
import com.codename1.maps.Coord;
import com.codename1.ui.BrowserComponent;
import com.codename1.ui.FontImage;
import com.codename1.ui.Form;
import com.codename1.ui.layouts.BorderLayout;
import com.codename1.ui.plaf.Style;
/**
*
* #author Christoforos
*/
public class ZappeionMap extends com.codename1.ui.Form {
private Form current;
private static final String HTML_API_KEY = "AIzaSyDHlFJK561bQVs0AyBm1M5xWS_YCHNuPfc";
public ZappeionMap() {
super("Ζάππειον", new BorderLayout());
final MapContainer cnt = new MapContainer( HTML_API_KEY );
this.addComponent(BorderLayout.CENTER, cnt);
cnt.setCameraPosition(new Coord(41.889, -87.622));
}
}
It still having the same problem. The screen is blank. The map only shown when I click on the screen.
I also noticed something else. On the debuger I get the message:
WARNING: Apple will no longer accept http URL connections from applications you tried to connect to http://tile.openstreetmap.org/4/2/9.png to learn more check out https://www.codenameone.com/blog/ios-http-urls.html
Why is it trying to connect to http://tile.openstreetmao.org. It is supposed to work with google maps not with openstreet maps.
One more information, maybe it is worh something. I test it on real device. The screen is still blink but when I touch the screen it does not show anythink, in contrast with the simulater that when I click on the screen the map appears.
I am using netbeans 8.2 on centos 7
Any help is appreciated.
Thank you Christoforos.
Why are you overriding the show method of form and don't construct the UI n the constructor?
It looks like you copied some code from the lifecycle class and mixed it with a form subclass e.g. the current variable.
This is closer to correct:
public class ZappeionMap extends com.codename1.ui.Form {
private Form current;
public ZappeionMap() {
super("Ζάππειον", new BorderLayout());
final MapContainer cnt = new MapContainer();
this.addComponent(BorderLayout.CENTER, cnt);
cnt.setCameraPosition(new Coord(41.889, -87.622));
}
}

Open gallery and display selected picture back Codename One

IDE:
Eclipse
Desktop Windows 7
Simulator Nexus 5
Device
I want to open the gallery of the device and display the image selected bythe user back. I make a button and and an ActionListener should deflect me to the device's gallery. But the simulator shows a blank screen even if I omit the opening of the gallery part and just add the button. Also, It gives the following exception in the log:-
Jul 20, 2017 4:11:00 PM java.util.prefs.WindowsPreferences <init>
WARNING: Could not open/create prefs root node Software\JavaSoft\Prefs at root 0x80000002. Windows RegCreateKeyEx(...) returned error code 5.
java.io.UTFDataFormatException: malformed input around byte 64
at java.io.DataInputStream.readUTF(Unknown Source)
at java.io.DataInputStream.readUTF(Unknown Source)
at com.codename1.ui.util.Resources.loadTheme(Resources.java:1270)
at com.codename1.ui.util.Resources.openFileImpl(Resources.java:303)
at com.codename1.ui.util.Resources.openFile(Resources.java:269)
at com.codename1.ui.util.Resources.<init>(Resources.java:189)
at com.codename1.ui.util.Resources.open(Resources.java:768)
at com.codename1.ui.util.Resources.open(Resources.java:688)
at com.codename1.impl.javase.JavaSEPort$4.run(JavaSEPort.java:1720)
at com.codename1.ui.Display.processSerialCalls(Display.java:1056)
at com.codename1.ui.Display.mainEDTLoop(Display.java:873)
at com.codename1.ui.RunnableWrapper.run(RunnableWrapper.java:120)
at com.codename1.impl.CodenameOneThread.run(CodenameOneThread.java:176)
The following is my main java file made on an empty new bare bone project:-
package com.mycompany.myapp;
import com.codename1.ui.Display;
import com.codename1.ui.Form;
import com.codename1.ui.Image;
import com.codename1.ui.Button;
import com.codename1.ui.Dialog;
import com.codename1.ui.Label;
import com.codename1.ui.plaf.UIManager;
import com.codename1.ui.util.Resources;
import com.codename1.components.ImageViewer;
import com.codename1.io.Log;
import com.codename1.media.MediaManager;
import com.codename1.ui.Toolbar;
import com.codename1.ui.events.ActionEvent;
import com.codename1.ui.events.ActionListener;
import java.io.IOException;
/**
* This file was generated by Codename One for the purpose
* of building native mobile applications using Java.
*/
public class MyApplication {
private Form current;
private Resources theme;
public void init(Object context) {
theme = UIManager.initFirstTheme("/theme");
// Enable Toolbar on all Forms by default
Toolbar.setGlobalToolbar(true);
// Pro only feature, uncomment if you have a pro subscription
// Log.bindCrashProtection(true);
}
public void start() {
if(current != null){
current.show();
return;
}
Form hi = new Form("Hi World");
hi.addComponent(new Label("Hi World"));
Button gallery = new Button("Browse");
hi.add(gallery);
gallery.addActionListener(new ActionListener<ActionEvent>() {
#Override
public void actionPerformed(ActionEvent evt) {
// TODO Auto-generated method stub
Display.getInstance().openGallery((e) -> {
if(e != null && e.getSource() != null) {
// String file = (String)e.getSource();
// try {
// Label path = new Label(file);
// hi.add(path);
//
// } catch(Exception err) {
// Log.e(err);
// }
}
}, Display.GALLERY_IMAGE);
}
});
}
public void stop() {
current = Display.getInstance().getCurrent();
if(current instanceof Dialog) {
((Dialog)current).dispose();
current = Display.getInstance().getCurrent();
}
}
public void destroy() {
}
}
It seems you have several issues. The first is a preferences issue which you should fix with your JDK install:
Resolving the problem The work around is to login as the administrator
and create the key HKEY_LOCAL_MACHINE\Software\JavaSoft\Prefs
From Java: java.util.Preferences Failing
Next it seems your resource file is corrupted. It's hard to tell what went wrong there... Is it 0 length?
Check out this article for tracking designer tool issues.
A large image from the camera will take some time to process on some devices. You can capture a smaller image (see the various capture methods) but I'm guessing that you just didn't revalidate after adding to the ui and incorrectly assumed this is slow.

BrowserComponent doesn't work on CodenameOne 3.7.2?

the following code doesn't work in the Simulator (on NetBeans 8.2 + CodenameOne 3.7.2 + Skin Iphone3gs) and doesn't work in my Android 4.1.2 device. It blocks the Simulator and it shows a blank screen on the real device (with a blinking "Loading..."). What's the problem? Thank you
import static com.codename1.ui.CN.*;
import com.codename1.ui.Display;
import com.codename1.ui.Form;
import com.codename1.ui.Dialog;
import com.codename1.ui.Label;
import com.codename1.ui.plaf.UIManager;
import com.codename1.ui.util.Resources;
import com.codename1.io.Log;
import com.codename1.ui.BrowserComponent;
import com.codename1.ui.Toolbar;
import com.codename1.ui.layouts.BorderLayout;
import java.io.IOException;
import com.codename1.ui.layouts.BoxLayout;
/**
* This file was generated by Codename One for the purpose
* of building native mobile applications using Java.
*/
public class Gmail {
private Form current;
private Resources theme;
public void init(Object context) {
theme = UIManager.initFirstTheme("/theme");
// Enable Toolbar on all Forms by default
Toolbar.setGlobalToolbar(true);
// Pro only feature
Log.bindCrashProtection(true);
}
public void start() {
if(current != null){
current.show();
return;
}
Form hi = new Form("Form title Example", new BorderLayout());
BrowserComponent browser = new BrowserComponent();
browser.setURL("https://students.uninettunouniversity.net/");
hi.add(BorderLayout.CENTER, browser);
}
public void stop() {
current = getCurrentForm();
if(current instanceof Dialog) {
((Dialog)current).dispose();
current = getCurrentForm();
}
}
public void destroy() {
}
}
You deleted the hi.show() line at the end

Resources