Solved

BeanBox: ImageIcon doesn't show

Posted on 1998-02-22
1
217 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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

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…
Java functions are among the best things for programmers to work with as Java sites can be very easy to read and prepare. Java especially simplifies many processes in the coding industry as it helps integrate many forms of technology and different d…
Viewers learn about the third conditional statement “else if” and use it in an example program. Then additional information about conditional statements is provided, covering the topic thoroughly. Viewers learn about the third conditional statement …
This tutorial covers a practical example of lazy loading technique and early loading technique in a Singleton Design Pattern.

930 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

10 Experts available now in Live!

Get 1:1 Help Now