Search code examples
java-memidplcdui

How to create multiple checkbox in canvas


I get trouble when I try to create a check box in canvas.

My checkbox works well but I don't know how to store value of each item, that mean when user check row 1 , and then they move to another row check box still check row 1, and when user check row 1 and 2 and move to another row, check box will check row 1 and 2.

But I can't find out solution for this problem


Solution

  • modify your code to use selectTodelete as boolean array instead of int, about like shown below

        // ...initialization of DataList
        boolean[] selectTodelete = new boolean[2]; // instead of int
        { selectTodelete[0] = selectTodelete[1] = false; } // init array
        Command editCommand, backCommand,selectCmd, unselectCmd,selectAll;
        //...
    
        protected void paint(Graphics g) {
            //...
            for(int i =0 ; i<countRow; i++ ){
                //draw background
                //...
                         if(selectTodelete[i]){ // was selectTodelete == 1
                                //draw select dot at location for row 'i'
                                //...
                         }
                // remove: you don't need that anymore: if(selectTodelete == 2) {
                                //draw select dot...
                //}
    
                // draw a checkbox before each item
                // ...
            }
        }
    
        public void commandAction(Command c, Displayable d) {
            //...
            if(c == selectCmd){
                selectTodelete[selectedItem] = true;
            }
            if(c== unselectCmd){
                selectTodelete[selectedItem] = false;
            }
            if(c == selectAll){
                selectTodelete[0] = selectTodelete[1] = true;
            }
            repaint();
        }
        //...
    }
    

    update - answer to question in comments

    I want to get RCID fit to checked it mean when row was checked I can get this id and when I use delete command it will delete all rows were checked

    For that, you can expose selectTodelete for use outside of its class with getter, or, better yet, with method like below...

        boolean isSelected(int elementNum) {
            return elementNum >= 0 && elementNum < selectTodelete.length
                    && selectTodelete[elementNum];
        } // modelled after javax.microedition.lcdui.Choice.isSelected
    

    ...information exposed like that can be further used anywhere when you need it to deal with RCID, like eg in method below:

        Vector useSelection(DataList dataList, DataStore[][] ds) {
            Vector result = new Vector();
            int count = ds.length;
            for(int i = 0; i < count; i++ ) {
                if (!dataList.isSelected(i)) {
                    continue; // skip non selected
                }
                System.out.println("RCID selected: [" + ds[i][5].cellText + "]");
                result.addElement(ds[i][5]);
            }
            return result;
        }