简体   繁体   English

将项目添加到JList

[英]adding items to JList

I want to add items to my List. 我想将项目添加到我的列表中。 My list is first initialized by initComponent() called automatically by instructor (I'm using NetBeans, and all GUI componenets are initialized by the prog automatically). 我的列表首先由讲师自动调用的initComponent()初始化(我正在使用NetBeans,并且所有GUI组件都由prog自动初始化)。

My questions is: 我的问题是:

  1. let's say that we have a Frame1, in this frame we have a Button "show images", when click on it 假设我们有一个Frame1,在此框架中,单击它时有一个“显示图像”按钮
  2. open Frame2 which has JList... 打开具有JList的Frame2 ...
  3. images list are added through Frame3 successfully... 图像列表已通过Frame3成功添加...

Below is my code where i want to list all images in my list: 以下是我要在列表中列出所有图像的代码:

private void setImagesToList()
{
    ***//bLayer is my Business Layer and _getNomOfSelectedImg() returns number of 
    //images.***

    int imagesCount = bLayer._getNomOfSelectedImg(); 

    ***// through my searches i fount that i've to create ListModel to hold my items*** 
    DefaultListModel listModel = new DefaultListModel();

    if (imagesCount > 0) // there is/are image(s)
    {
        for(int i=0; i < imagesCount ; i++)
        {
            // ***i want to add image name and tooltip (image path) ***
            String imgName = bLayer._getImageName(i);
            String imgPath = bLayer._getImagePath(i);
            listModel.add(i, imgName);
            break;
        }
        images_List.setModel(listModel);
    }
} 

when I run this code it throws NullPointerException in the last line images_List.setModel(listModel); 当我运行此代码时,它将在最后一行中抛出NullPointerException images_List.setModel(listModel);

What to do to display these items, allow multi-selection, adding mouse click event? 如何显示这些项目,允许多选,添加鼠标单击事件?

Yes, you can add tooltips. 是的,您可以添加工具提示。 You just have to set the tooltip text on the component returned by the renderer. 您只需在渲染器返回的组件上设置工具提示文本。 The JList will use those component tooltip's to determine the correct tooltip text. JList将使用那些组件工具提示来确定正确的工具提示文本。 This can be seen in the JList#getTooltipText implementation of which I copied the relevant part 这可以在我复制相关部分的JList#getTooltipText实现中看到。

Component rComponent = r.getListCellRendererComponent(
                       this, getModel().getElementAt(index), index,
                       lsm.isSelectedIndex(index),
                       (hasFocus() && (lsm.getLeadSelectionIndex() ==
                                       index)));

            if(rComponent instanceof JComponent) {
                MouseEvent      newEvent;

                p.translate(-cellBounds.x, -cellBounds.y);
                newEvent = new MouseEvent(rComponent, event.getID(),
                                          event.getWhen(),
                                          event.getModifiers(),
                                          p.x, p.y,
                                          event.getXOnScreen(),
                                          event.getYOnScreen(),
                                          event.getClickCount(),
                                          event.isPopupTrigger(),
                                          MouseEvent.NOBUTTON);

                String tip = ((JComponent)rComponent).getToolTipText(
                                          newEvent);

                if (tip != null) {
                    return tip;
                }

Could you also update your question with those new questions, as your 'answer with the new question' will float to the bottom 您还可以用这些新问题来更新您的问题吗,因为“新问题的答案”将浮动到底部

i found my great mistake :( :( i called the functions which set the images to the list before calling initComponent(), that's why the exception was thrown.. 我发现了我的大错误:( :(我在调用initComponent()之前调用了将图像设置为列表的函数,这就是引发异常的原因。

thnx all for your answer, but i have to more questions: 1) could i add ToolTipText to the list item, i want to add the image path 2) what did you mean about "my accept ratio"... 谢谢所有,但我还有其他问题:1)我可以将ToolTipText添加到列表项,我想添加图像路径2)您对“我的接受率”是什么意思...

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM