问题描述
我有一个JPopUpMenu,上面有几个JCheckBoxMenuItem's
.
I have a JPopUpMenu with several JCheckBoxMenuItem's
on it.
我还有一个属性文件(config.properties),当我关闭应用程序时,我在其中放置了一个参数,该参数保存了用户在此JPopUpMenu上检查过的每个JCheckBoxMenuItem.
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.
所以这个文件就像:
config.properties:
config.properties :
listeFiltres =Autres,Afrique du sud,Algérie
我想做的是,在应用程序启动时,将setSelected
保存在我的properties参数中的每个项目都保存起来.
What I would like to do is , on the start of my application, to setSelected
every item that is saved in my properties parameter.
例如,如果将"Afrique,Algérie,Allemagne"存储在config.properties
的参数listeFiltres
中,则我希望在应用程序开始时检查这3个JCheckBoxMenuItem
.
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.
问题是我只知道setSelected
方法,该方法允许选择具有特定索引(如2)的项目,但是在这里,我需要选择具有特定的项目>名称(例如非洲"),因此此方法不适合我.
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.
这是我的JPopUpMenu的代码:
Here's the code of my JPopUpMenu :
MainVue.java:
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);
}
}
推荐答案
您可以按以下方式从tableauPays数组中获取名称的索引,并将其传递给setSelected()方法
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++;
}
}
这篇关于如何在JPopUpMenu中通过setSelected命名一些JMenuItem?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!