- 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 add JList to Scroll pane in Java?
To add JList to Scroll pane in Java, use JScrollPane:
JList list = new JList(sports); JScrollPane scrollPane = new JScrollPane(list);
After that set it to Container:
Container contentPane = frame.getContentPane(); contentPane.add(scrollPane, BorderLayout.CENTER);
The following is an example to add JList to Scroll pane:
Example
package my; import java.awt.event.*; import java.awt.*; import javax.swing.*; class SwingDemo extends JFrame { static JFrame frame; static JList list; public static void main(String[] args) { frame = new JFrame("JList Demo"); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); String sports[]= {"Tennis","Archery","Football","Fencing","Cricket","Squash","Hockey","Rugby"}; list = new JList(sports); JScrollPane scrollPane = new JScrollPane(list); Container contentPane = frame.getContentPane(); contentPane.add(scrollPane, BorderLayout.CENTER); frame.setSize(400,100); frame.setVisible(true); } }
The output is as follows. As you can see ScrollPane is visible for the items:
Output
- Related Articles
- How to add ScrollBar to JList in Java?
- How to create a scroll pane using JavaFX?
- Create JList and always display the scroll bar in Java?
- How can we add different font style items to JList in Java?\n
- How to read an input value from a JTextField and add to a JList in Java?
- How to store string array in Java JList?
- How to add scroll bar to an image in JavaFX?
- How to display row count in Java Swing JList
- How to select the second index in Java JList?
- How to create a left-right split pane in Java?
- How to create a top-bottom split pane in Java?
- Java Program to select the first item in JList
- How to display a value when select a JList item in Java?
- How to scroll down a webpage in selenium using Java?
- How to scroll down using Selenium WebDriver with Java?

Advertisements