How to setSelected by name some JMenuItem in a JPopUpMenu?

纵然是瞬间 提交于 2019-12-11 05:19:20

问题


I have a JPopUpMenu with several JCheckBoxMenuItem's on it.

I also have a properties files (config.properties) where I put a parameter which save every JCheckBoxMenuItem the user has checked on this JPopUpMenu , when i close the application.

So this file is like :

config.properties :

listeFiltres =Autres,Afrique du sud,Algérie

What I would like to do is , on the start of my application, to setSelected every item that is saved in my properties parameter.

For exemple, if "Afrique,Algérie,Allemagne" are stored in my parameter listeFiltres in config.properties, I want these 3 JCheckBoxMenuItem to be checked at the start of my application.

The problem is that I only know the setSelected method which allows to select an item with a specific index(like 2), but here I need to select an item with a specific name (like "Afrique"), so this method isn't appropriate for me.

Here's the code of my JPopUpMenu :

MainVue.java:

public class MainVue extends JFrame implements ActionListener {

 private static final JScrollPopupMenu menuProduit = new JScrollPopupMenu();
 private static final JScrollPopupMenu menuPays = new JScrollPopupMenu();
 private static List<String> listeFiltres = new ArrayList<String>();
 private String listeDeFiltres;
 private String[] tableauFiltrePermanent;
 private String listeFiltrePermanent;

 private String[] tableauPays = { "Autres", "Afrique du sud", "Algérie", "Allemagne", "Arabie Saoudite", "Argentine",
        "Australie", "Bangladesh", "Belgique", "Brésil", "Bulgarie", "Canada", "Chine", "Corée du sud", "Egypte",
        "Emirats-Arabes Unis", "Espagne", "Etats-Unis", "Ethiopie", "Europe", "France", "Hongrie", "Inde",
        "Indonésie", "Irak", "Iran", "Israél", "Italie", "Japon", "Jordanie", "Kazakhstan", "Koweit", "Liban",
        "Libye", "Malaisie", "Maroc", "Mexique", "Monde", "Oman", "Pakistan", "Pays-Bas", "Philippines", "Poligne",
        "Portugal", "Qatar", "République tchéque", "Roumanie", "Russie", "Taïwan", "Tunisie", "Turquie",
        "Ukraine" };
 private String[] tableauProduit = { "Blé", "Colza", "Mais", "Orge", "Orge de Brasserie", "Palme", "Soja",
        "Tournesol", "Tourteaux De Colza", "Tourteaux de Soja", "Huile de Soja", "Huile De Colza" };

 private List<JCheckBoxMenuItem> listJCBProduit = new ArrayList<JCheckBoxMenuItem>();
 private List<JCheckBoxMenuItem> listJCBPays = new ArrayList<JCheckBoxMenuItem>();
 public static PropertiesConfiguration prop;

 public MainVue(Modele modele, Controleur controleur) throws ClassNotFoundException, SQLException, IOException {

  prop = new PropertiesConfiguration("config.properties");

  for (int i = 0; i < tableauProduit.length; i++) {
        listJCBProduit.add(new JCheckBoxMenuItem(tableauProduit[i]));
    }

    for (int j = 0; j < listJCBProduit.size(); j++) {
        JCheckBoxMenuItem produitActuel = listJCBProduit.get(j);
        menuProduit.add(produitActuel);
        produitActuel.addActionListener(new OpenAction(menuProduit, boutonProduit));


    }

    for (int i = 0; i < tableauPays.length; i++) {
        listJCBPays.add(new JCheckBoxMenuItem(tableauPays[i]));
    }

    for (int j = 0; j < listJCBPays.size(); j++) {
        JCheckBoxMenuItem paysActuel = listJCBPays.get(j);
        menuPays.add(paysActuel);
        paysActuel.addActionListener(new OpenAction(menuPays, boutonPays));
    }
    listeDeFiltres = "";

        for (int p = 0; p < listeFiltres.size(); p++) {
            String filtreActuel = listeFiltres.get(p);
            if (listeDeFiltres == "") {
                listeDeFiltres += filtreActuel;
            } else {
                listeDeFiltres += "," + filtreActuel;
            }

            }
  prop.setProperty("listeFiltres", listeDeFiltres);
 }
}

回答1:


You can get the index of name from tableauPays array as follows and can pass it to the setSelected() method

public int getIndex(String name){
        int index = 0;
        for(String p : tableauPays){
            if(p.equals(name)){
                return index;
            }
            index++;
        }
    }


来源:https://stackoverflow.com/questions/32586045/how-to-setselected-by-name-some-jmenuitem-in-a-jpopupmenu

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!