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 197382 - Handling Images in A GUI Application
Summary: Handling Images in A GUI Application
Status: RESOLVED FIXED
Alias: None
Product: usersguide
Classification: Unclassified
Component: Form (show other bugs)
Version: 7.0
Hardware: PC Other
: P3 normal (vote)
Assignee: AlyonaStashkova
URL: http://netbeans.org/kb/docs/java/gui-...
Keywords:
Depends on:
Blocks:
 
Reported: 2011-04-04 08:07 UTC by AlyonaStashkova
Modified: 2012-04-20 08:50 UTC (History)
1 user (show)

See Also:
Issue Type: DEFECT
Exception Reporter:


Attachments

Note You need to log in before you can comment on or make changes to this bug.
Description AlyonaStashkova 2011-04-04 08:07:35 UTC
email cited:

"For instance, you have a tutorial that shows how to import an image to a
project by using a jButton. But, what if I want to add an image and make it
clickable via x, y coordinates? I am unable to do this using the jButton
method.

Also, what if I want to place an image as the background on the frame? The
gui builder does not allow me to do this.

Any guidance or assistance would be appreciated."
Comment 1 Jan Stola 2011-11-29 10:45:38 UTC
> "For instance, you have a tutorial that shows how to import an image to a
> project by using a jButton. But, what if I want to add an image and make it
> clickable via x, y coordinates? I am unable to do this using the jButton
> method.

If you want to display some image and respond to mouse clicks over this image then I suggest you to display the image using JLabel (as it is shown in the tutorial) and register event handlers for mouse events on this label. In other words, invoke Events > Mouse > mouseClicked/mousePressed/mouseReleased from the context menu of the JLabel => event handler is generated for the corresponding event. You can get the mouse coordinates (i.e., the location of the mouse click) in the event handler using evt.getPoint()/getX()/getY() methods.
Comment 2 Jan Stola 2011-11-29 13:07:51 UTC
> Also, what if I want to place an image as the background on the frame? The
> gui builder does not allow me to do this.

There is no direct support for background image of JFrame in GUI Builder. In fact, there is no direct support for background image of JFrame in Swing itself. On the other hand, there are various (indirect) ways how to achieve this.

For example, you can place custom JPanel (with overriden paintComponent() method) into the frame and use it as a parent of all your components:

1. Create JFrame.
2. Set its layout to BorderLayout.
3. Add JPanel into the frame (it will cover the whole content
   of the frame because it is placed in the center area of BorderLayout).
4. Add 'private ImageIcon image;' field into the frame
   and initialize the this field in the constructor.
5. Invoke Customize Code... from the context menu of the panel.
6. Switch the combo-box next to initialization code of the panel from
   'default code' to 'custom creation' and insert:

new javax.swing.JPanel(){
    protected void paintComponent(Graphics g) {
        super.paintComponent(g);
        g.drawImage(image.getImage(), 0, 0, null);
    }
}

Another approach is to place JLabel with the image into the frame and place a transparent JPanel over this JLabel and use this JPanel as the parent of all your components:

1. Create JFrame.
2. Set its layout to GridBagLayout.
3. Add JPanel into the frame.
4. Uncheck its opaque property (i.e., set it to false).
5. Add JLabel into the frame and set its icon property.
6. Change layout properties of both JPanel and JLabel in the following way:
   Grid X = 1, Grid Y = 1, Fill = Both, Weight X = 1, Weight Y = 1

In both cases, you have to place your components into the JPanel (and its subcontainers).
Comment 3 AlyonaStashkova 2012-04-20 08:50:04 UTC
The Handling Images in a Java GUI Application tutorial was enhanced with the Displaying the Image as the Background on the Frame section (http://netbeans.org/kb/docs/java/gui-image-display.html#background).