Solved

BeanBox: ImageIcon doesn't show

Posted on 1998-02-22
1
221 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Introduction This article is the first of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article explains our test automation goals. Then rationale is given for the tools we use to a…
In this post we will learn different types of Android Layout and some basics of an Android App.
Viewers learn about the “for” loop and how it works in Java. By comparing it to the while loop learned before, viewers can make the transition easily. You will learn about the formatting of the for loop as we write a program that prints even numbers…
Viewers learn about the scanner class in this video and are introduced to receiving user input for their programs. Additionally, objects, conditional statements, and loops are used to help reinforce the concepts. Introduce Scanner class: Importing…

696 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