File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Swing / AWT / SWT and the fly likes Using images within a JPanel Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

Win a copy of Java Interview Guide this week in the Jobs Discussion forum!
JavaRanch » Java Forums » Java » Swing / AWT / SWT
Bookmark "Using images within a JPanel" Watch "Using images within a JPanel" New topic

Using images within a JPanel

Shane Lilly

Joined: Feb 20, 2004
Posts: 18
I am drawing a menu bar on the side of a canvas. I have a series of JLabels describing what various key strokes do.
However below this I would like to draw a key showing what colours in the canvas represent.
Is there any way that I have do something where I have a small image then beside it some text describing what the color represents?
Say the box below is the JPanel...

|<image><JLabel> |
|<image><JLabel> |
|<image><JLabel> |
|<image><JLabel> |
| |
| |
| |
This is the effect that I would like to get.
Nathan Pruett

Joined: Oct 18, 2000
Posts: 4121

You can make the icon yourself by just by making a class that implements Icon and takes a Color, and just paints a rectangle of that color in the paintIcon() method. JLabels can have both text and an Icon, so you can just set them both in a JLabel. The default is for the icon to be on the left of the text on the label, so you won't have to mess with setting any of that differently.

Write once, run anywhere, because there's nowhere to hide! - /. A.C.
I agree. Here's the link:
subject: Using images within a JPanel
It's not a secret anymore!