This Bugzilla instance is a read-only archive of historic NetBeans bug reports. To report a bug in NetBeans please follow the project's instructions for reporting issues.

Bug 156115 - Icons are lost when pasting components between projects
Summary: Icons are lost when pasting components between projects
Status: NEW
Alias: None
Product: guibuilder
Classification: Unclassified
Component: Code (show other bugs)
Version: 6.x
Hardware: All All
: P3 blocker (vote)
Assignee: issues@guibuilder
Depends on:
Reported: 2008-12-28 22:36 UTC by ahristov
Modified: 2009-10-16 10:28 UTC (History)
0 users

See Also:
Issue Type: DEFECT
Exception Reporter:


Note You need to log in before you can comment on or make changes to this bug.
Description ahristov 2008-12-28 22:36:34 UTC
Product Version: NetBeans IDE 6.5 (Build 200811100001)

When a component that references an Icon is copied to a different project, the code generator emits a .setIcon(null)
regardless of whether the target project references the original project as part of its build path. Curiously, even with
that statement in the code, while the design view stays open, the IDE behaves as if the component had the icon and shows it.

Steps to reproduce

1) Create some project A, put an image file somewhere, create a JFrame with a JLabel and reference that image in the
icon property.
2) Create some project B. Create a JFrame. Project B may or may not reference Project A in its build path. The results
will be the same.
3) Select project A's JFrame and Copy the JLabel
4) Select project B's JFrame and Paste the previously copied component. In the code, setIcon(null) will be emitted,
though the GUI designer will think the label has an icon (and show it). Even in preview mode, the icon is shown.
5) After closing the editing window and reopening it again, the icon will be gone.

The proper behaviour should be, IMHO

- If project B contains a reference to project A,  simply keep the URL to the original resource
- If project B doesn't reference project A, offer the user these options
    * Add a reference and keep the original URL
    * Copy the resource to a user-selected package and rewrite the URL of the resource
    * Remove the icon

Currently the system always behaves in the last way, which is the most user-hostile of the possibilities.
Comment 1 Jiri Vagner 2009-01-02 10:45:28 UTC
This is a strange, I was trying to reproduce that generated code "setIcon(null)", but it works correctly.

I have project A with ...
 - "labelFull" component inside
      - icon was set using full disk path "D:\doc\icon2.jpg"
      - generated code ....setIcon(new javax.swing.ImageIcon("D:\\doc\\icon2.jpg"));

 - "labelRelative" component inside
      - icon was set using relative path "icon.jpg"
      - generated code ....setIcon(new javax.swing.ImageIcon(getClass().getResource("/icon.jpg")))
      - icon.jpg and form file are in the same folder

I have a project B, this project contains reference to project A.
I have a project C, this project doesn't reference to project A.

I built project A and then project B.

I was able to copy-paste "labelFull" component to project B and C, it always shows icon, generated code was valid.

I tried to copy-paste "labelRelative" component to B and C project. 
Inside project B valid icon was shown, generated code was valid.
Inside project C label didn't contain an icon and there were no setIcon() code generated inside initComponents() method

This behaviour sounds reasonably to me. Can you please attach more detailed steps to reproduce that strange
"setIcon(null)" behaviour?
Comment 2 ahristov 2009-01-02 13:33:44 UTC
Well, the steps are as basic as they get:

1) File-> New Project.
2) Select Java/Java Desktop Appliction. Click Next
3) Project Name : ProjectA. 
4) Repeat 1-3 for ProjectB
5) In the "Projects" View, right click on Project B. 
6) Select libraries.
7) In the Compile tab, select Add Project, double click on Project A
8) In the Run tab, select Add Project, double click on Project A
9) Use the Windows File Manager to locate some image, then right click on the file name -> Copy
10) Back in netbeans, right click on the "projecta" package -> Paste 
11) Open (in projectA) Place a JLabel on it. Click on the icon property to edit it. Select "Image
Within Project", and in the combo select the "projecta" package, then in the second combo select the name of the image.
Click ok
12) Optionally, Build projectA
13) Right click on the JLabel and select Copy
14) Open (in projectB) Right click on the frame and select Paste. The label appears with its icon
15) In the "Source" view, you will see that setIcon(null) has been generated.
16) If you click on "Preview Design", the label will be shown with its icon.
17) Close Open it again. The icon is gone now.

I have a Camstudio screen video capture of the issue, but it is 60Mb so unless you want it, I'm not attaching it. I'll
try to reduce it somehow

Comment 3 Jan Stola 2009-10-16 10:27:59 UTC
I followed your steps and the result depends on your 'optional' step 12. If the Project A is built then everything 
works fine (in the latest development build). If the Project A is not built then the copied label do not have icon set 
(e.g. no setIcon() call is generated - I wasn't able to reproduce your 'setIcon(null)' problem).

Hence, leaving this issue open because of the 'cleaned Project A' test-case. The root problem here seems to be the 
inability of the IconEditor to find the icon on the source path of a cleaned project (this project depends on). The 
following scenario shows more serious manifestation of this problem:

Let's assume that ProjectB depends on ProjectA and that ProjectA is cleaned.
1. Open IconEditor for some label in ProjectB.
2. Select icon from ProjectA (using 'Image Within Project' chooser)
   => the icon is correctly shown in the designer.
3. Save the corresponding form, close it and reopen it
   => the icon is gone.