views:

1562

answers:

1

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?

+1  A: 

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.

Michael Myers
jumar
Okay. And no, there isn't a custom renderer for JLabel or JButton (at least not in standard Swing; there might be third-party libraries which offer something similar).
Michael Myers
To add any JComponent to a JTable, you can extend the DefaultTableCellRenderer and over ride the getTableCellRendererComponent. Just return an instance of your BarGraph object. You have to set the cellRenderder on the JTable also, to your subclassed DefaultTableCellRenderer.
Steve K