简体   繁体   中英

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).

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
  2. open Frame2 which has JList...
  3. images list are added through Frame3 successfully...

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);

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. This can be seen in the JList#getTooltipText implementation of which I copied the relevant part

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..

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"...

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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