JListmyList = new JList<>(new String[] {"apple", "banana", "orange", "kiwi"}); myList.setPreferredSize(new Dimension(150, 100));
JListIn this example, we create a JList with three Country objects (defined elsewhere), and set the preferred size to be 200 pixels wide by 150 pixels tall. The javax.swing package contains the Swing library.countryList = new JList<>(new Country[] {new Country("USA", "United States"), new Country("CAN", "Canada"), new Country("MEX", "Mexico")}); countryList.setPreferredSize(new Dimension(200, 150));