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


Updated on: 30-Jul-2019

326 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements