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 display the JList items from top to bottom and left to right in Java?
For this, set the layout orientation to the following −
setLayoutOrientation(JList.VERTICAL_WRAP);
The following is an example to display the JList items from top to bottom and left to right −
Example
package my;
import java.awt.BorderLayout;
import java.util.ArrayList ;
import java.util.List;
import javax.swing.JFrame;
import javax.swing.JList;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
public class SwingDemo {
public static void main(String[] args) {
JPanel panel = new JPanel(new BorderLayout());
List<String> myList = new ArrayList<>(10);
for (int index = 0; index < 20; index++) {
myList.add("List Item " + index);
}
final JList<String> list = new JList<String>(myList.toArray(new String[myList.size()]));
JScrollPane scrollPane = new JScrollPane();
scrollPane.setViewportView(list);
list.setLayoutOrientation(JList.VERTICAL_WRAP);
panel.add(scrollPane);
JFrame frame = new JFrame("Demo");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.add(panel);
frame.setSize(500, 250);
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
}
Output

Advertisements