Java 设置JList的最大容量?

Java 设置JList的最大容量?,java,jlist,swingx,Java,Jlist,Swingx,我正在尝试将我的JList的最大容量设置为5个元素。我似乎只能通过using.capacity()找到设置最小容量的方法。是否将最大值设置为5,并在超出此限制时显示对话框?到目前为止,我有: String getProcessName = ""; getProcessName = jTextField1.getText(); if (info.size() >= 5) { JOptionPane.showMessageDialog(null,"5 elements reached");

我正在尝试将我的
JList
的最大容量设置为5个元素。我似乎只能通过using.capacity()找到设置最小容量的方法。是否将最大值设置为5,并在超出此限制时显示对话框?到目前为止,我有:

String getProcessName = "";
getProcessName = jTextField1.getText();
if (info.size() >= 5)
{
  JOptionPane.showMessageDialog(null,"5 elements reached");
} 
else
{
  info.addElement(getProcessName);
}  
这将显示一条错误消息,但仍会将下一个值添加到此列表中。 有什么建议吗?

JFrame=newjframe();
JFrame frame = new JFrame();

//Other frame "stuff"

JList list = new JList(new Object[]{"121", "131", "141", "151" , "111", "181"});
    list.setSelectionModel(new MySelectionModel(list, 5)); //<-- This is the magic line right here

    frame.setVisible(true);
//其他框架“东西” JList list=newjlist(新对象[]{“121”、“131”、“141”、“151”、“111”、“181”});
list.setSelectionModel(新的MySelectionModel(列表,5))// 您必须创建自己的ListSelectionModel,该模型能够根据您的要求主动拒绝或调整选择

它不应该添加元素,您确定您没有在其他地方添加元素吗?
JList
是以编程方式填充的,而不是由用户填充的。所以你可以完全控制里面有多少物品。您已经拥有的代码看起来很模糊,就像您需要检查的代码,您没有超过自己的最大值。