dcsimg
Login | Register   
LinkedIn
Google+
Twitter
RSS Feed
Download our iPhone app
TODAY'S HEADLINES  |   ARTICLE ARCHIVE  |   FORUMS  |   TIP BANK
Browse DevX
Sign up for e-mail newsletters from DevX

By submitting your information, you agree that devx.com may send you DevX offers via email, phone and text message, as well as email offers about other products and services that DevX believes may be of interest to you. DevX will process your information in accordance with the Quinstreet Privacy Policy.


Tip of the Day
Language: Java
Expertise: Advanced
May 29, 2003

WEBINAR:

On-Demand

Building the Right Environment to Support AI, Machine Learning and Deep Learning


Extend Swing's JTable to Support Rendering of Different Cell Types in a Column

The basic JTable rendering mechanism supports a single renderer for all cells in a column. The Column Renderer may be set in the TableColumn class. If the TableColumn Renderer is null, the JTable looks up the Renderer based on the Column Class. The Column Class is then determined by the TableModel getColumnClass() method.

Both these approaches assume all values in a column are of the same type, prohibiting the rendering of cell values of different types in a column

To extend the JTable to support the display of cells of different types in one column, sublass JTable. Then, override the getCellRenderer() method to look up the appropriate renderer at run-time, based on the type of the value in the cell.


public class JMyTable  extends JTable{

 public TableCellRenderer getCellRenderer(int row, int column) {
  Object value = getValueAt(row,column);
  if (value !=null) {
    return getDefaultRenderer(value.getClass());
  }
  return super.getCellRenderer(row,column);
 }
};
Lara D'Abreo
 
Comment and Contribute

 

 

 

 

 


(Maximum characters: 1200). You have 1200 characters left.

 

 

Sitemap
Thanks for your registration, follow us on our social networks to keep up-to-date