Java Code Examples for com.codename1.ui.Form#setLayout()

The following examples show how to use com.codename1.ui.Form#setLayout() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: Log.java    From CodenameOne with GNU General Public License v2.0 6 votes vote down vote up
/**
 * Places a form with the log as a TextArea on the screen, this method can
 * be attached to appear at a given time or using a fixed global key. Using
 * this method might cause a problem with further log output
 * @deprecated this method is an outdated method that's no longer supported
 */
public static void showLog() {
    try {
        String text = getLogContent();
        TextArea area = new TextArea(text, 5, 20);
        Form f = new Form("Log");
        f.setScrollable(false);
        final Form current = Display.getInstance().getCurrent();
        Command back = new Command("Back") {
            public void actionPerformed(ActionEvent ev) {
                current.show();
            }
        };
        f.addCommand(back);
        f.setBackCommand(back);
        f.setLayout(new BorderLayout());
        f.addComponent(BorderLayout.CENTER, area);
        f.show();
    } catch (Exception ex) {
        ex.printStackTrace();
    }
}
 
Example 2
Source File: Flickr.java    From codenameone-demos with GNU General Public License v2.0 6 votes vote down vote up
public static Form createMainForm() {
    Form main = new Form("Flickr tags");
    main.setLayout(new BorderLayout());
    Toolbar bar = new Toolbar();
    main.setToolBar(bar);
    addCommandsToToolbar(bar);

    TextArea desc = new TextArea();
    desc.setText("This is a Flickr tags demo, the demo uses the Toolbar to arrange the Form Commands.\n\n"
            + "Select \"Cats\" to view the latest photos that were tagged as \"Cats\".\n\n"
            + "Select \"Dogs\" to view the latest photos that were tagged as \"Dogs\".\n\n"
            + "Select \"Search\" to enter your own tags for search.");
    desc.setEditable(false);

    main.addComponent(BorderLayout.CENTER, desc);
    return main;
}
 
Example 3
Source File: ChartsInBoxLayout.java    From codenameone-demos with GNU General Public License v2.0 6 votes vote down vote up
public ChartsInBoxLayout() {
    form = new Form("Charts in Box Layout");
    form.setLayout(new BoxLayout(BoxLayout.Y_AXIS));
    
    Component chart1 = getTemperatureChart();
    chart1.getStyle().setBgColor(0x0);
    //chart1.setHeight(300);
    //chart1.setWidth(300);
    chart1.setPreferredH(500);
    
    form.addComponent(chart1);
    Component chart2 = getCombinedTemperatureChart();
    chart2.getStyle().setBgColor(0x0);
    chart2.setPreferredH(500);
    //chart2.setHeight(300);
    //chart2.setWidth(300);
    form.setScrollableY(true);
    form.addComponent(chart2);
    
    
    

}
 
Example 4
Source File: SignatureComponentDemo.java    From codenameone-demos with GNU General Public License v2.0 6 votes vote down vote up
public void start() {
    if(current != null){
        current.show();
        return;
    }
    Form hi = new Form("Signature Component");
    hi.setLayout(new BoxLayout(BoxLayout.Y_AXIS));
    hi.add("Enter Your Name:");
    hi.add(new TextField());
    hi.add("Signature:");
    SignatureComponent sig = new SignatureComponent();
    sig.addActionListener((evt)-> {
        System.out.println("The signature was changed");
        Image img = sig.getSignatureImage();
        // Now we can do whatever we want with the image of this signature.
    });
    hi.addComponent(sig);
    hi.show();
}
 
Example 5
Source File: PhotoShare.java    From codenameone-demos with GNU General Public License v2.0 6 votes vote down vote up
private void showMainForm() {
    final Form photos = new Form("Photos");
    photos.setLayout(new BorderLayout());
    GridLayout gr = new GridLayout(1, 1);
    final Container grid = new Container(gr);
    gr.setAutoFit(true);
    grid.setScrollableY(true);
    grid.addPullToRefresh(new Runnable() {
        public void run() {
            refreshGrid(grid);
        }
    });

    grid.addComponent(new InfiniteProgress());
    photos.addComponent(BorderLayout.CENTER, grid);

    photos.removeAllCommands();
    photos.setBackCommand(null);
    photos.addCommand(createPictureCommand(grid));

    photos.show();
    refreshGrid(grid);
}
 
Example 6
Source File: SearchCommandTextHintSample2953.java    From CodenameOne with GNU General Public License v2.0 6 votes vote down vote up
public void start() {
    if (current != null) {
        current.show();
        return;
    }
    Form hi18 = new Form("FormTitle");
    hi18.setLayout(BoxLayout.y());
    Container cont18 = hi18.getContentPane();
    hi18.getToolbar().addSearchCommand((e) -> {
        String text = (String) e.getSource();
        for (Component c : hi18.getContentPane()) {
            c.setHidden(c instanceof Label && ((Label) c).getText().indexOf(text) < 0);
        }
        hi18.getComponentForm().animateLayout(150);
    });
    for (int i = 0; i < 20; i++) {
        Label l = new Label("Label " + i);
        cont18.add(l);
    }
    hi18.show();
}
 
Example 7
Source File: WKWebViewTest.java    From CodenameOne with GNU General Public License v2.0 5 votes vote down vote up
private void showWebView() {
	Form form = new Form();
	BrowserComponent bc = new BrowserComponent();
	form.setLayout(new BorderLayout());
	form.add(BorderLayout.CENTER, bc);
	bc.setURL("https://www.primefaces.org/primeng/#/");
	
	Button back = new Button("back");
	back.addActionListener((e) -> {
		hi.show();
	});
	form.add(BorderLayout.NORTH, back);
	
	form.show();
}
 
Example 8
Source File: SetLoopTest2969.java    From CodenameOne with GNU General Public License v2.0 5 votes vote down vote up
public void start() {
    if(current != null){
        current.show();
        return;
    }
    Form f = new Form("NativePlayerMode Test");
    f.setLayout(new BorderLayout());
    Container videoContainer = new Container(new BorderLayout());
    Button playButton = new Button("Play Video");
    playButton.addActionListener(e->{
        String videoPath;
        if (isSimulator()) {
            videoPath = "https://weblite.ca/cn1tests/small.mp4";
        } else {
            videoPath = "https://weblite.ca/cn1tests/small.mp4";
        }
        try {
            if (media != null) {
                media.cleanup();
                media = null;
            }
            Media video = MediaManager.createMedia(videoPath, true);
            MediaPlayer mp = new MediaPlayer(video);
            mp.setLoop(true);
            videoContainer.removeAll();
            videoContainer.add(CENTER, mp);
            media = media;
            f.revalidateWithAnimationSafety();
            
        } catch (Exception ex) {
            Log.e(ex);
        }
    });
    f.add(NORTH, playButton);
    f.add(CENTER, videoContainer);
    f.show();
}
 
Example 9
Source File: WebSocketsSample.java    From CodenameOne with GNU General Public License v2.0 5 votes vote down vote up
void showChat() {
    Form f= new Form("Chat");
    f.setLayout(new BorderLayout());
    
    Container south = new Container();
    final TextField tf = new TextField();
    Button send = new Button(new Command("Send") {

        @Override
        public void actionPerformed(ActionEvent evt) {
            if (sock.getReadyState() == WebSocketState.OPEN) {
                sock.send(tf.getText());
                tf.setText("");
            } else {
                Dialog.show("", "The socket is not open", "OK", null);
                showLogin();
            }
            
        }
         
    });
    
    chatContainer = new Container();
    chatContainer.setLayout(new BoxLayout(BoxLayout.Y_AXIS));
    
    south.addComponent(tf);
    south.addComponent(send);
    f.addComponent(BorderLayout.SOUTH, south);
    f.addComponent(BorderLayout.CENTER, chatContainer);
    f.setFormBottomPaddingEditingMode(true);
    f.show();
    
}
 
Example 10
Source File: CodenameOneComponentWrapper.java    From CodenameOne with GNU General Public License v2.0 5 votes vote down vote up
public CodenameOneComponentWrapper(com.codename1.ui.Component codenameOneCmp, boolean forceShow) {
    this.codenameOneCmp = codenameOneCmp;
    if(codenameOneCmp instanceof Label) {
        ((Label)codenameOneCmp).setLegacyRenderer(true);
    }
    if(codenameOneCmp.getParent() == null) {
        if(!(codenameOneCmp instanceof Form)) {
            Form dummy = new Form("") {
                @Override
                protected boolean shouldPaintStatusBar() {
                    return false;
                }

                @Override
                protected Component createStatusBar() {
                    return new com.codename1.ui.Container();
                }
            };
            dummy.getTitleArea().setPreferredSize(new com.codename1.ui.geom.Dimension(0, 0));
            dummy.setLayout(new com.codename1.ui.layouts.BorderLayout());
            dummy.addComponent(com.codename1.ui.layouts.BorderLayout.CENTER, codenameOneCmp);
            dummy.setWidth(1000);
            dummy.setHeight(1000);
            dummy.layoutContainer();
            if(forceShow || com.codename1.ui.Display.getInstance().getCurrent() == null) {
                dummy.show();
            }
        } else {
            ((Form)codenameOneCmp).layoutContainer();
            if(com.codename1.ui.Display.getInstance().getCurrent() == null) {
                if(codenameOneCmp instanceof com.codename1.ui.Dialog) {
                    ((com.codename1.ui.Dialog)codenameOneCmp).showModeless();
                } else {
                    ((Form)codenameOneCmp).show();
                }
            }
        }
    }
}
 
Example 11
Source File: GameCanvasImplementation.java    From CodenameOne with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Start media playback implicitly setting the component to visible
 */
public void play() {
    if(nativePlayer && curentForm == null){
        curentForm = Display.getInstance().getCurrent();
        Form f = new Form();
        f.setLayout(new BorderLayout());
        f.addComponent(BorderLayout.CENTER, new MediaPlayer(this));
        f.show();
    }
    
    player.play();
}
 
Example 12
Source File: MapsDemo.java    From codenameone-demos with GNU General Public License v2.0 5 votes vote down vote up
private void showMeOnMap() {
    Form map = new Form("Map");
    map.setLayout(new BorderLayout());
    map.setScrollable(false);
    final MapComponent mc = new MapComponent();

    putMeOnMap(mc);
    mc.zoomToLayers();

    map.addComponent(BorderLayout.CENTER, mc);
    map.getToolbar().addCommandToLeftBar(new MapsDemo.BackCommand());
    map.setBackCommand(new MapsDemo.BackCommand());
    map.show();

}
 
Example 13
Source File: PhotoShare.java    From codenameone-demos with GNU General Public License v2.0 5 votes vote down vote up
private void showLoginForm() {
    Form login = new Form("Login");
    BorderLayout bl = new BorderLayout();
    bl.setCenterBehavior(BorderLayout.CENTER_BEHAVIOR_CENTER_ABSOLUTE);
    ComponentGroup loginDetails = new ComponentGroup();
    
    TextField displayName = new TextField();
    displayName.setHint("Display Name");
    loginDetails.addComponent(displayName);

    final TextField email = new TextField();
    email.setHint("E-Mail");
    loginDetails.addComponent(email);

    Button send = new Button("Send Verification");
    loginDetails.addComponent(send);
    
    send.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent evt) {
            onSendAction(email);
        }
    });
    
    login.setLayout(bl);
    login.addComponent(BorderLayout.CENTER, loginDetails);
    login.show();
}
 
Example 14
Source File: ClockDemo.java    From codenameone-demos with GNU General Public License v2.0 5 votes vote down vote up
public void start() {
    if(current != null){
        current.show();
        return;
    }
    Form hi = new Form("Clock Demo");
    AnalogClock clock = new AnalogClock();
    hi.setLayout(new BorderLayout());
    hi.addComponent(BorderLayout.CENTER, clock);
    clock.start();
    
    hi.show();
}
 
Example 15
Source File: SwitchScrollWheelingIssue.java    From CodenameOne with GNU General Public License v2.0 5 votes vote down vote up
public void start() {
    if(current != null){
        current.show();
        return;
    }
    Form f = new Form();
     f.setLayout(new BoxLayout(BoxLayout.Y_AXIS));
     f.setScrollable(true);
     for (int x=1; x < 100; x++) {
         Container cnt = new Container(new GridLayout(2));
         cnt.addAll(new Label("Line" + x), new Switch());
         f.add(cnt);
     }
     f.show();
}
 
Example 16
Source File: SocialChat.java    From codenameone-demos with GNU General Public License v2.0 5 votes vote down vote up
private void createMorphEffect(Label titleLabel) {
    // animate the components out of the previous form if we are coming in from the login form
    Form parent = Display.getInstance().getCurrent();
    if(parent.getUIID().equals("MainForm")) {
        for(Component cmp : parent.getContentPane()) {
            cmp.setX(parent.getWidth());
        }
        
        // moves all the components outside of the content pane to the right while fading them out over 400ms
        parent.getContentPane().animateUnlayoutAndWait(400, 0);
        parent.getContentPane().removeAll();
        
        // we can't mutate a form into a component in another form so we need to convert the background to an image and then morph that
        // this is especially easy since we already removed all the components
        Label dummy = new Label();
        dummy.setShowEvenIfBlank(true);
        dummy.setUIID("Container");
        dummy.setUnselectedStyle(new Style(parent.getUnselectedStyle()));
        parent.setUIID("Form");
        
        // special case to remove status bar on iOS 7
        parent.getTitleArea().removeAll();
        parent.setLayout(new BorderLayout());
        parent.addComponent(BorderLayout.CENTER, dummy);
        parent.revalidate();
        
        // animate the main panel to the new location at the top title area of the screen
        dummy.setName("fullScreen");
        titleLabel.setName("titleImage");
        parent.setTransitionOutAnimator(MorphTransition.create(1100).morph("fullScreen", "titleImage"));
    }
}
 
Example 17
Source File: TestNativePlayerMode2972.java    From CodenameOne with GNU General Public License v2.0 5 votes vote down vote up
public void start() {
    if(current != null){
        current.show();
        return;
    }
    Form f = new Form("NativePlayerMode Test");
    f.setLayout(BoxLayout.y());
    
    Button playButton = new Button("Play Video");
    playButton.addActionListener(e->{
        String videoPath;
        if (isSimulator()) {
            videoPath = "http://www.codenameone.com/files/hello-codenameone.mp4";
        } else {
            videoPath = "https://www.codenameone.com/files/hello-codenameone.mp4";
        }
        try {
            Media video = MediaManager.createMedia(videoPath, true);
            video.setNativePlayerMode(true); // this works nicely on iOS 12, I didn't test other iOS versions yet
            video.prepare();
            video.setFullScreen(true);
            video.play();
        } catch (Exception ex) {
            Log.e(ex);
        }
    });
    f.add(playButton);
    f.show();
    
    
}
 
Example 18
Source File: Poker.java    From codenameone-demos with GNU General Public License v2.0 4 votes vote down vote up
/**
 * The splash screen is relatively bare bones. Its important to have a splash screen for iOS 
 * since the build process generates a screenshot of this screen to speed up perceived performance
 */
public void showSplashScreen() {
    final Form splash = new Form();
    
    // a border layout places components in the center and the 4 sides.
    // by default it scales the center component so here we configure
    // it to place the component in the actual center
    BorderLayout border = new BorderLayout();
    border.setCenterBehavior(BorderLayout.CENTER_BEHAVIOR_CENTER_ABSOLUTE);
    splash.setLayout(border);
    
    // by default the form's content pane is scrollable on the Y axis
    // we need to disable it here
    splash.setScrollable(false);
    Label title = new Label("Poker Ace");
    
    // The UIID is used to determine the appearance of the component in the theme
    title.setUIID("SplashTitle");
    Label subtitle = new Label("By Codename One");
    subtitle.setUIID("SplashSubTitle");
    
    splash.addComponent(BorderLayout.NORTH, title);
    splash.addComponent(BorderLayout.SOUTH, subtitle);
    Label as = new Label(cards.getImage("as.png"));
    Label ah = new Label(cards.getImage("ah.png"));
    Label ac = new Label(cards.getImage("ac.png"));
    Label ad = new Label(cards.getImage("ad.png"));

    // a layered layout places components one on top of the other in the same dimension, it is
    // useful for transparency but in this case we are using it for an animation
    final Container center = new Container(new LayeredLayout());
    center.addComponent(as);
    center.addComponent(ah);
    center.addComponent(ac);
    center.addComponent(ad);
    
    splash.addComponent(BorderLayout.CENTER, center);
            
    splash.show();
    splash.setTransitionOutAnimator(CommonTransitions.createCover(CommonTransitions.SLIDE_VERTICAL, true, 800));

    // postpone the animation to the next cycle of the EDT to allow the UI to render fully once
    Display.getInstance().callSerially(new Runnable() {
        public void run() {
            // We replace the layout so the cards will be laid out in a line and animate the hierarchy
            // over 2 seconds, this effectively creates the effect of cards spreading out
            center.setLayout(new BoxLayout(BoxLayout.X_AXIS));
            center.setShouldCalcPreferredSize(true);
            splash.getContentPane().animateHierarchy(2000);

            // after showing the animation we wait for 2.5 seconds and then show the game with a nice
            // transition, notice that we use UI timer which is invoked on the Codename One EDT thread!
            new UITimer(new Runnable() {
                public void run() {
                    showGameUI();
                }
            }).schedule(2500, false, splash);
        }
    });
}
 
Example 19
Source File: SocialChat.java    From codenameone-demos with GNU General Public License v2.0 4 votes vote down vote up
/**
 * Shows the form containing the list of contacts we can chat with
 */
void showContactsForm(UserData data) {
    listenToMessages();
    Form contactsForm = new Form("Contacts");
    contactsForm.setLayout(new BoxLayout(BoxLayout.Y_AXIS));
    
    // the toolbar is created into a layer on the content pane. This allows us to render behind it and leave it semi transparent
    Toolbar tb = new Toolbar(true);
    
    // we want the title area to be transparent so it won't get in the way
    contactsForm.getTitleArea().setUIID("Container");

    // folds the toolbar automatically as we scroll down, shows it if we scroll back up
    tb.setScrollOffUponContentPane(true);
    contactsForm.setToolBar(tb);
    
    // we want the image behind the toolbar to stretch and fit the entire screen and leave no margin
    Label titleLabel = new Label(" ");
    Style titleLabelStyle = titleLabel.getUnselectedStyle();
    titleLabelStyle.setBgImage(theme.getImage("social-chat-tutorial-image-top.jpg"));
    titleLabelStyle.setBackgroundType(Style.BACKGROUND_IMAGE_SCALED_FILL);
    titleLabelStyle.setPadding(tb.getPreferredH(), tb.getPreferredH(), tb.getPreferredH(), tb.getPreferredH());
    titleLabelStyle.setPaddingUnit(Style.UNIT_TYPE_PIXELS, Style.UNIT_TYPE_PIXELS, Style.UNIT_TYPE_PIXELS, Style.UNIT_TYPE_PIXELS);
    titleLabelStyle.setMargin(0, 0, 0, 0);
    
    contactsForm.addComponent(titleLabel);
    
    // the behavior of the title is rather complex so we extract it to a separate method
    tb.setTitleComponent(createTitleComponent(contactsForm));
            
    InfiniteProgress ip = new InfiniteProgress();
    contactsForm.addComponent(ip);
    
    loadContacts(data, ip, contactsForm.getContentPane());
    
    // creates the morph and other animations from the main form to the second form of the app
    createMorphEffect(titleLabel);
    
    contactsForm.show();
    showPendingMessages(contactsForm);
}
 
Example 20
Source File: TestComponent.java    From CodenameOne with GNU General Public License v2.0 4 votes vote down vote up
private void testBrowserComponent2267() {
    Form hi = new Form();
    String formName = "testBrowserComponent2267";
    hi.setName(formName);
    hi.setLayout(new BorderLayout());

    BrowserComponent browserComponent = new BrowserComponent();
    hi.add(BorderLayout.CENTER, browserComponent);

    final Throwable[] ex = new Throwable[1];
    final boolean[] complete = new boolean[1];
    Button loadButton = new Button("setUrl");
    String buttonName = "setUrl";
    loadButton.setName(buttonName);
    loadButton.addActionListener((ev) -> {
            ActionListener errorHandler = new ActionListener() {
                public void actionPerformed(ActionEvent e) {
                    e.consume();
                    ex[0] = (Throwable)e.getSource();
                    complete[0] = true;
                    Display.getInstance().removeEdtErrorHandler(this);
                }
            };
            try {


                Display.getInstance().addEdtErrorHandler(errorHandler);
                browserComponent.addWebEventListener("onLoad", e-> {
                    Display.getInstance().removeEdtErrorHandler(errorHandler);
                    complete[0] = true;
                });
                browserComponent.setURL("https://www.google.es");
            } catch (Throwable t) {
                ex[0] = t;
                complete[0] = true;
                Display.getInstance().removeEdtErrorHandler(errorHandler);
            } finally {
                //complete[0] = true;
            }
    });
    hi.add(BorderLayout.SOUTH, loadButton);
    hi.show();
    log("About to wait for form "+formName);
    TestUtils.waitForFormName(formName, 4000);
    log("Finished waiting for form "+formName);
    TestUtils.clickButtonByName(buttonName);
    log("Waiting for browserComponent to load https://www.google.es");
    while (!complete[0]) {
        Display.getInstance().invokeAndBlock(()->{
            Util.sleep(50);
        });
    }
    log("Finished waiting for browserComponent to load https://www.google.es");
    String message = null;
    if (ex[0] != null) {
        message = ex[0].getMessage();
    }
    TestUtils.assertBool(ex[0] == null, "We received an exception setting the browserComponent URL: "+message);
}