Win a copy of Functional Reactive Programming this week in the Other Languages forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

JTable + TableCellRenderer

 
Amir Kobylinski
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi
I have a [JTable(dataModel)] the datas in the dataModel are vectors so i have one vector for the header of the table and the other ( dataVector) is a vector of vector
when i'm adding something ( a text) to the dataVector i convert it to JTextField but my first row on this JTable is a JButtons so to do this I used the TableCellRenderer like following

class ComponentCellRenderer implements TableCellRenderer
{

public Component getTableCellRendererComponent(JTable table, Object value,
boolean isSelected, boolean hasFocus, int row, int column) {

return (JComponent)value;
}
}
my JTable is :
class TableData extends JTable {
public TableData ( TableModel model) {
setModel(model);
setDefRendAndEdit();
}

public void setDefRendAndEdit(){
setDefaultRenderer( Object.class, new ComponentCellRenderer() );
setDefaultEditor( Object.class, new ComponentCellEditor) );

}
public TableCellRenderer getCellRenderer(int row, int column) {
TableColumn tableColumn = getColumnModel().getColumn (column);
TableCellRenderer renderer = tableColumn.getCellRenderer();
if (renderer == null) {
Class c = getColumnClass(column);
Object o = getValueAt(row,column);
if( o != null ){
c = getValueAt(row,column).getClass();
renderer=getDefaultRenderer(c);
}
}

return renderer;
}

public TableCellEditor getCellEditor(int row, int column) {
TableColumn tableColumn = getColumnModel().getColumn(column);
TableCellEditor editor = tableColumn.getCellEditor();
if (editor == null) {
Class c = getColumnClass(column);
Object o = getValueAt(row,column);
if( o != null )
c = getValueAt(row,column).getClass();
editor = getDefaultEditor(c);
}

return editor;
}

}
when im using this cod to open a small file ( 112 kb) is ok .. but when it is bigger so i got a out of memory error ... the problem i think is in dataVector using
loop {
row.addElement(new JTextField(elem));
}
vectorD.addElement(row);
is better
loop{
row.addElement(elem);
}
vectorD.addElement(row);
but elem is a String not a JComponent !! so my getTableCellRendererComponent() in ComponentCellRenderer will not like it because it return JComponent !!!
how can i fix this
help please and sorry for my english
 
VIJAY Yadlapati
Ranch Hand
Posts: 175
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
By default, TableCellrenderer is JLabel. So your getTableCellRendererComponent() has to return JLabel.
 
Amir Kobylinski
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yes but i need to have a JButton in the first row in the JTable so im sending back a JComponent ( after i recogniz. what is this )
and this work but the problem with the OutOfmemory error ...
know i can open a file ( 9,5 MB) no more ...
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic