
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
How to read an input value from a JTextField and add to a JList in Java?
In this article, we will learn to read an input value from a JTextField and add it to a JList in Java. The Swing's two commonly used components are JTextField for text input and JList for displaying a list of items for building graphical user interfaces.
JList
A JList is a subclass of the JComponent class that allows the user to choose either a single selection or multiple selections. The JList class itself does not support a scrollbar.
In order to add a scrollbar, we have to use the JScrollPane class together with the JList class. The JScrollPane then manages a scrollbar automatically. A DefaultListModel class provides a simple implementation of a list model, which can be used to manage items displayed by a JList control.
addElement() Method
The addElement() method of the DefaultListModel class adds the specified component to the end of this list.
public void addElement(E element)
Reading and Adding Input Values into a JList from a JTextField
We can add items or elements to a JList by using the addElement() method of the DefaultListModel class. We can also add items or elements to a JList by reading an input value from a text field.
The following are the steps for reading an input value from a JTextField and adding it to a JList in Java:
JTextField Creation
Creates a JTextField with the text "Type something and Hit Enter" and stores it into a variable named "jtf".
jtf = new JTextField("Type something and Hit Enter");
Mouse Listener for Text Field
Removes the default text using the MouseListener when the user clicks on the text field, and uses MouseAdapter to respond to the mouse click events.
jtf.addMouseListener(new MouseAdapter() { public void mouseClicked(MouseEvent me) { jtf.setText(""); } });
List Configuration
Creates a JList named "list" using the model, and sets light gray as the background color for the JList.
list = new JList(model); list.setBackground(Color.lightGray);
Adding the Text into a JList
The ActionListener is triggered when the user presses Enter, then adds the text to the list model using the addElement() method, and shows a confirmation dialog with the entered text using the JOptionPane dialogue box.
jtf.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent event) { model.addElement(jtf.getText()); JOptionPane.showMessageDialog(null, jtf.getText()); jtf.setText("Type something and Hit Enter"); } });
Placing the JList and JTextField
Adds the JTextField component to the top of the JFrame, wraps the list using the JScrollPane to allow scrolling, and positions the list in the center of the frame using the BorderLayout.
add(jtf, BorderLayout.NORTH);
add(new JScrollPane(list), BorderLayout.CENTER);
Example
Below is an example of reading an input value from a JTextField and adding it to a JList in Java:
import java.awt.*; import java.awt.event.*; import javax.swing.*; public class JTextfieldToJListTest extends JFrame { private DefaultListModel model; private JList list; private JTextField jtf; public JTextfieldToJListTest() { setTitle("JTextfieldToJList Test"); model = new DefaultListModel(); jtf = new JTextField("Type something and Hit Enter"); jtf.addMouseListener(new MouseAdapter() { public void mouseClicked(MouseEvent me) { jtf.setText(""); } }); list = new JList(model); list.setBackground(Color.lightGray); jtf.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent event) { model.addElement(jtf.getText()); JOptionPane.showMessageDialog(null, jtf.getText()); jtf.setText("Type something and Hit Enter"); } }); add(jtf,BorderLayout.NORTH); add(new JScrollPane(list),BorderLayout.CENTER); setSize(375, 250); setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); setLocationRelativeTo(null); setVisible(true); } public static void main(String[] args) { new JTextfieldToJListTest(); } }