I'm missing some imports on the CodenameOne Hellp World program - codenameone

I think I'm missing some imports, especially importing Button for the CodenameOne Hello World program from the CodenameOne.com webpage. Please tell me what I need to add to this program to put the button on the program and react to a button click. The program compiles and runs, but it does not display the button.
Here is the code:
package CodenameOneHelloWorld;
import static com.codename1.ui.CN.*;
import com.codename1.ui.*;
import com.codename1.ui.plaf.UIManager;
import com.codename1.ui.util.Resources;
import com.codename1.io.Log;
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"));
hi.show();
Button b = new Button("Show Dialog");
hi.add(b);
b.addActionListener(e -> Dialog.show("Dialog Title", "Hi", "OK", null));
}
public void stop() {
current = getCurrentForm();
if(current instanceof Dialog) {
((Dialog)current).dispose();
current = getCurrentForm();
}
}
public void destroy() {
}
}

If you forgot some imports, the code would not compile.
The Button is not shown because you added it after hi.show().
To make your code working, you have two ways. The first is to add hi.revalidate() at the end, like this:
Form hi = new Form("Hi World", BoxLayout.y());
hi.add(new Label("Hi World"));
hi.show();
Button b = new Button("Show Dialog");
hi.add(b);
b.addActionListener(e -> Dialog.show("Dialog Title", "Hi", "OK", null));
hi.revalidate();
The second way is to add the button before hi.show(), like this:
Form hi = new Form("Hi World", BoxLayout.y());
hi.add(new Label("Hi World"));
Button b = new Button("Show Dialog");
hi.add(b);
b.addActionListener(e -> Dialog.show("Dialog Title", "Hi", "OK", null));
hi.show();
The reason is explained in the section "7.1. Layout Reflow" of the developer guide (https://www.codenameone.com/developer-guide.html):
«Layout in tools such as HTML is implicit, when you add something into
the UI it is automatically placed correctly. Other tools such as
Codename One use explicit layout, that means you have to explicitly
request the UI to layout itself after making changes! [...] When
adding a component to a UI that is already visible, the component will
not show by default. [...] That is why, when you add components to a
form that is already showing, you should invoke revalidate() or
animate the layout appropriately. [...]»

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/

trouble with Theme in the olderGUI

i´m starting with CN1, and i traying with Todo App example. When i run de app in Netbeans, only appears a blank form, I changed the themes, I added a jpg image and made others changes in the res file with the olderGUI Builder and use the function theme = UIManager.initNamedTheme("/theme","Theme 2");, but i don´t have any change in the dorm when i try to simulate in Netbeans.
This is the complete code in the TodoApp java file, I comment the part of the new hi Form (I use the "Hi World" bare bones template):
public class TodoApp {
private Form current;
private Resources theme;
public void init(Object context) {
// use two network threads instead of one
updateNetworkThreadCount(2);
//theme = UIManager.initFirstTheme("/theme");
theme = UIManager.initNamedTheme("/theme","Theme 2");
// 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"));
//hi.show();
new TodoForm();
}
public void stop() {
current = getCurrentForm();
if(current instanceof Dialog) {
((Dialog)current).dispose();
current = getCurrentForm();
}
}
public void destroy() {
}
}
I couldn't find what i do wrong. Thanks!!!
P.D. Sorry for my English, I learning it too.
We recommend avoiding the old GUI builder. It's no longer supported and wasn't designed for modern mobile apps. In order to use it you have to create a special type of project (old GUI project) and only that type of project works with it.
The new GUI builder works better, it has a more powerful layout system and works in a way that's more consistent with modern GUI builders. See https://www.codenameone.com/blog/tutorial-gui-builder-autolayout-signin-form-responsive.html

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.

Side menu issue with toastbar

I've a toastbar in a screen. When the sidemenu is opened as soon as toastbar appears, the sidemenu fluctuates a bit and don't work unless back btn is clicked. Normally the sidemenu closes if the screen is touched but it doesn't here.
Plz see the video here.
https://youtu.be/3zRN26FTij4
Update:
public void start() {
if (current != null) {
current.show();
return;
}
Form hi = new Form("Hi World", BoxLayout.y());
hi.add(new Label("Hi World"));
hi.show();
Command cmd = new Command("home");
hi.getToolbar().addCommandToSideMenu(cmd);
Button btn = new Button("toastBtn");
btn.addActionListener(e -> {
ToastBar.showErrorMessage("this is an error");
});
hi.add(btn);
}

CodenameOne Dialog showing without a call to show

I am aware of existing error dialogs but I am trying to create my own dialog from scratch.
public class ExceptionDialog extends Dialog {
private Button okButton;
private SpanLabel errorMessage;
private Label title;
public ExceptionDialog(String titleText, String messageText, String buttonText) {
super();
this.setLayout(new BorderLayout());
okButton = new Button(buttonText);
errorMessage = new SpanLabel(messageText);
title = new Label(titleText);
this.add(BorderLayout.NORTH, title);
this.add(BorderLayout.CENTER_BEHAVIOR_CENTER, errorMessage);
this.add(BorderLayout.SOUTH, okButton);
this.setAutoDispose(false);
this.setDisposeWhenPointerOutOfBounds(true);
this.showStretched(BorderLayout.SOUTH, true);
this.setTransitionInAnimator(CommonTransitions.createSlide(CommonTransitions.SLIDE_VERTICAL, true, 1000));
this.setTransitionOutAnimator(CommonTransitions.createSlide(CommonTransitions.SLIDE_VERTICAL, false, 1000));
Dialog thisDialog = this;
okButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent ev) {
System.out.println("BUTTON CLICKE");
thisDialog.dispose();
}
});
}
}
The problem: I noticed that on initialization, the dialog is showing without really calling ex.show(). Also, the button does not work, the only way to get the dialog to disappear is by clicking somewhere else on the screen.
Form hi = new Form("Hi World", BoxLayout.y());
hi.add(new Label("Hi World"));
hi.show();
ExceptionDialog ex = new ExceptionDialog("A", "B", "C");
Then I tried appending ex.show() to the code above. Now the dialog gets shown twice. The second time with a working button dismissing the dialog.
showStretched is show it will show the dialog and block until the dialog is disposed.

Resources