I have renderer a JPanel at the fourth column of my JTable. I need to draw an image Dynamically. I have used this code to get the component...... at certain row and column.....

JPanel cPanel = (JPanel) jTable.getCellRenderer(0, 4).getTableCellRendererComponent(jTable, jTable.getValueAt(0, 4), false, false, 0, 4);
Graphics graphics = cPanel.getGraphics();

When i get the graphics It shows NULL.... Is there is any solution????

5 Years
Discussion Span
Last Post by sankubha
  • Graphics graphics = cPanel.getGraphics();

    1. is proper methods for saving this snapshot to File, BufferedImage, paper Printer

    2. this snapshot is very volatile, can expired on 1st mouse, key or from methods implemented in API

    3. has nothing to do with ---> I need to draw an image Dynamically, painting in Swing is done in paintComponent

    4. you can to override JComponent/JLabel (default returns from XxxTableCellRenderer) and painting there anything as is possible for plain JPanel/JComponent, issue is with refresh rate (crazy !!!) for some of animaged Gifs, is required to resync before in external SW

    5. again XxxTableCellRenderer is invoked, refreshed from ever mouse, key or from methods implemented in API inside visible JTables Rectangle == JViewport

  • JTable knows Icon/ImageIcon, put this object to XxxTableModel directly, override getColumnClass to Icon/ImageIcon

  • When i get the graphics It shows NULL.... Is there is any solution???? please why shouting

  • please clarify whats I need to draw an image Dynamically.

Edited by mKorbel


thank u .I Used getcolumnclass() function....but only a part of the image is showing....how to resize the image icon...... to fit for width=100 and height=100......

  • TableCellRenderer by default returns JComponent/JLabel

  • Icon/ImageIcon placed in JLabel its schrinked to JLabels bounds

  • there is only one issue with Icon/ImageIcon placed in JLabel, where pixels ratio is lower than JLabels ratio, then Icon/ImageIcon (default setting in API) is centered and no longer resized with JLabel

  • then

    1. override setPreferredSize inside XxxCellRenderer,

    2. override JTable.doLayout() with getPreferredSize

    3. see my comment to @JamesCherrill's post here (esp. 2nd. point) and to override my a.m. points 1st or 2nd.

@JamesCherrill (note, not intented as attack)

Use Image's getScaledInstance method to resize an Image


  • OP is able to put asynchronous ScaledInstance inside TableCellRenderer, not good idea, can caused unwanted side effect from repaints events inside JTable


  • agree in the case that this ScaledImage is prepared as local variable on applications startup

Edited by mKorbel

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.