Win a copy of Murach's Python Programming this week in the Jython/Python forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

Difference b/w CellRenderer and CellEditor  RSS feed

 
sahas ullur
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi,
i am confused between CellRenderer and CellEditor in JTable
what is the difeerence with them and where to use it ?
please help me
 
Brian Knight
Greenhorn
Posts: 16
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
A cell renderer is used to render the cell in the table, i.e. this is what actually paints what you see in the table.
The cell editor on the other hand is used to actually edit the contents of the cell. So you could write a cell editor that knows how to edit the the value in a particular cell and then a painter that nows how to paint it, maybe with an icon or something. This enables you to write editors and painters for you own custom classes so that they can be displayed in the table etc.
Hope that helps
 
Bruno Dery
Ranch Hand
Posts: 37
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Most of the CellEditors (the default ones at least) will be a widget with which you can enter text or select something, for example a JTextField, JCombo or JCheckBox.
On the other hand, CellRenderers are the ones used by swing to only display what has been entered previously, so in this case it's usually a JLabel.
Think of it as the CellEditor is usually what's used to edit a certain field, and the renderer is used to display ALL fields. The editor will only be displayed when you are in editing mode, meaning when you start typing something in a jtable's cell for example. In all other cases, the renderer is what's used for displaying.
 
Bruno Dery
Ranch Hand
Posts: 37
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
To give a short example, let's say you have a column in your jtable that is for choosing a color. Obviously you will want another way of selecting your color than having to type the text in the table cell (using the default cell editor which is a JTextField), so most likely we would have something like a color chooser for the Editor, or maybe a JCombo to at least select from a given set of colors. That's the editor.
Then, it would be nice to display these colors that were entered in all the cells for the color column, so instead of just having the name, you could have the name and a small box painted in the selected color to be more visual. For that, you would need to provide a custom CellRenderer. That's the renderer.
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!