How can I fix the number of rows being displayed regardless of the number of objects in my JList? For example, how would I go about displaying 5 rows in a JList when the JList contains 4 or fewer items?
1 Answer
You mean JList's setVisibleRowCount(int count) method?
This is all you need to do. For example, here's a JList with just 3 items, but 5 rows are showing due to the method above, and due to the JList being displayed within a JScrollPane:
import javax.swing.*; public class TestJLIst { public static void main(String[] args) { JList<String> list = new JList<>(new String[] {"A", "B", "C"}); list.setVisibleRowCount(5); JScrollPane scrollPane = new JScrollPane(list); JPanel panel = new JPanel(); panel.add(scrollPane); JOptionPane.showMessageDialog(null, panel); } }