I have been working on an html text editing program for a while and i wondered if anyone could help me with this small problem. I have two jmenus but one of them are misbehaving. i have two problems. 1 the menu item in the menu has a submenu arrow although it is not a sub menu
is that i have added an action listener to the menu but when i click the action doesent happen. here is the code.
import java.awt.GridLayout; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import java.awt.event.KeyEvent; import java.io.File; import java.io.FileReader; import java.io.FileWriter; import java.io.IOException; import javax.swing.JButton; import javax.swing.JFileChooser; import javax.swing.JFrame; import javax.swing.JMenu; import javax.swing.JMenuBar; import javax.swing.JMenuItem; import javax.swing.JOptionPane; import javax.swing.JTextArea; public class Window { static boolean saved = false; static boolean opened = false; static File saveDirectory = null; static File openDirectory = null; static final JTextArea editor = new JTextArea(10,50); public static void openWindowEditor(){ JFrame f = new JFrame("Easy HTML Text editor"); JMenuBar menuBar = new JMenuBar(); JMenu fileMenu = new JMenu("File"); JMenu viewMenu = new JMenu("View"); JMenuItem fileMenuSave = new JMenuItem("Save"); JMenuItem fileMenuOpen = new JMenuItem("Open"); JMenuItem fileMenuSaveAs = new JMenuItem("Save as..."); JMenuItem viewMenuEasyInsert = new JMenu("Easy insert"); fileMenu.setMnemonic(KeyEvent.VK_A); viewMenu.setMnemonic(KeyEvent.VK_A); menuBar.add(fileMenu); menuBar.add(viewMenu); fileMenu.add(fileMenuSave); fileMenu.add(fileMenuOpen); fileMenu.add(fileMenuSaveAs); viewMenu.add(viewMenuEasyInsert); fileMenuSave.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent arg0) { save(editor.getText()); } }); fileMenuOpen.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent arg0) { open(); } }); fileMenuSaveAs.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { saveAs(); } }); viewMenuEasyInsert.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent arg0) { openEasyInsertWindow(); } }); f.setJMenuBar(menuBar); f.add(editor); f.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE); f.setLayout(new GridLayout(1,1)); f.pack(); f.setVisible(true); } public static void open(){ JFileChooser chooser = new JFileChooser(); int returnVal = chooser.showDialog(null, "open"); if(returnVal == JFileChooser.APPROVE_OPTION){ File file = chooser.getSelectedFile(); try{ editor.read(new FileReader(file), null); }catch(IOException e){ JOptionPane.showMessageDialog(null, e); } } } public static void save(String text){ JFileChooser chooser = new JFileChooser(); File file = null; int returnVal = chooser.showDialog(null, "Save"); if(returnVal == JFileChooser.APPROVE_OPTION){ file = (chooser.getSelectedFile()); String result = text.replace("\n", System.getProperty("line.separator")); saveDirectory = chooser.getSelectedFile(); try{ FileWriter fw = new FileWriter(file); fw.write(result); fw.close(); saved = true; }catch(IOException e){ JOptionPane.showMessageDialog(null, e); } } } public static void saveAs(){ JFileChooser chooser = new JFileChooser(); File dir; int returnVal = chooser.showDialog(null, "Save as..."); if(returnVal == JFileChooser.APPROVE_OPTION){ dir = chooser.getSelectedFile(); try{ FileWriter fw = new FileWriter(dir); fw.write(editor.getText()); fw.close(); }catch(IOException e){ JOptionPane.showMessageDialog(null, e); } } } public static void openEasyInsertWindow(){ JFrame f = new JFrame("easy insert"); JButton insertDivButton = new JButton("Insert <div> element"); JButton openInsertSettings = new JButton("Open the insert settings"); f.setLayout(new GridLayout(2,1)); f.add(insertDivButton); f.add(openInsertSettings); f.pack(); f.setVisible(true); JOptionPane.showMessageDialog(editor, "Easy insert"); } }
Because you've defined it as a
JMenu
and not aJMenuItem
should be