【问题标题】:Populate JTextField when JList item is double clicked双击 JList 项时填充 JTextField
【发布时间】:2014-06-30 22:37:53
【问题描述】:

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

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


            }

        });

【问题讨论】:

  • 我很好奇您是如何实现 Enter 键功能的?

标签: java swing mouseevent jlist


【解决方案1】:

您只需添加一个 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));
                }
            }
        }
    });

希望对你有帮助!

【讨论】:

    【解决方案2】:

    当用户双击项目时,我正在尝试从 JList 填充 JTextArea。

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

    查看List Action。当您double click 或使用Enter 键时,它将调用Action。您所要做的就是创建Action

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2012-10-12
      • 2015-06-19
      • 1970-01-01
      • 2012-01-14
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多