aspose file tools*
The moose likes Swing / AWT / SWT and the fly likes Display text over an icon in Jlabel Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Swing / AWT / SWT
Bookmark "Display text over an icon in Jlabel" Watch "Display text over an icon in Jlabel" New topic
Author

Display text over an icon in Jlabel

Deepu James
Greenhorn

Joined: Oct 27, 2008
Posts: 6
Hi,

I have Jlabel with mutiple icons over it. I have created a new class extending Icon to achive this. Now I would like to display the icon image number over the image. Is there any option of Extending setText to diplay text over image icon in a Jlabel to display it in correct x, y coordinates?

Thanks
DJ
Maneesh Godbole
Saloon Keeper

Joined: Jul 26, 2007
Posts: 10451
    
    8

Originally posted by Deepu James:
Is there any option of Extending setText to diplay text over image icon in a Jlabel to display it in correct x, y coordinates?


I think not.
But you can still achieve it by tweaking the paintComponent()


[How to ask questions] [Donate a pint, save a life!] [Onff-turn it on!]
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 39380
    
  28
Can you alter transparency and put two Components on top of each other?
Deepu James
Greenhorn

Joined: Oct 27, 2008
Posts: 6
How to do it? Do you have any code sample to do it

Thank you very much for a quick reply.
Deepu James
Greenhorn

Joined: Oct 27, 2008
Posts: 6
It is not problem to display text over another component... I dont care whether it would be transperant or not. I want to plce image numbers over images displayed. The only problem is postioning text in the desired location.

Thanks very much for your help

Kind Regards
DJ
pete stein
Bartender

Joined: Feb 23, 2007
Posts: 1561
Originally posted by Deepu James:
It is not problem to display text over another component... I dont care whether it would be transperant or not. I want to plce image numbers over images displayed. The only problem is postioning text in the desired location.


Since you've already solved one portion of this problem, I think that it would help all of us here if you posted the code that you currently have that is not working. In fact, you would serve us best by creating a very small program that is compilable, demonstrates how you are placing text over an image, yet also demonstrates how this is not working fully to your liking and why. If you do this, you will have the greatest chance of getting useful help.

This technique is called creating an SSCCE. For more info, please check out this link: SSCCE

Good luck!
Deepu James
Greenhorn

Joined: Oct 27, 2008
Posts: 6
hi see th efollowing code which code of multi ICon class chechi can display multiple icons over JLabel. Now I want display Text..( image numbers ) over the icons. with Jlabel setText() i am not able to postion it correctly. Is there any way to display image unumber(SetText()) below or over the icons Added over a Jlabel.

Any sample code is highly appricated.

here is the code of MultiIcon I am using
package ateportal;

import java.awt.Component;
import java.awt.Graphics;
import javax.swing.Icon;
import javax.swing.ImageIcon;
import javax.swing.JOptionPane;

/**
*
* @author deepuj
*/
public class MultiIcon implements Icon {

public static final int ALIGNMENT_HORIZONTAL = 1;
public static final int ALIGNMENT_VERTICAL = 2;
public static final int ALIGNMENT_STACKED = 3;

public ImageIcon thumbNail1 = null;
public ImageIcon thumbNail2 = null;
public ImageIcon thumbNail3 = null;

public ImageIcon boxPosIcon1 = null;
public ImageIcon boxPosIcon2 = null;
public ImageIcon boxPosIcon3 = null;
private int gap = 3; // will be ignored for stacked alignment

private Icon[] icons;
private int alignment;
private int width = 0;
private int height = 0;

public MultiIcon() {
this(ALIGNMENT_HORIZONTAL);
}

public MultiIcon(int alignment) {
this.alignment = alignment;
}

public int getIconCount() {
if (icons == null) {
return 0;
} else {
return icons.length;
}
}

public Icon getIconAt(int index) {
return icons[index];
}

public int getIconAt(int x, int labelx) {
//int y = point.y;
int firstIconWidth = getIconAt(0).getIconWidth();
//JOptionPane.showMessageDialog(null, "firstIconWidth = " +firstIconWidth);
int secondIconWidth = getIconAt(1).getIconWidth();
int thirdIconWidth = getIconAt(2).getIconWidth();
int fourthIconWidth = getIconAt(3).getIconWidth();
int fifthIconWidth = getIconAt(4).getIconWidth();
//JOptionPane.showMessageDialog(null, "fourthIconWidth = " +fourthIconWidth);
//JOptionPane.showMessageDialog(null, "fifthIconWidth = " +fifthIconWidth);
if (x - labelx > 0) {
if (x - (labelx + firstIconWidth) > 0) {
if (x - (labelx + firstIconWidth + secondIconWidth+(gap*1)) > 0) {
if (x - (labelx + firstIconWidth + secondIconWidth + thirdIconWidth+(gap*2)) > 0) {
if (x - (labelx + firstIconWidth + secondIconWidth + thirdIconWidth + fourthIconWidth+(gap*3)) > 0) {
if (x - (labelx + firstIconWidth + secondIconWidth + thirdIconWidth + fourthIconWidth+ fifthIconWidth +(gap*4)) > 0){
//outside
return 0;
}else{
return 5;
}

} else {
return 4;
}

} else {
return 3;
}

} else {
return 2;
}

} else {
return 1;
}

} else {
return 0;
}

}
// public int getIconAt(int x, int y, int labelx, int labely) {
// //int y = point.y;
// int firstIconWidth = getIconAt(0).getIconWidth();
// int secondIconWidth = getIconAt(1).getIconWidth();
// int thirdIconWidth = getIconAt(2).getIconWidth();
// int fourthIconWidth = getIconAt(3).getIconWidth();
// int fifthIconWidth = getIconAt(4).getIconWidth();
//
// if (x - labelx > 0) {
// if (x - (labelx + firstIconWidth) > 0) {
// if (x - (labelx + firstIconWidth + secondIconWidth+(gap*1)) > 0) {
// if (x - (labelx + firstIconWidth + secondIconWidth + thirdIconWidth+(gap*2)) > 0) {
// if (x - (labelx + firstIconWidth + secondIconWidth + thirdIconWidth + fourthIconWidth+(gap*3)) > 0) {
// if (x - (labelx + firstIconWidth + secondIconWidth + thirdIconWidth + fourthIconWidth+ fifthIconWidth +(gap*4)) > 0){
// //outside
// return 0;
// }else{
// return 5;
// }
//
// } else {
// return 4;
// }
//
// } else {
// return 3;
// }
//
// } else {
// return 2;
// }
//
// } else {
// return 1;
// }
//
// } else {
// return 0;
// }
//
// }

public void replaceIconAt(int index, Icon newIcon) {
icons[index] = newIcon;
width = calculateIconWidth();
height = calculateIconHeight();
}

public void addIcon(Icon icon) {
if (icon == null) {
return;
} else {
if (icons == null) {
icons = new Icon[]{icon};
} else {
Icon[] newIcons = new Icon[icons.length + 1];
System.arraycopy(icons, 0, newIcons, 0, icons.length);
newIcons[newIcons.length - 1] = icon;
icons = newIcons;
}
width = calculateIconWidth();
height = calculateIconHeight();
}
}

public void paintIcon(Component c, Graphics g, int x, int y) {
if (icons == null) {
return;
} else if (alignment == ALIGNMENT_VERTICAL) {
int yIcon = y;
for (int i = 0; i < icons.length; i++) {
Icon icon = icons[i];
int xIcon = x + (width - icon.getIconWidth()) / 2;
icon.paintIcon(c, g, xIcon, yIcon);
yIcon += icon.getIconHeight() + gap;
}
} else if (alignment == ALIGNMENT_STACKED) {
for (int i = 0; i < icons.length; i++) {
Icon icon = icons[i];
int xIcon = x + (width - icon.getIconWidth()) / 2;
int yIcon = y + (width - icon.getIconWidth()) / 2;
icon.paintIcon(c, g, xIcon, yIcon);
}
// for (int i = 0; i < icons.length; i++) {
// Icon icon = icons[i];
// int xIcon = x;
// int yIcon = y;
// if(i==1)
// {
// xIcon = x ;
// yIcon = y + (icon.getIconHeight()) / 2 + (icon.getIconHeight()) / 3 + (icon.getIconHeight()) / 3;
// icon.paintIcon(c, g, xIcon, yIcon);
// }
// else{
// //Icon icon = icons[i];
// // yIcon =y;
// xIcon = x + (width - icon.getIconWidth()) / 2;
// icon.paintIcon(c, g, xIcon, yIcon);
// yIcon += icon.getIconHeight() + gap;
// }
//
//
// }
} else {
assert alignment == ALIGNMENT_HORIZONTAL;
ImageIcon left = new ImageIcon("C:\\Documents and Settings\\Deepu James\\Desktop\\1.png");
int xIcon = x;
for (int i = 0; i < icons.length; i++) {


//yIcon = y + (height - icon.getIconHeight()) / 2;
//end of code
Icon icon = icons[i];
int yIcon = y -10 ;//+ (height - icon.getIconHeight()) / 2;
icon.paintIcon(c, g, xIcon, yIcon);
// code
// if(i!=0 && i!=4)
// {
// //yIcon = y;
// //xIcon = x;
// Icon icon1 = null;
// if (i==1)
// icon1 =this.boxPosIcon1;
// else if(i==2)
// icon1 =this.boxPosIcon2;
// else if(i==3)
// icon1 =this.boxPosIcon3;
// yIcon = yIcon + icon.getIconHeight()+3;
// int dx = xIcon + icon.getIconWidth()/2 - icon1.getIconWidth()/2;
// icon1.paintIcon(c, g, dx, yIcon);
// }
xIcon += icon.getIconWidth() + gap;
}

}
}

public int calculateIconWidth() {
if (icons == null) {
return 0;
} else if (alignment == ALIGNMENT_HORIZONTAL) {
int width = 0;
for (int i = 0; i < icons.length; i++) {
width += icons[i].getIconWidth();
}
width += gap * (icons.length - 1);
return width;
} else {
int width = 0;
for (int i = 0; i < icons.length; i++) {
width = Math.max(width, icons[i].getIconHeight());
}
return width;
}
}

public int calculateIconHeight() {
if (icons == null) {
return 0;
} else if (alignment == ALIGNMENT_VERTICAL) {
int height = 0;
for (int i = 0; i < icons.length; i++) {
height += icons[i].getIconWidth();
}
height += gap * (icons.length - 1);
return height;
} else {
int height = 0;
for (int i = 0; i < icons.length; i++) {
height = Math.max(height, icons[i].getIconHeight());
}
return height;
}
}

public int getIconWidth() {
return width;
}

public int getIconHeight() {
return height;
}
}
Brian Cole
Author
Ranch Hand

Joined: Sep 20, 2005
Posts: 862
Originally posted by Deepu James:
I have Jlabel [...] Now I would like to display the icon image number over the image. Is there any option of Extending setText to diplay text over image icon in a Jlabel to display it in correct x, y coordinates?


There's no need to extend anything. Simply call

yourLabel.setVerticalTextPosition(JLabel.CENTER);
yourLabel.setHorizontalTextPosition(JLabel.CENTER);


See the 5th icon shown in figure 4-5 for an example.

[edit: I had originally included setHorizontalAlignment(JLabel.CENTER) but it's not necessary.]
[ November 13, 2008: Message edited by: Brian Cole ]

bitguru blog
Deepu James
Greenhorn

Joined: Oct 27, 2008
Posts: 6
Hello,

I have tried Sttexthorrizonatl and Vertical alighnment. But still I am not able display text over the icon and postion it correctly according to my needs. Anyway thats for your help.
Brian Cole
Author
Ranch Hand

Joined: Sep 20, 2005
Posts: 862
Originally posted by Deepu James:
I have tried Sttexthorrizonatl and Vertical alighnment. But still I am not able display text over the icon and postion it correctly according to my needs. Anyway thats for your help.


It should work. I have tried it, including again just now.

If you run the ImageLabelExample (from the link above, or here),
changing the path from "images/smile.gif" to an image file that
exists on your file system, does it work?

If so, I'd suppose I'd have to guess that your Icon implementation
is not handling its x and y coordinates correctly.
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19718
    
  20

Deepu, can you please Use Code Tags in the future? Thanks.


SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
How To Ask Questions How To Answer Questions
Derek Boring
Ranch Hand

Joined: Aug 15, 2006
Posts: 63
I did something similar once. I don't remember the exact implementaion, but I do remeber I extended the component, and then I modified the paint() method to draw the background (e.g. your icon) and then it would call super.paint() which would draw the rest of the component. I believe I had to do "super.setOpaque(false)" but I don't remember for sure.

Hope this helps!


Life is 10% what happens and 90% how you react to it.
Attitude is everything.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Display text over an icon in Jlabel