Search code examples
java-mecalendarpopuplwuit

lwuit calendar with button event


i tried to show the calendar on button click using form but i'm unable to change the date and very much struggled to find where the focus .

    ...
    Button mdate=new Button("change date");
    mdate.addActionListener(this);
    ...
    public void actionPerformed(ActionEvent ae) {
       Form cal= new Form();
       com.sun.lwuit.Calendar c =new com.sun.lwuit.Calendar();
       c.setFocus(true);
       c.addActionListener(this);
       cal.addComponent(c);
       cal.show();
    }

how to show and hide calendar on button click in a better way


Solution

  • Better you can use Dialog (like pop up) instead of Form. You can easily dispose within a Form. No need to show another form. See the below sample code,

    Button button = new Button("Click me");
    form.addComponent(button);
    button.addActionListener(new ActionListener() {
    
        public void actionPerformed(ActionEvent ae) {
            final Dialog cal = new Dialog();
            final com.sun.lwuit.Calendar c = new com.sun.lwuit.Calendar();
            c.setFocus(true);
            c.addActionListener(this);
            cal.addComponent(c);
            cal.addCommand(new Command("Cancel") {
    
             public void actionPerformed(ActionEvent evt) {
                  cal.dispose();
                }
            });
          c.addActionListener(new ActionListener() {
             public void actionPerformed(ActionEvent evt) {
                System.out.println("Selected date :: " + c.getDate().toString())
            }
         });
        cal.show(20, 20, 20, 20, true, false);
        }
    });
    

    And add the selected and unselected style for Calendar like CalendarSelectedDay, CalendarDate. Also add the selected and unselected style for ComboBox.