By default the "string painted" appears at the center of JProgressBar but I want that the "string painted" should appear at the point just after the value of JProgressbar, for example if value of JprogressBar is 0 "string painted" should appears at the extreme left of the JProgressBar, please tell how can I do that???
Asked
Active
Viewed 502 times
1 Answers
1
you need to subclass JProgressBar...
private class CustomProgressBar extends JProgressBar{
private static final long serialVersionUID = 1L;
private boolean isStringToBePainted = false;
@Override
protected void paintComponent(Graphics g) {
super.paintComponent(g);
if(isStringToBePainted ){
Dimension size = CustomProgressBar.this.getSize();
int x = (int)( size.width * CustomProgressBar.this.getPercentComplete() );
int height = g.getFontMetrics().getHeight();
int d = g.getFontMetrics().getDescent();
int y = (size.height + height)/2-d;
String text = getString();
g.setColor(Color.BLACK );
g.drawString(text, x, 12);
}
}
@Override
public void setStringPainted(boolean b) {
// don't do super.setStringPainted(b);
//super.setStringPainted(b);
isStringToBePainted=b;
}
}
maybe you play a bit with the font color, i haven't found out where to get the right one...

Martin Frank
- 3,445
- 1
- 27
- 47
-
1i don't write code for you ... i gave a nice hint on how to solve your problem... have a try on your own and if you fail show me what you've achieved and whats is your problem... – Martin Frank Aug 21 '14 at 05:57
-
seeing your question on http://stackoverflow.com/questions/25416336/jprogressbar-setstringpaintedtrue-is-painting-two-strings the code abovew might work as well with a vertical progressbar... - but you must switch vertical and horizontal coordinates... – Martin Frank Aug 21 '14 at 09:48