Java/Swing: How to draw a simple bar graph in a custom renderer for a JLabel

I would like to dynamically create a minimal transparent bar graph to display over a canvas.

I was thinking of using a custom renderer for a JButton or a JLabel; but how do I draw my bar graph in this renderer?


Asked by: Kelvin392 | Posted: 23-01-2022






Answer 1

The standard way would be to create a subclass (possibly an anonymous one, if you prefer) of JLabel or JPanel and overload the paintComponent(Graphics g) method. You can then use the passed Graphics object to draw whatever rectangles (and so forth) that you need. For more information on that part of it, refer to the Java 2D Graphics Trail.

EDIT: Does that answer the question? I just re-read it and now I'm not sure.

Answered by: John112 | Posted: 24-02-2022



Similar questions





Still can't find your answer? Check out these amazing Java communities for help...



Java Reddit Community | Java Help Reddit Community | Dev.to Java Community | Java Discord | Java Programmers (Facebook) | Java developers (Facebook)



top