双击JList项时填充JTextField [英] Populate JTextField when JList item is double clicked

查看:183
本文介绍了双击JList项时填充JTextField的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当用户双击项目时,我正在尝试从JList填充JTextArea.我不确定如何完成此操作,这是我到目前为止所拥有的.

I am trying to populate a JTextArea from a JList when the user double clicks the item. I am not completely sure how to accomplish this, here is what I have so far.

// Create Top Right JPanel and JList
        String[] listB = { "Some content on the right panel", "More content", "Some more content", "More and more content", "More and more content", "More and more content",
                "More and more content", "More and more content", "More and more content", "More and more content", "More and more content", "More and more content", "More and more content", "More and more content", 
                "More and more content" }; 
        final JList listBottom = new JList(listB);
        listBottom.setVisibleRowCount(12);
        JScrollPane scrollPaneB = new JScrollPane(listBottom);
        panelBottom.add(scrollPaneB);
        scrollPaneB.setViewportView(listBottom);
        scrollPaneB.setBorder(BorderFactory.createTitledBorder("Bottom Panel"));
        scrollPaneB.setVisible(true);
        //listBottom.setVisible(true);
        listBottom.setBorder(BorderFactory.createLoweredBevelBorder());

        // Create Top Right Action Listener
        listBottom.addListSelectionListener(new ListSelectionListener(){

            @Override
            public void valueChanged(ListSelectionEvent arg0) {
                Selection selectionB = (Selection)listBottom.getSelectedValue();

                textField.setText(selectionB.getContents());


            }

        });

推荐答案

您只需添加一个mouseClicked侦听器,然后检查鼠标是否单击了JList.

You simply add a mouseClicked listener, and check if the mouse clicks the JList.

    String[] listB = { "Some content on the right panel", "More content", "Some more content", "More and more content", "More and more content", "More and more content",
            "More and more content", "More and more content", "More and more content", "More and more content", "More and more content", "More and more content", "More and more content", "More and more content", 
            "More and more content" }; 


    final JList listBottom = new JList(listB);
    ....//other code
    listBottom.addMouseListener(new MouseAdapter(){

        //Called when you click the JList
        public void mouseClicked(MouseEvent e) {

            JList list = (JList)e.getSource();
            //Makes sure it only registers for single clicks(always registers even on double clicks, just registers twice.)
            if (e.getClickCount() == 1) {

                //Gets the point where you clicked and returns the index of the element at that point
                int index = list.locationToIndex(e.getPoint());
                //Makes sure that where you clicked was an element and that it is a String(We know it will be but its better to be safe)
                if(list.getModel().getElementAt(index) != null&&list.getModel().getElementAt(index) instanceof String){
                    //Populates your textField with the element at this index
                    textField.setText(list.getModel().getElementAt(index));
                }
            }
        }
    });

希望有帮助!

这篇关于双击JList项时填充JTextField的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆