For your ImagePanel
class
super.paint[Component]
before all the other stuff.- Don't override
paint
but insteadpaintComponent
- Don't set properties in
paintComponent
method iesetOpaque()
. Beside,JPanel
is opaque by default - Override
getPreferredSize()
for painting on panels
For loadng images
Make a habit of not reading images from the file system, unless the application is specific to only your machine.
Instead read from the class path and make the image a resource by packaging it into the class path
Change your file structure
ProjectRoot src images 3D.jpg
Read from class path. Use
ImageIO
to make sure your path is correct. If it's invalid, an exception will be thrownURL url = getClass().getResource("/images/3D.jpg"); Image image = ImageIO.read(url);
For Netbeans GUI Builder
You can set the label icon using the design tool
- Select your label from the navigator or the design view.
- Go to the properties window in the right and find the property
icon
- click the ellipses button to the right of the property and a dialog will appear.
- Find your image and select OK (make sure your image is in a package in the src)
See related and maybe related