Win a copy of Beginning Java 17 Fundamentals: Object-Oriented Programming in Java 17 this week in the Java in General forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Tim Cooke
  • Campbell Ritchie
  • Ron McLeod
  • Liutauras Vilda
  • Jeanne Boyarsky
Sheriffs:
  • Junilu Lacar
  • Rob Spoor
  • Paul Clapham
Saloon Keepers:
  • Tim Holloway
  • Tim Moores
  • Jesse Silverman
  • Stephan van Hulst
  • Carey Brown
Bartenders:
  • Al Hobbs
  • Piet Souris
  • Frits Walraven

JTable + TableCellRenderer

 
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Ranch Hand
Posts: 175
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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 ...
 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic