I have added a JList, and made it to only display 4 records at a time. If there are more than 4 records the user should be able to scroll and view other records. But in my case, i get to see all 8-10 records that i added. The code is not showing the first 4 records and the scroll bar. Can someone tell me what i'm missing ?
import java.awt.BorderLayout;
public class FrameTest {
private JPanel panel;
private JFrame frame;
private FrameTest ft;
private JList list;
public FrameTest() {
initComponents();
ft = this;
}
private void initComponents() {
frame = new JFrame("Test");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setExtendedState(JFrame.MAXIMIZED_BOTH);
panel = new JPanel();
panel.setBorder(new EmptyBorder(5, 5, 5, 5));
panel.setLayout(new BorderLayout(0, 0));
frame.getContentPane().add(panel);
list = new JList();
list.setVisibleRowCount(4);
list.setModel(new AbstractListModel() {
String[] values = new String[] {"adf", "gr", "rg", "g", "tg", "gt", "tg", "tg", "t", "gt", "gt"};
public int getSize() {
return values.length;
}
public Object getElementAt(int index) {
return values[index];
}
});
list.setSelectedIndex(1);
panel.add(list, BorderLayout.SOUTH);
frame.setLocationByPlatform(true);
frame.pack();
frame.setVisible(true);
}
}
note: Above is a simplified version of my code.