0

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?

jumar
  • 5,360
  • 8
  • 46
  • 42

1 Answers1

2

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
  • 188,989
  • 46
  • 291
  • 292
  • Your answer is OK... I was thinking of a custom renderer as for table cells but this may not exist for JLabel &/or JButtons. thanks, julien – jumar Oct 09 '08 at 20:07
  • 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 Oct 09 '08 at 20:11
  • 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 Oct 09 '08 at 20:26