- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- 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?
A JList is a subclass of JComponent class that allows the user to choose either a single selection or multiple selections. A JList class itself does not support scrollbar. In order to add scrollbar, we have to use 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. We can add items or elements to a JList by using addElement() method of DefaultListModel class. We can also add items or elements to a JList by reading an input value from a text field.
Example
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(); } }
Output
- Related Articles
- How can we add padding to a JTextField in Java?
- How to add ScrollBar to JList in Java?
- How to add JList to Scroll pane in Java?
- How to convert/read an input stream into a string in java?
- How to display a value when select a JList item in Java?
- Ways to read input from console in Java
- Can we save content from JTextField to a file in Java?
- How can we read from standard input in Java?
- Java Program to Read The Number From Standard Input
- How to populate an array one value at a time by taking input from user in Java?
- How to read data from scanner to an array in java?
- How to read a 2d array from a file in java?
- How can we add different font style items to JList in Java?\n
- How to read the data from a file in Java?
- How to read an input image and print it into an array in matplotlib?

Advertisements