Question

I have simple sample of code. How can I to add Mouse Listener for displaying simple menu(when i clicked on right mouse button) for selected JList item? In addition: how can I to add Mouse Listener for left mouse button?

Like this: enter image description here

Here is my code:

package test;

import java.awt.Dimension;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.ArrayList;

import javax.swing.AbstractListModel;
import javax.swing.JFrame;
import javax.swing.JList;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.JScrollPane;
import javax.swing.SwingUtilities;

public class Test implements ActionListener {

    private JList list;
    private MyJListModel model;

    public Test() {
        JFrame frame = new JFrame("Test");
        frame.setMinimumSize(new Dimension(400,200));

        JMenuBar menubar = new JMenuBar();
        JMenu filemenu = new JMenu("File");
        JMenuItem addItem = new JMenuItem("Add");
        filemenu.add(addItem);
        menubar.add(filemenu);

        addItem.addActionListener(this);

        model = new MyJListModel();
        list = new JList(model);
        JScrollPane scrollpane = new JScrollPane(list);

        frame.add(scrollpane);
        frame.setJMenuBar(menubar);
        frame.setVisible(true);
    }

    public void actionPerformed(ActionEvent event) {
        String pushedItem = event.getActionCommand();
        if(pushedItem.equals("Add")) {
            model.addElement("Item");
        }
    }

    public static void main(String[] args) {
        SwingUtilities.invokeLater(new Runnable() {
            public void run() {
                new Test();
            }
        });
    }
}

class MyJListModel extends AbstractListModel {
    private ArrayList<String> arraylist = new ArrayList<String>();

    public MyJListModel() {
        super();
    }

    @Override
    public int getSize() {
        return arraylist.size();
    }

    @Override
    public Object getElementAt(int i) {
        return arraylist.get(i);
    }

    public void addElement(String string) {
        arraylist.add(string);
        this.fireContentsChanged(this, 0, arraylist.size() - 1);
    }
}
Was it helpful?

Solution

Heres a great article tutorial i found that helped me to accomplish what your trying to accomplish.

http://docs.oracle.com/javase/tutorial/uiswing/components/menu.html#popup

i also found that link from this stack overflow article

How do I create a right click context menu in Java Swing?

Hope this helps

OTHER TIPS

You add the mouselistener to the table, not the model.

 Table.addMouseListener(new MouseAdapter()
    {
        public void mouseReleased(MouseEvent e)
        {
            if(e.isPopupTrigger())
            {
                onShowPopup(e);
            }
        }

        public void mousePressed(MouseEvent e)
        {
            if(e.isPopupTrigger())
            {
                onShowPopup(e);
            }
        }
    });
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top