简体   繁体   English

双击JList项时填充JTextField

[英]Populate JTextField when JList item is double clicked

I am trying to populate a JTextArea from a JList when the user double clicks the item. 当用户双击项目时,我试图从JList填充JTextArea 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());


            }

        });

You simply add a mouseClicked listener, and check if the mouse clicks the JList. 您只需添加一个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));
                }
            }
        }
    });

Hope it helps! 希望能帮助到你!

I am trying to populate a JTextArea from a JList when the user double clicks the item. 当用户双击项目时,我试图从JList填充JTextArea。

When designing a GUI the user should be able to use the mouse or the keyboard to invoke an Action on a component. 在设计GUI时,用户应该能够使用鼠标或键盘来调用组件上的Action。

Check out List Action . 签出列表操作 It will invoke an Action when you double click or use the Enter key. 当您double click或使用Enter键时,它将调用一个Action All you have to do is create the Action . 您要做的就是创建Action

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

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