0

I have a JMenu populated with JMenuItems from a database that have listeners, one of them being to delete the entry from the database if selected. When this happens that JMenuItem should disappear from the menus. Here's a short bit as an example

for (final Racer r : Racer.getAllRacers()) {
        JMenuItem j = new JMenuItem(r.getName());
        racerDelete.add(j);
        j.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                int reply = JOptionPane.showConfirmDialog(null,
                        "Are you sure you want to delete racer " + r.getName() + "?", "Delete?",
                        JOptionPane.YES_NO_OPTION);
                if (reply == JOptionPane.YES_OPTION){
                    r.delete();
                    racerDelete.remove(???);
                }

            }
        });
    }

So what can I put in place of the "???"? The fields of r are about all I have to identify the JMenuItem. I already tried racerDelete.remove(j) which doesnt work and I'm not sure why.

Michael Groleau
  • 81
  • 1
  • 2
  • 9

2 Answers2

1

The trick is to read the error message, which probably says something like "variable j must be declared final to be used inside the anonymous class". Change your code to

    final JMenuItem j = new JMenuItem(r.getName());
    racerDelete.add(j);
    j.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent e) {
            int reply = JOptionPane.showConfirmDialog(null,
                    "Are you sure you want to delete racer " + r.getName() + "?", "Delete?",
                    JOptionPane.YES_NO_OPTION);
            if (reply == JOptionPane.YES_OPTION){
                r.delete();
                racerDelete.remove(j);
            }

        }
    });
JB Nizet
  • 678,734
  • 91
  • 1,224
  • 1,255
  • Yeah I did that, and it should work in theory, but in practice it doesn't. Is the UI just not updating properly? – Michael Groleau Oct 11 '13 at 20:45
  • 1
    The post an SSCCE reproducing the problem. Note that describing your problems by "it doesn't work" doesn't help us much. – JB Nizet Oct 11 '13 at 20:55
1

The source of the Action event will be the JMenuItem that you clicked on so you can just use code like:

JMenuItem mi (JMenuItem)e.getSource();
menu.remove( mi );

Also, there is no need to create unique ActionListeners. You can create a shared listener with code like:

ActionListener removeItem = (new ActionListener() 
{
    public void actionPerformed(ActionEvent e) 
    {
        JMenuItem mi = (JMenuItem)e.getSource();

        int reply = JOptionPane.showConfirmDialog(null,
                "Are you sure you want to delete racer " + mi.getText() + "?", "Delete?",
                JOptionPane.YES_NO_OPTION);

        if (reply == JOptionPane.YES_OPTION)
        {
            r.delete();
            racerDelete.remove(mi);
        }
    }
};


for (final Racer r : Racer.getAllRacers()) 
{
    JMenuItem j = new JMenuItem(r.getName());
    racerDelete.add(j);
    j.addActionListener(removeItem);
}
camickr
  • 321,443
  • 19
  • 166
  • 288