public void paintItems()
     {
         ArrayList inRoomItems = ((Room) scenery.getCurrentRoom()).getItemsArray();
         Point origin = new Point(380, 10);
         int layerNumber = 50;
         int numberHorizontally = 0;
         int n=inRoomItems.size();
         for(int i=0; i<n; i++){
             image = new JLabel(((Item) inRoomItems.get(i)).getImageIcon());
             imagePane.add(image, new Integer(layerNumber));
             image.setOpaque(false);
             image.setBounds(origin.x, origin.y, 100, 100);
             origin.y += 90;
             numberHorizontally++;
             if (numberHorizontally % 3 ==0){
                 origin.x -= 100;
                 origin.y = 10;
                }
                layerNumber ++;
            }
        }

I am having a problem displaying images in GUI. I have a borderLayout, and on the right (.EAST) I am displaying ImageIcons of my inventory. here is the code I am using:

        public void paintInventoryItems()
    {
        itemPane.removeAll();
        setInventoryLabel();
        HashMap items = scenery.getInventory();
        Iterator it = items.keySet().iterator();
        while (it.hasNext()){
            String key = (String) it.next();
            JLabel item = new JLabel (((Item) items.get(key)).getImageIcon());
            item.setBorder(new EmptyBorder(4,0,4,0));
            itemPane.add(item);
        }
        frame.pack();
    }

The problem is that each ImageIcon appears with a different size, so one is small, and others are big and the first doesnt start at the top, but in the middle!

any help with setting the size and the coordination they appear with?

thanks!

Edited 3 Years Ago by peter_budo: Editing so text is separated from code

I used setBounds() method, set the size to a constant for each item, but I still get a difference in size!

Do your original images that used as icons are different sizes? If so, you may need to scale it before you add to the JLabel, or simply scale the size of images and resave them yourself.

This question has already been answered. Start a new discussion instead.