gianglaodai
gianglaodai

Reputation: 321

Can I getActionCommand from JMenu (not JMenuItem)?

The problem is I create a Popup Menu for people to select where he come from like this: Change location->country->city->district->town. With the last level town it is JMenuItem so it easy to use getActionCommand()(I send ID of location to ActionCommand), but sometime people only want to select from city level or district level, addActionListener() can't help me getActionCommand(). Is it have anyway to getActionCommand or get ID from city level, district level, or country level when user select it?

This is my code:

public class PopUpMenu extends JPopupMenu {

    JMenu changeLocation = null;

    public PopUpMenu() {
        changeLocation = new JMenu("Change Location");
        add(changeLocation);
        MenuActionListener listen = null;
        listen = new MenuActionListener();
        changeLocation.setAutoscrolls(true);
        for (JMenu countries : getCountry()) {
            changeLocation.add(countries);
        }
    }

    public Vector<JMenuItem> getTown(int ID) {
        Hashtable<Integer, String> v = null;
        Vector<JMenuItem> town = null;
        town = new Vector<>();
        v = new Hashtable<>();
        v.put(7, "town1");
        v.put(8, "town2");
        for (Map.Entry<Integer, String> entrySet : v.entrySet()) {
            Integer key = entrySet.getKey();
            String value = entrySet.getValue();
            JMenuItem jTown = new JMenuItem(value);
            jTown.setActionCommand("" + key + "");
            jTown.addActionListener(new MenuActionListener());
            town.add(jTown);
        }
        return town;
    }

    public Vector<JMenu> getDistrict(int ID) {
        Hashtable<Integer, String> v = null;
        Vector<JMenu> district = null;
        district = new Vector<>();
        v = new Hashtable();
        v.put(5, "district1");
        v.put(6, "district2");
        for (Map.Entry<Integer, String> entrySet : v.entrySet()) {
            Integer key = entrySet.getKey();
            String value = entrySet.getValue();
            JMenu jDistrict = new JMenu(value);
            for (JMenuItem districtes : getTown(key)) {
                jDistrict.add(districtes);

            }
            jDistrict.setActionCommand("" + key + "");
            jDistrict.addActionListener(new MenuActionListener());
            district.add(jDistrict);
        }
        return district;
    }

    public Vector<JMenu> getCity(int ID) {
        Hashtable<Integer, String> v = null;
        Vector<JMenu> city = null;
        city = new Vector();
        v = new Hashtable();
        v.put(3, "City1");
        v.put(4, "City2");
        for (Map.Entry<Integer, String> entrySet : v.entrySet()) {
            Integer key = entrySet.getKey();
            String value = entrySet.getValue();
            JMenu jCity = new JMenu(value);
            for (JMenu districties : getDistrict(key)) {
                jCity.add(districties);
            }
            city.add(jCity);
        }
        return city;
    }

    public Vector<JMenu> getCountry() {
        Hashtable<Integer, String> v = null;
        Vector<JMenu> country = null;
        country = new Vector();
        v = new Hashtable();
        v.put(1, "Country1");
        v.put(2, "Country2");
        for (Map.Entry<Integer, String> entrySet : v.entrySet()) {
            Integer key = entrySet.getKey();
            String value = entrySet.getValue();
            JMenu jCountry = new JMenu(value);
            for (JMenu cities : getCity(key)) {
                jCountry.add(cities);
            }
            country.add(jCountry);
        }
        return country;
    }
}

class MenuActionListener implements ActionListener {

    @Override
    public void actionPerformed(ActionEvent e) {
        System.out.println("Selected: " + e.getActionCommand());
    }
}

And this is run file:

public class Test extends JFrame {

    public Test() {
        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
        GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(layout.createParallelGroup(GroupLayout.Alignment.LEADING).addGap(0, 400, Short.MAX_VALUE));
        layout.setVerticalGroup(layout.createParallelGroup(GroupLayout.Alignment.LEADING).addGap(0, 300, Short.MAX_VALUE));
        pack();
        this.addMouseListener(new MouseAdapter() {

            @Override
            public void mouseClicked(MouseEvent e) {
                if (e.getButton() == MouseEvent.BUTTON3) {
                    doPop(e);
                }
            }

            public void doPop(MouseEvent e) {
                PopUpMenu menu = new PopUpMenu();
                menu.show(e.getComponent(), e.getX(), e.getY());
            }
        });
    }

    public static void main(String[] args) {
        new Test().setVisible(true);
    }
}

Upvotes: 0

Views: 272

Answers (1)

syllabus
syllabus

Reputation: 581

So, to rephrase @trashgod comments and mine (thanks @trashgod), you can use a MouseListener.mouseCLicked on your menus and pass the key as a constructor argument of the listener:

class MenuMouseListener extends MouseAdapter {

    private String id;

    public MenuMouseListener(String id) {
        this.id = id;
    }

    @Override
    public void mouseClicked(MouseEvent arg0) {
        System.out.println("Selected: " + id);
    }

}

instead of

jDistrict.setActionCommand("" + key + "");
jDistrict.addActionListener(new MenuActionListener());

you now have

jDistrict.addMouseListener(new MenuMouseListener("" + key + ""));

Upvotes: 1

Related Questions