I'm scratching my head over this one. Below is a running sample. I have 2 Swing Lists in a flow layout each with a simple data model. Depending on how I instantiate the FlowLayout the 2nd List does not display its elements. Weird.
import javax.swing.*;
import java.awt.*;
public class ListboxTest2 {
public static void main(String[] args) {
JFrame f = new JFrame();
f.setSize(500, 500);
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JPanel panel = new JPanel();
// this works
// panel.setLayout(new FlowLayout(FlowLayout.LEFT, 10, 10));
// this doesn't
panel.setLayout(new FlowLayout());
final JList list1 = new JList();
list1.setPreferredSize(new Dimension(120, 60));
final JList list2 = new JList();
list2.setPreferredSize(new Dimension(120, 60));
final String[] strings1 = {"Item 1", "Item 2", "Item 3", "Item 4", "Item 5", "Item 6", "Item 7", "Item 8", "Item 9", "Item 10", "Item 11", "Item 12"};
final String[] strings2 = {"Item 1", "Item 2", "Item 3", "Item 4", "Item 5", "Item 6", "Item 7", "Item 8", "Item 9", "Item 10", "Item 11", "Item 12"};
list1.setModel(new AbstractListModel() {
public int getSize() {
return strings1.length;
}
public Object getElementAt(int index) {
return strings1[index];
}
});
panel.add(list1);
list2.setModel(new AbstractListModel() {
public int getSize() {
return strings2.length;
}
public Object getElementAt(int index) {
return strings2[index];
}
});
panel.add(list2);
f.add(panel);
f.setVisible(true);
list1.grabFocus();
list1.setSelectedIndex(0);
}
}