GeeCON Prague 2014*
The moose likes Swing / AWT / SWT and the fly likes JTable + TableCellRenderer Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


JavaRanch » Java Forums » Java » Swing / AWT / SWT
Bookmark "JTable + TableCellRenderer" Watch "JTable + TableCellRenderer" New topic
Author

JTable + TableCellRenderer

Amir Kobylinski
Greenhorn

Joined: Aug 20, 2003
Posts: 2
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

Joined: Aug 04, 2003
Posts: 175
By default, TableCellrenderer is JLabel. So your getTableCellRendererComponent() has to return JLabel.
Amir Kobylinski
Greenhorn

Joined: Aug 20, 2003
Posts: 2
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 ...
 
GeeCON Prague 2014
 
subject: JTable + TableCellRenderer