Win a copy of Re-engineering Legacy Software this week in the Refactoring forum
or Docker in Action in the Cloud/Virtualization forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

JTable not working with tableChanged

 
Walk Rustin
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,
I have a JTable and I use table model. I also use TableCellRenderer. Now when i use tableChange() method the method getValueAt(int nRow, int nCol) in the model is called recursively forever. It starts with row zero to row 7 and then starts again. I have spent quite some time on it and have been unable to solve it. Please help me.
thanks.
....
class StockTableData extends AbstractTableModel {}
......
table.setModel(m_data);
......
for (int k = 0; k < m_data.getColumnCount(); k++ ) {
System.out.println("k is " + k);
TableCellRenderer renderer = new TextAreaCellRenderer();
TableColumn column = new TableColumn(k, StockTableData.m_columns[k].m_width, renderer, null);
table.addColumn(column);
}

...
class TextAreaCellRenderer extends JTextArea implements TableCellRenderer {
protected static Border m_noFocusBorder = new EmptyBorder(1, 1, 1, 1);
protected static Border m_focusBorder = UIManager.getBorder("Table.focusCellHighlightBorder");
public TextAreaCellRenderer() {
setEditable(false);
setLineWrap(true);
setWrapStyleWord(true);
setBorder(m_noFocusBorder);
}
public Component getTableCellRendererComponent(JTable table,
Object value, boolean isSelected, boolean hasFocus,
int nRow, int nCol)
{
if (value instanceof ColorData) {
ColorData cvalue = (ColorData)value;
setText(cvalue.m_data.toString());
setBackground(isSelected && !hasFocus ?
table.getSelectionBackground() : table.getBackground());
setForeground(isSelected && !hasFocus ?
table.getSelectionForeground() : cvalue.m_color);
setFont(table.getFont());
setBorder(hasFocus ? m_focusBorder : m_noFocusBorder);
// Adjust row's height
int width = table.getColumnModel().getColumn(nCol).getWidth();
setSize(width, 1000);
int rowHeight = getPreferredSize().height;
System.out.println("-- ");
if (table.getRowHeight(nRow) != rowHeight)
table.setRowHeight(nRow, rowHeight);
}
return this;
}
// To fix JDK bug
public String getToolTipText(MouseEvent event) {
return null;
}
}

- Walk
 
Gregg Bolinger
GenRocket Founder
Ranch Hand
Posts: 15302
6
Chrome IntelliJ IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Walk Run
Welcome to Javaranch! We don't have too many rules around here but we do have a Naming Policy. Please adjust your display name accordingly.
You can change your display name here
Thanks and hope to see you around.
 
Eddie Vanda
Ranch Hand
Posts: 281
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Walk,
class StockTableData extends AbstractTableModel {}

AbstractTableModel does not have any mechanism for storing data so you can't just extend it with an empty class. This would not compile because you need to provide the getValueAt and a couple of other methods yourself.
If you have written some code in your model StockTableData then that is most likely where the problem is.
If this does not make sense to you, then swap AbstractTableModel for DefaultTableModel and most of your problems should go away.
Ed
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic