双击JList项时填充JTextField

时间:2014-06-30 22:37:54

标签: java swing mouseevent jlist

当用户双击该项目时,我尝试从JTextArea填充JList。我不完全确定如何实现这一点,这是我到目前为止所做的。

// 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());


            }

        });

2 个答案:

答案 0 :(得分:4)

您只需添加一个mouseClicked侦听器,并检查鼠标是否单击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));
                }
            }
        }
    });

希望它有所帮助!

答案 1 :(得分:0)

  

我试图在用户双击项目时从JList填充JTextArea。

在设计GUI时,用户应该能够使用鼠标或键盘来调用组件上的Action。

结帐List Action。当您Action或使用double click密钥时,它会调用Enter。您所要做的就是创建Action