dcsimg
About JTable's HeaderRenderer Class
1 posts in topic
Flat View  Flat View
TOPIC ACTIONS:
 

Posted By:   Tonmoy_Paul
Posted On:   Wednesday, April 2, 2003 10:22 PM

DefaultTableModel dm = new DefaultTableModel(); dm.setDataVector(new Object[][]{ {"119","foo","bar","ja","ko","zh"}, {"911","bar","foo","en","fr","pt"}}, new Object[]{"SNo.","1","2","Native","2","3"}); JTable table = new JTable( dm ); TableColumn tc = table.getColumn(dm.getColumnName(0)); JLabel renderer = (JLabel)tc.getCellRenderer(); Q. why (JLabel)tc.getCellRenderer(); return null to renderer? what is the possible solution to get the renderer JLabel    More>>

DefaultTableModel dm = new DefaultTableModel();
dm.setDataVector(new Object[][]{
{"119","foo","bar","ja","ko","zh"},
{"911","bar","foo","en","fr","pt"}},
new Object[]{"SNo.","1","2","Native","2","3"});

JTable table = new JTable( dm );


TableColumn tc = table.getColumn(dm.getColumnName(0));
JLabel renderer = (JLabel)tc.getCellRenderer();

Q. why (JLabel)tc.getCellRenderer(); return
null to renderer? what is the possible solution to get
the renderer JLabel

   <<Less

Re: About JTable's HeaderRenderer Class

Posted By:   Bahman_Barzideh  
Posted On:   Thursday, April 3, 2003 06:29 AM

I think the current (Java 1.4.1) cell renderer scheme works as
follows:

  • Each TableColumn can have its own TableCellRenderer for painting its values. You can set this
    value by calling the setCellRenderer () method of the
    table column or by specifying it when you create the TableColumn. By default this value is null.
  • A JTable maintains a default cell renderer for each object type known to it (it gets the object type from its getColumnClass () method). JTable has a default renderer for String, numbers (Integer, Float, etc), Date, Boolean, Icon and finally Object (every thing else). You can also specify a default renderer for a given class by calling JTable.setDefaultRenderer ().
  • When it needs to paint a given cell, JTable checks to
    see if the associated TableColumn defines a cell renderer (
    its getCellRenderer () returns a non-null value). If it does
    not, JTable will look at the value of the cell and based on
    its type picks one of the default renderers to paint the cell.



    The short answer is tc.getCellRenderer () returns null
    because you have not set the renderer for the column prior to this
    call.




    The best way to get the renderer for a given JTable cell is by
    calling JTable.getCellRenderer (row, column).
  • About | Sitemap | Contact