lets say I have a folder called examples/basics/
In that folder I have a bunch of .asm files. What I would now like to do is to have those files automatically made into JMenuItems, without the .asm extension, placed inside a JMenu and have actionlisteners added to them, that do the following:
User clicks on JMenuItem genetated. A new, lets say a CodeArea object is created and the file examples/basics/what I clikced on is passed in as a new File.
How to achive this with the simplest means?
Simple example for your purposes:
JFrame frame = new JFrame();
frame.setSize(400,400);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JMenuBar menu = new JMenuBar();
frame.setJMenuBar(menu);
JMenu mainMenu = new JMenu("Menu");
menu.add(mainMenu);
File f = new File(PATH_TO_FOLDER);
if(f.exists()){
File[] listFiles = f.listFiles();
for(File file : listFiles){
if(file.getAbsolutePath().endsWith(EXTENSION)){
final JMenuItem m = new JMenuItem(file.getName());
mainMenu.add(m);
m.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent arg0) {
System.out.println(m.toString());
}
});
}
}
}
frame.setLocationRelativeTo(null);
frame.setVisible(true);
Here PATH_TO_FOLDER is path to your folder with files
EXTENSION is target extension of file for menus