Solved

BeanBox: ImageIcon doesn't show

Posted on 1998-02-22
1
216 Views
Last Modified: 2012-06-21
Could anybody tell me why the following bean (packed in
a jar of course) doesn't show the icon my.gif in the BeanBox,
only the JTextField is visible. When I instantiate MyBean
in a program, put it into a container of type JComponent
both are visible, the icon as well as the textfield.
Somehow the icon get's lost in the beanbox.

I attached the code of the bean and the MANIFEST of
the affiliated jar. Maybe there is a problem with
the paths, maybe the icon must be specified as some
kind of resource in the manifest, or the icon some-
how gets painted over (it is there but I cannot see
it) ...????


import com.sun.java.swing.*;
import com.sun.java.swing.event.*;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import java.awt.*;
import java.io.Serializable;


public class MyBean extends JPanel implements Serializable {
  JTextField textField;

  public MyBean() {
    setPreferredSize(new Dimension(300, 200));
    add(new JLabel(new ImageIcon("icons/my.gif")));
    textField = new JTextField(15);
    add(textField);
    textField.addKeyListener(new KeyAdapter() {
      ...
    });
  }

    /*public void paint(Graphics g) {
        g.setColor(beanColor);
        g.fillRect(20, 5, 20, 30);
    }*/

  public Dimension getMinimumSize() {
    return new Dimension(250, 100);
  }

  public void setString(String text) {
    textField.setText(text);
  }

  public String getString() {
    return textField.getText();
  }

  public Color getColor() {
    return beanColor;
  }

  public void setColor(Color newColor) {
    beanColor = newColor;
    repaint();
  }

  private Color beanColor = Color.blue;
}

-----------------------

Now for the manifest.mf:

Manifest-Version: 1.0

Name: MyBean.class
Java-Bean: True
Digest-Algorithms: SHA MD5
SHA-Digest: um+FrdYl5a3ZYMn/kXP1XzMgB+c=
MD5-Digest: mThC8cRAoDOAJ+w3Hpp0bQ==

Name: MyBean$1.class
Digest-Algorithms: SHA MD5
SHA-Digest: 6mMdpd/iW98NYJrgnf3bDUCp248=
MD5-Digest: TOfGXLJaAF8FzAOpZERvWA==

Name: icons/my.gif
Digest-Algorithms: SHA MD5
SHA-Digest: bn0lsJKG0RPy8QBUvLFgX4FfHHs=
MD5-Digest: D+tS5orsaKjInUE3W3rT1g==
0
Comment
Question by:Lewis
1 Comment
 
LVL 4

Accepted Solution

by:
russgold earned 300 total points
ID: 1233190
To show an icon in the BeanBox (or any bean builder for that matter), you must supply a BeanInfo class which defines the icon. In particular, you should subclass Java.beans.SimpleBeanInfo with a class named MyBeanBeanInfo which overrides the getIcon() method as follows:


public java.awt.Image getIcon(int iconKind) {
  java.awt.Image img = loadImage("icons/my.gif");
  return img;
  }


This assumes, of course, that your icon is the correct size.  You can use the iconKind parameter to select 16x16 icons or 32x32 icons.




0

Featured Post

Threat Intelligence Starter Resources

Integrating threat intelligence can be challenging, and not all companies are ready. These resources can help you build awareness and prepare for defense.

Join & Write a Comment

For customizing the look of your lightweight component and making it look opaque like it was made of plastic.  This tip assumes your component to be of rectangular shape and completely opaque.   (CODE)
For customizing the look of your lightweight component and making it look lucid like it was made of glass. Or: how to make your component more Apple-ish ;) This tip assumes your component to be of rectangular shape and completely opaque. (COD…
Viewers learn how to read error messages and identify possible mistakes that could cause hours of frustration. Coding is as much about debugging your code as it is about writing it. Define Error Message: Line Numbers: Type of Error: Break Down…
The viewer will learn how to implement Singleton Design Pattern in Java.

707 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

17 Experts available now in Live!

Get 1:1 Help Now