Repaint issue

71 views
Skip to first unread message

Aashika

unread,
Aug 31, 2013, 1:46:14 PM8/31/13
to codenameone...@googlegroups.com
Hi,
   I have a file browser where each item is displayed with a checkbox in a list. I want to retain the checked boxes as the user navigates in and out of a folder. I am able to store which boxes are checked but they do not get repainted properly. I have also attached the full source codes. Please help.

FileListRenderer.java
public FileListRenderer() {
setLayout(new BorderLayout());
Container cnt=new Container(new BorderLayout());
cnt.addComponent(BorderLayout.WEST, name);
cnt.addComponent(BorderLayout.EAST, checkBox);
addComponent(BorderLayout.CENTER, cnt);
checkBox.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent evt) {
System.out.println("sourcecb:"+evt.getSource());
checkBoxClicked=1;
//CheckBox item = (CheckBox) evt.getSource();
//item.setSelected(!item.isSelected());
}
});
name.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent ae) {
System.out.println("sourcenm:"+ae.getSource());
//FileBrowser.sel=name.getText();
//FileBrowser.open();
}
});
}

public Component getListCellRendererComponent(final List list,
Object value, int index, boolean isSelected) {

ListItem cell = (ListItem) value;
name.setText(cell.getName());
/*name.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent ae) {
System.out.println("source1:"+ae.getSource());
//FileBrowser.sel=name.getText();
//System.out.println("sel:"+FileBrowser.sel);
//FileBrowser.open();
}
});*/
checkBox.setSelected(cell.isCellSelected());
checkBox.requestFocus();
/*checkBox.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent evt) {
ListItem item = (ListItem) list.getSelectedItem();
item.setCellSelected(!item.isSelected);
}
});*/
return this;
}

public Component getListFocusComponent(List arg0) {
// TODO Auto-generated method stub
return null;
}

}

class ListItem {
String name;
boolean isSelected = false;

ListItem(String name) {
this.name = name;
}

public String getName() {
return name;
}

public boolean isCellSelected() {
return isSelected;
}

public void setCellSelected(boolean isSelected) {
this.isSelected = isSelected;
}


FileBrowser.java
for (int j = 0; j < checkedFiles.size(); j++) {
 if (checkedFiles.elementAt(j).toString().startsWith(simgurl)) {
  String check = checkedFiles.elementAt(j).toString()
for (int i = 0; i < addv.size(); i++) {
String toCheck = addv.elementAt(i).toString();
listItem[i] = new ListItem(toCheck);
toCheck = simgurl + toCheck;
if (check.equals(toCheck)) {
        listItem[i].setCellSelected(true);
Display.getInstance().callSerially(new Runnable() {
             public void run() {
listf.repaint();                                                   filf.revalidate();
//filf.repaint();
}
});
** System.out.println("checked"+listItem[i].isCellSelected()+" "+listItem[i].getName());
break;
}
}



**- This prints all checked boxes properly. But the boxes are not checked.
FileBrowser.java
FileListRenderer.java

Shai Almog

unread,
Aug 31, 2013, 11:58:55 PM8/31/13
to codenameone...@googlegroups.com
Hi,
there is a bit of code here so its a bit hard to follow.

I suggest running this in the debugger and seeing whether checked ever arrives as true into the renderer, then inspecting the List that you got there and seeing if it is indeed the same model you got when adding the file elements.

Aashika

unread,
Sep 6, 2013, 9:57:13 AM9/6/13
to codenameone...@googlegroups.com
Hi Shai,
  Thanks for your reply. I checked as you said. I am able to retain only the last one or two checked items. The full code is as follows:


FileRenderer.java


public FileListRenderer() {
        setLayout(new BorderLayout());
        Container cnt=new Container(new BorderLayout());
        cnt.addComponent(BorderLayout.WEST, name);
        cnt.addComponent(BorderLayout.EAST, checkBox);
        addComponent(BorderLayout.CENTER, cnt);
        checkBox.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent evt) {
                System.out.println("sourcecb:"+evt.getSource());
                checkBoxClicked=1;
            }
        });
        name.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent ae) {
                System.out.println("sourcenm:"+ae.getSource());
            }
            });
       
    }

    public Component getListCellRendererComponent(final List list,
            Object value, int index, boolean isSelected) {

        ListItem cell = (ListItem) value;
        name.setText(cell.getName());
        checkBox.setSelected(cell.isCellSelected());
        checkBox.requestFocus();
        System.out.println("ren:"+name.getText()+" "+checkBox.isSelected());

        return this;
    }

    public Component getListFocusComponent(List arg0) {
        // TODO Auto-generated method stub
        return null;
    }

}

class ListItem {
    String name;
    boolean isSelected = false;

    ListItem(String name) {
        this.name = name;
    }

    public String getName() {
        return name;
    }

    public boolean isCellSelected() {
        return isSelected;
    }

    public void setCellSelected(boolean isSelected) {
        this.isSelected = isSelected;
    }


FileBrowser.java

    public FileBrowser() {
        filf = this;
        mainf = new Form("Main form");
        open = new Command("Open");
        back = new Command("Back");
  
        fss = FileSystemStorage.getInstance();

        addCommand(open);
        addCommand(back);
        addCommandListener(this);
        addCommandListener(new ActionListener() {

            public void actionPerformed(ActionEvent evt) {
                if (evt.getSource().toString() == "Back") {
                    try {
                        System.out.println("self sz:" + self.size());
                        if (self.size() != 0) {
                            self.removeElementAt(self.size() - 1);
                        }
                        if (self.size() == 0) {
                           simgurl = "";
                            temp = fn = "";
                            self = null;
                            self = new Vector();
                            list_roots();
                        } else {
                            temp = "";
                            for (int i = 0; i < self.size(); i++) {
                                temp = temp + self.elementAt(i);
                            }
                            simgurl = temp;
                            listedf = fss.listFiles(simgurl);
                            browse();
                        }
                    } catch (Exception e) {
                        Dialog.show(
                                "Error",
                                "There is some problem with tis folder..Please try another folder or restart the application..",
                                "Ok", null);
                        e.printStackTrace();
                    }
                }
            }
        });
        removeAll();
        addCommand(open);
        addCommand(back);
       list_roots();
    }

    public void actionPerformed(ActionEvent ae) {

        if (ae.getSource().toString() == "Open" || ae.getSource().equals(listf)) {
            open();
        }
    }

    public static void open() {
        try {
            if (listedRoots == 1) {
                sel = listf.getSelectedItem().toString();
                listedRoots = 0;
            }

            simgurl = "";

            String temp = sel;
            if (temp.equals("Phone Memory")) {
               temp = listedr[0];
            } else if (temp.equals("Memory Card")) {
               temp = listedr[1];
            }
            simgurl = getURL(temp);
            if (fss.isDirectory(simgurl)) {
                filf.removeAll();
               filf.repaint();
                System.out.println("surl:" + simgurl);
                listedf = fss.listFiles(simgurl);
                // browse dir
                browse();
                filf.repaint();
                filf.show();
            } else if (!fss.isDirectory(simgurl)) {
                simgurl = simgurl.substring(0, simgurl.length() - 1);
               fn = "";
                temp = "";
                self = null;
                self = new Vector();

                Display.getInstance().callSerially(new Runnable() {
                    public void run() {
                        mainf.show();
                    }
                });
            }
        } catch (SecurityException e) {
            Dialog.show(
                    "Change rights",
                    "This file doesnt hav enough rights. Please select another file or change rights of this file",
                    "Ok", null);
        } catch (Exception e) {
            e.printStackTrace();
            Dialog.show(
                    "Error",
                    "There is some problem with ur file..Please try another file or try again later..",
                    "Ok", null);
        }
    }

    public static void list_roots() {
        try {
            int ph = -1;
            listf = null;
            filf.removeAll();
            filf.revalidate();
            filf.repaint();
            listedr = fss.getRoots();
            listedRoots = 1;

            listedf = new String[listedr.length];

            /*
             * C drive - phone memory.E drive - memory card.
             */
            for (int i = 0; i < listedr.length; i++) {
                if (listedr[i].indexOf("C") > -1) {
                    ph = 1;
                } else if (listedr[i].indexOf("E") > -1) {
                    ph = 2;
                }
                if (ph == 1) {
                    listedf[0] = "Phone Memory";
                } else if (ph == 2) {
                    listedf[1] = "Memory Card";
                }
            }
            listf = new List(listedf);
            listf.addActionListener(new ActionListener() {
                public void actionPerformed(ActionEvent evt) {
                    open();
                }
            });
            ph = -1;
            listedf = null;
            filf.addComponent(listf);
            filf.revalidate();
            filf.repaint();
        } catch (Exception e) {
            e.printStackTrace();
            Dialog.show(
                    "Change rights",
                    "Ur device drives cannot be accessed due to lack of access rights. Please change the rights and try again",
                    "Ok", null);
        }

    }

    public static String getURL(String temp) {
        String tmp = String.valueOf(fss.getFileSystemSeparator());
        if (!temp.endsWith(tmp)) {
            temp += fss.getFileSystemSeparator();
        }
        self.addElement(temp);
        temp = "";
        System.out.println("selfURL:" + self.size());
        for (int i = 0; i < self.size(); i++) {
            temp = temp + self.elementAt(i);
        }
        simgurl = temp;
       return simgurl;
    }

    public static void browse() {
        int exf = 0;
        if (listf.size() != 0) {
            listf = null;
            addv = null;
            filf.removeAll();
        }
        try {
            addv = new Vector();
            for (int i = 0; i < listedf.length; i++) {
                if (listedf[i].indexOf(".") > -1) {
                    exf = 1;
               }
                if (exf == 1) {
                    addv.addElement(listedf[i]);

                    exf = 0;
               } else if (exf == 0) {
                   addv.addElement(listedf[i]);
                }
            }
            ListItem[] listItem = new ListItem[addv.size()];
            if (checkedFiles != null && checkedFiles.size() > 0) {

                for (int j = 0; j < checkedFiles.size(); j++) {
                    if (checkedFiles.elementAt(j).toString()
                            .startsWith(simgurl)) {
                        String check = checkedFiles.elementAt(j).toString();
                        for (int i = 0; i < addv.size(); i++) {
                            String toCheck = addv.elementAt(i).toString();
                            listItem[i] = new ListItem(toCheck);
                            toCheck = simgurl + toCheck;
                            if (check.equals(toCheck)) {
                                listItem[i].setCellSelected(true);
                                Display.getInstance().callSerially(new Runnable() {
                                    public void run() {
                                        listf.repaint();
                                        filf.revalidate();
                                    }
                                });

                                System.out.println("checked"+listItem[i].isCellSelected()+" "+listItem[i].getName());       
                                break;
                            }
                        }
                    } else {

                        for (int i = 0; i < addv.size(); i++) {
                            String toCheck = addv.elementAt(i).toString();
                            listItem[i] = new ListItem(toCheck);
                        }
                    }
                }
            } else {

                for (int i = 0; i < addv.size(); i++) {
                    String toCheck = addv.elementAt(i).toString();
                    listItem[i] = new ListItem(toCheck);
                }
            }
            Display.getInstance().callSerially(new Runnable() {
                public void run() {
                    listf.repaint();
                    filf.revalidate();
                    filf.repaint();
                }
            });

            listf = new List(listItem);
            listf.setListCellRenderer(new FileListRenderer());
            listf.addActionListener(new ActionListener() {
                public void actionPerformed(ActionEvent evt) {
                    System.out.println("sourcelst" + evt.getSource());
                    ListItem item = (ListItem) listf.getSelectedItem();
                    String name = item.getName();
                    if (FileListRenderer.checkBoxClicked == 1) {
                        boolean isChecked = item.isCellSelected();
                        item.setCellSelected(!isChecked);
                        FileListRenderer.checkBoxClicked = 0;
                        String url = simgurl + name;
                        if (!fss.isDirectory(simgurl)) {
                            url = url.substring(0, url.length() - 1);
                        }
                        if ((!isChecked) == true) {
                            checkedFiles.addElement(url);
                            System.out.println("added:" + url);
                        } else {
                            checkedFiles.removeElement(url);
                            System.out.println("removed:" + url);
                        }
                    } else {
                        sel = name;
                        open();

                    }
                }
            });
            Display.getInstance().callSerially(new Runnable() {
                public void run() {
                    filf.repaint();
                }
            });
            addv = null;
            if (listf.size() == 0) {
                temp = "";
               
                Dialog.show(
                        "Change selection",
                        "No files in this folder. Please select another folder or file",
                        "Ok", null);
               
                try {
                    if (self.size() != 0) {
                        self.removeElementAt(self.size() - 1);
                    }
                    if (self.size() == 0) {
                        simgurl = "";
                        self = null;
                        self = new Vector();
                        list_roots();

                    } else {
                        for (int i = 0; i < self.size(); i++) {
                            temp = temp + self.elementAt(i);
                        }
                        simgurl = temp;
                        listedf = fss.listFiles(simgurl);
                        browse();
                    }
                } catch (Exception e) {
                    Dialog.show(
                            "Change rights",
                            "This file does not have enough rights. Please select another file or change rights of this file"
                                    + e.getMessage(), "Ok", null);
                    e.printStackTrace();
                }

            } else {
                filf.addComponent(listf);
            }
            filf.revalidate();
        } catch (SecurityException e) {
            Dialog.show(
                    "Change rights",
                    "This file does not have enough rights. Please select another file or change     rights of this file",
                    "Ok", null);
        } catch (Exception e) {



Shai Almog

unread,
Sep 6, 2013, 1:18:49 PM9/6/13
to codenameone...@googlegroups.com
I asked for less code and you give me more :-)

Is the checked state set into the model?
If its modified from the model then who does the modification?
Reply all
Reply to author
Forward
0 new messages