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

Viewed 30

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 <a href="https://www.codenameone.com/">Codename One</a> 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() {
    }

}
1 Answers

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