[英]Retrieving specific item from a JList
I have a list of JXHyperlinks, I need to retrieve them one by one and add to a panel 我有一个JXHyperlinks列表,我需要一个一个地检索它们并添加到面板中
the code is: 代码是:
for(int i=1; i<=lcol-1;i++)
{
a2=sheet.getCell(i,0);
stringy=a2.getContents();
testlink= new JXHyperlink(new Action(stringy));
list.add(testlink);
}
for(int i=0;i<list.getModel().getSize();i++)
{
panel2.add(list.getModel().getElementAt(i));
}
the first loop is creating the list of links with their respective names (taken from an excel file). 第一个循环是创建具有各自名称的链接列表(取自excel文件)。 The second loop takes the pre-made list and adds each object to a panel. 第二个循环获取预制列表,并将每个对象添加到面板。 The problem is that id doesn't add them. 问题是id不会添加它们。
You are confusing List
with JList
. 您将List
与JList
混淆了。 List#add is inhered from Collection, and adds a Object to itself. List#add从Collection继承,并向其自身添加一个Object。 JList#add is inhered from Container and adds a component to a Container. JList#add是从Container继承的,并将组件添加到Container。 So Jlist#add its like JPanel#add 所以Jlist#add就像JPanel#add
The problem is, you are adding the JXHyperlink
components directly to the list, not the list model. 问题是,您是将JXHyperlink
组件直接添加到列表中,而不是列表模型中。
for(int i=1; i<=lcol-1;i++)
{
a2=sheet.getCell(i,0);
stringy=a2.getContents();
testlink= new JXHyperlink(new Action(stringy));
// I'm the JList, not it's model :P
list.add(testlink);
}
// I bet nothing exists in the model
for(int i=0;i<list.getModel().getSize();i++)
{
panel2.add(list.getModel().getElementAt(i));
}
This is not how lists should work. 这不是列表应如何工作的方式。
Instead, add the link String
to the ListModel
and use the JXHyperlink
as a bases for a ListCellRenderer
, then add an instance of JXHyperlink
to the panel for each String
in the list 相反,将链接String
添加到ListModel
并使用JXHyperlink
作为ListCellRenderer
的基础,然后为列表中的每个String
将JXHyperlink
实例添加到面板中
See How to use lists for more details, in particular, Creating a model and Writing a Custom cell Renderer 有关更多详细信息,请参见如何使用列表 ,特别是创建模型和编写自定义单元格渲染器。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.