Gaurav Takte
Gaurav Takte

Reputation: 615

Codename one date picker issue

I have created a custom calendar where I am trying to call picker whenever I am clicking on calendar button, but it can't work for me, Following is my application code, which is not working properly. So how can I modify this code so I am able to call picker from ActionEvent?

    public class PivDisplayCalendar extends Container {


    int length =37;
     private ComboBox year;
     private static final String[] DAYS = {"Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday"};
     private static final String[] LABELS = {"Su", "M", "Tu", "W", "Th", "F", "Sa"};

     private ArrayList<Button> allButtons = new ArrayList<Button>();
     //Button newButton = new Button("");

    public PivDisplayCalendar(){

        super(new BoxLayout(BoxLayout.Y_AXIS));
            Container calendarTitle = new Container(new BorderLayout(BorderLayout.CENTER_BEHAVIOR_CENTER));
            Container title = new Container(new GridLayout(1,7));
            Container days = new Container(new GridLayout(6, 7));
            Container calendarTitleCopy = new Container(new GridLayout(1, 1));
                calendarTitleCopy.setUIID("CalendarTitleCopy");
            this.addComponent(calendarTitleCopy);
            this.addComponent(title);
            this.addComponent(days);


            Button prevMonth = new Button("<");
            Button nextMonth = new Button(">");
            SpanLabel monthYear = new SpanLabel("Month " + " Year");
            calendarTitle.add(BorderLayout.WEST, prevMonth);
            calendarTitle.add(BorderLayout.CENTER, monthYear);
            calendarTitle.add(BorderLayout.EAST, nextMonth);
            calendarTitleCopy.add(calendarTitle);
            Button dayButton= new Button();




           if(UIManager.getInstance().isThemeConstant("calTitleDayStyleBool", false)) {
                title.setUIID("CalendarTitleArea");
                days.setUIID("CalendarDayArea");
            }
            for (int iter = 0; iter < DAYS.length; iter++) {
                title.addComponent(createDayTitle(iter));
            }
            for (int iter = 1; iter < length; iter++) {
               dayButton = new Button(""+iter);

                    dayButton.addActionListener(new ActionListener() {
                        @Override
                        public void actionPerformed(ActionEvent evt) {
                            Log.p("Action event triggered");
                            Picker datePicker = new Picker();
                            datePicker.setType(Display.PICKER_TYPE_DATE);

                            //Button b1 = (Button)(evt.getActualComponent());
                            //Log.p( b1.getText() );
                        }
                    });

               allButtons.add(dayButton);
                days.addComponent(dayButton);
                if (iter <= 7) {
                    dayButton.setNextFocusUp(year);
                }
            }              
    }


     protected Label createDayTitle(int day) {
        String value = getUIManager().localize("Calendar." + DAYS[day], LABELS[day]);
        Label dayh = new Label(value, "Label");
        dayh.setEndsWith3Points(false);
        dayh.setTickerEnabled(false);
        return dayh;
    }
}

This is my application screenshot:-

enter image description here

And following method is used for event handling:-

for (int iter = 1; iter < length; iter++) {
               dayButton = new Button(""+iter);

                    dayButton.addActionListener(new ActionListener() {
                        @Override
                        public void actionPerformed(ActionEvent evt) {
                            Log.p("Action event triggered");
                            Picker datePicker = new Picker();
                            datePicker.setType(Display.PICKER_TYPE_DATE);

                            //Button b1 = (Button)(evt.getActualComponent());
                            //Log.p( b1.getText() );
                        }
                    });

               allButtons.add(dayButton);
                days.addComponent(dayButton);
                if (iter <= 7) {
                    dayButton.setNextFocusUp(year);
                }
            }

Upvotes: 1

Views: 679

Answers (1)

steve hannah
steve hannah

Reputation: 4716

Your code creates a Picker component, but doesn't add it to any form or display it. Probably what you are looking for is Display.showNativePicker(), as this will simply pop up a native picker dialog.

You should first check to see if the platform supports a native picker (only iOS and Android have native pickers), and create your own dialog with a DateSpinner component in it. Check out the source for the Picker class to see an example of how it does this.

Upvotes: 2

Related Questions