John B. Matthews wrote:
In article <4958486d$0$23155$b9f67a60@news.newsdemon.com>,
Knute Johnson <nospam@rabbitbrush.frazmtn.com> wrote:
Daniel Moyne wrote:
I have created the same ActionListener on a collection of menu items in
a loop like this :
for (int i = 0; i < NUMBER_OPENED_FILES + 1; i++) {
name =.....
menuItem = new JMenuItem(name);
menuItem.setName("menuItem_" + String.valueOf(i));
menuItem.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
menuItemActionPerformed(evt);
}
});
}
In my method actionPerformed I want to know which menu has been
clicked; I know it is ridiculous because normally :
(1) you attach an ActionListener at each menu item,
(2) and in general you do not care about the menu item clicked.
I can process the string evt to extract the identifier of the menu item
clicked as the desired information in recorded in evt ; any other ideas
?
[...]
You can override JMenuItem to include a index number (the OO folks will
cringe though) or you can put them in an ArrayList and get their index
that way. The latter is probably best.
Knute's right, but I couldn't resist trying this alternative:
<code>
import java.awt.EventQueue;
import java.awt.event.ActionEvent;
import java.io.File;
import javax.swing.AbstractAction;
import javax.swing.Action;
import javax.swing.JFrame;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
/**
* @author John B. Matthews
*/
public class FileMenu {
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
(new FileMenu()).create();
}
});
}
void create() {
File userDir = new File(System.getProperty("user.dir"));
File[] files = userDir.listFiles();
JMenu menu = new JMenu("Recent Files");
for (File f : files) {
if (f.isFile()) {
RecentFile rf = new RecentFile(f);
menu.add(new JMenuItem(rf.getAction()));
}
}
JMenuBar bar = new JMenuBar();
bar.add(menu);
JFrame f = new JFrame("FileMenu");
f.setJMenuBar(bar);
f.pack();
f.setSize(480, 320);
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
f.setLocationRelativeTo(null);
f.setVisible(true);
}
}
class RecentFile {
private final File file;
private final Action action;
public RecentFile(final File file) {
this.file = file;
this.action = new AbstractAction(file.getPath()) {
public void actionPerformed(ActionEvent ae) {
System.out.println(file.length()
+ " " + file.getAbsolutePath());
}
};
}
public Action getAction() {
return action;
}
}
</code>
thanks Johne I will try this to undertstand the mechanism first ; in my case I store the files used and opened in a properrties file for later use ; when a file is selectee and no more exist I remove it from the list and I update the menu as the user can retry very likely with another vone from the list but that should not be a problem to refactor the code for this.
What is the use of the EventQueue.invokeLater(new Runnable() ; is this a thread ?
Regards.
--------------------------------oOO-(_)-OOo---------------------------------