JList: horizontal wrap not working with 5 or more items per row

S

Scott Steiner

Hi,

I have a JList with a HORIZONTAL_WRAP LayoutOrientation. All is fine but
when I want to display 5 or more items in a row then the JList only
displays 4 items in a row and then make a wrap. How can I display 5
elements in each row? Here is the simple copy & paste code:

import java.awt.Container;
import javax.swing.*;

public class Test extends JFrame
{
public Test()
{
Container c = this.getContentPane();
c.setLayout(null);
this.setSize(200,200);

DefaultListModel listModel = new DefaultListModel();
listModel.addElement("1");
listModel.addElement("2");
listModel.addElement("3");
listModel.addElement("4");
listModel.addElement("5");
listModel.addElement("6");
listModel.addElement("7");

JList sourceList = new JList(listModel);
sourceList.setLayoutOrientation(JList.HORIZONTAL_WRAP);
sourceList.setBounds(10,10,100,100);

int rowCount = listModel.getSize() / 5;
if ((listModel.getSize() % 5) != 0) rowCount += 1;
sourceList.setVisibleRowCount(rowCount);
c.add(sourceList);

this.setVisible(true);
}

public static void main(String[] args)
{
new Test();
}
}
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,968
Messages
2,570,150
Members
46,696
Latest member
BarbraOLog

Latest Threads

Top