简体   繁体   English

无法将鼠标侦听器添加到包含列表的JScrollpane中

[英]Can't add a mouse listener to a JScrollpane which contains a list

I have a JScrollPane (myListScroll) that is added to a JPanel (which in turn is added to another JPanel before being added to a JFrame). 我有一个JScrollPane(myListScroll),它已添加到一个JPanel中(后者又被添加到另一个JPanel中,然后再添加到JFrame中)。 This JScrollPane (myListScroll) consists of a list of strings. 此JScrollPane(myListScroll)由字符串列表组成。 I want to be able to handle mouse events when clicking on the different items in this list. 我希望能够在单击此列表中的不同项目时处理鼠标事件。

In the code below I want to try if something happens if I click i the JScrollpane but nothing happens. 在下面的代码中,我想尝试一下,如果单击JScrollpane却发生了什么,但是什么也没发生。 What is wrong? 怎么了? Why is "test" not written? 为什么不写“测试”?

            JScrollPane myListScrol = new JScrollPane(myList);
        myListScrol.getViewport().addMouseListener(new MouseAdapter() {

            @Override
            public void mouseClicked(MouseEvent mouseEvent) {
                System.out.println("test");
            }
        });

我应该将侦听器添加到myList而不是myListScrol。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM