Monday, July 18, 2011

How to add label icon in to our Swing List box

How to add label icon in to our Swing Jlist box
----------------------------------------------------
Steps.
-----
1.create a class(here OnlineUserCellRenderer) and implement ListCellRenderer.

  class OnlineUserCellRenderer extends JLabel implements 
  ListCellRenderer {

        private final Color HIGHLIGHT_COLOR = new Color(0, 0, 128);

        public OnlineUserCellRenderer() {
            setOpaque(true);
            setIconTextGap(12);
        }

        public Component getListCellRendererComponent(JList list, Object value,
                int index, boolean isSelected, boolean cellHasFocus) {
            TestImageList entry = (TestImageList) value;
            setText(entry.getUser());
            if (entry.getImage() != null) {
                setIcon(entry.getImage());
            }
            setIconTextGap(12);
            if (isSelected) {
                setBackground(HIGHLIGHT_COLOR);
                setForeground(Color.white);
            } else {
                setBackground(Color.white);
                setForeground(Color.black);
            }
            return this;
        }
    }

2. Then create a TestImageList class.This class is used to set
   properties for label.
    class TestImageList {

        private final String Name;
        private ImageIcon image;

        public TestImageList(String Name, ImageIcon image) {
            this.Name = Name;
            this.image = image;
        }

        public String getName() {
            return Name;
        }

        public ImageIcon getImage() {
            return image;
        }

        // Override standard toString method to give a useful result
        public String toString() {
            return Name;
        }
    }

3.  Declare DefaultListModel object for inserting data in to jlist
    box in our class import javax.swing.DefaultListModel.and 
    declare a object for DefaultListModel.
   
    public DefaultListModel model = new DefaultListModel();
   
4.  Then add elements in to our DefaultListModel for insert it in
    to swing jList box.
   
    model.add(comboCount, new OnlineUserListEntry(Name, newIcon));
    lstChatlist.setModel(model);
    OnlineUserCellRenderer labelUser = new OnlineUserCellRenderer();
    lstChatlist.setCellRenderer(labelName);
      

How to resize image using java.

How to resize image using java.


Steps

1. Import useful libraries in to our class.
    here we need

    import java.awt.image.BufferedImage;
    import javax.swing.Icon;
    import javax.swing.ImageIcon;


2. Then we need to create a function ie 'imageIconResize'
   for resizing the image.and we need to pass some parameters in to
   that function
   
   here is the function.


  public BufferedImage imageIconResize(Image img,int postionX,int 
  postionY, int width, int hight)

  {

  BufferedImage bi = new BufferedImage(img.getWidth(null),
  img.getHeight(null),  BufferedImage.SCALE_FAST);
  Graphics g = bi.createGraphics();
  g.drawImage(img, postionX, postionY, width, hight, null, null);
  return bi;


  }
3. Then pass the parameters in to our function. for that we must
   have a image.
   In my example i have byte array of image.then convert it to
   ImageIcon.

   ie

  iconImage = new ImageIcon(bytes);
   
   I use getImage() function for get image from icon object.
   
  iconImage = new ImageIcon(bytes);
  Image img = iconImage.getImage();


4. So we have a image then we call the function it return a
   BufferedImage object.
   we need to convert it to ImageIcon.
   
 BufferedImage imageIconResize = imageIconResize(img,10,15, 50, 50);
 ImageIcon newIcon = new ImageIcon(imageIconResize);

   
5. Changing the value of parameters will change the size of the
   image.