?
Solved

BeanBox: ImageIcon doesn't show

Posted on 1998-02-22
1
Medium Priority
?
223 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 600 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

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Question has a verified solution.

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

Introduction Java can be integrated with native programs using an interface called JNI(Java Native Interface). Native programs are programs which can directly run on the processor. JNI is simply a naming and calling convention so that the JVM (Java…
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…
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…
This video teaches viewers about errors in exception handling.
Suggested Courses
Course of the Month14 days, 12 hours left to enroll

771 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