如何将数据从JTextField发送到JList? [英] How to send data from a JTextField to a JList?

查看:82
本文介绍了如何将数据从JTextField发送到JList?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经集成了一个表单,用于在用户输入内容并按下 JButton 后将 JTextField 中的数据存储到变量中.我的问题是,每次他们输入内容并单击提交"按钮时,我都希望将此数据发送到 JList .

I have integrated a form to store the data from a JTextField to a variable after a user enters something and presses a JButton. My issue is that I want to send this data to a JList every time they type something in and hit the submit button.

这怎么可能?

推荐答案

将ActionListener添加到JButton的实例,获取JTextField的内容(例如,名称为aTextField),并将其存储到列表中(例如,您拥有类成员 JList dataInputted )

Add an ActionListener to the instance of the JButton, get the content of the JTextField (say with name aTextField), and store it to a list (say you have a class member JList dataInputted )

button.addActionListener(new ActionListener() {

            public void actionPerformed(ActionEvent e)
            {
                // Execute when button is pressed
                dataInputted.addElement(aTextField.getText());
            }
        });  

这篇关于如何将数据从JTextField发送到JList?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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