Win a copy of Murach's Python Programming this week in the Jython/Python forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

slow in TableCellRenderer  RSS feed

 
eric sato
Ranch Hand
Posts: 37
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi all,
i've the JTable that using model AbstractTableModel (load data from DB). Initially my application runs without any renderer, it runs fine in term of speed. But after i added the renderer from TableCellRenderer, it speed was greately reduced.
My JTable displayed around 4 to 5 hundred rows with 3 columns. Whenever user double-click, a dialog box pop up (it will display detail information from the user selected). Then the JTable will rendered the selected row with background color light-blue.
Any better idea without using TableCellRenderer?
 
Don Kiddick
Ranch Hand
Posts: 580
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Can you post your TableCellRenderer code please.
 
eric sato
Ranch Hand
Posts: 37
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
/*
* InboxCellRender.java
*
* Created on 06 February 2004, 23:34
*/
package companyName.gui.util;
/**
*
* @author Administrator
*/
import java.awt.*;
import javax.swing.*;
import javax.swing.table.*;
public class InboxCellRender extends JLabel implementsTableCellRenderer {
private booleanisSelected;
private booleanhasFocus;
private int row = 0 , column = 0;
private boolean mbBG = false;
/** Creates a new instance of InboxCellRender */
public InboxCellRender() {
}

public Component getTableCellRendererComponent( JTable table, Object value,
boolean isSelected, boolean hasFocus, int row, int column ) {
StringsText = (String)value;
this.isSelected = isSelected;
this.hasFocus = hasFocus;
this.row = row;
this.column = column;
setFont(new Font("Table", Font.PLAIN,12));
mbBG = false;
ColorbColor = Color.white;
TableModel loTableModel = table.getModel();
String lsRead = (String)loTableModel.getValueAt(row, 4);
if(lsRead != null && lsRead.trim().equals("1")) {
mbBG = true;
}

if(mbBG)bColor = new Color(223,234,255);
if( isSelected || hasFocus) bColor = new Color(180,180,255);
setBackground(bColor);
setValue(value);
return this;
}

protected void setValue(Object value) {
setText((value == null) ? "" : value.toString());
}


}
 
Don Kiddick
Ranch Hand
Posts: 580
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I can see two potential problems. Firstly you create a Color object everytime you render a cell which is not white. Fixed by creating some Color constants. Secondly you extend JLabel but do not override validate, revalidate, repaint, and firePropertyChange methods to be no-ops. Check out the DefaultTableCellRenderer documentation for details on why you should do this. To fix this you can either implement these methods to be no-ops or as I have done, made your class inherit from DefaultTableCellRenderer :

does that help ?
D.
 
eric sato
Ranch Hand
Posts: 37
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi Don,
Yup, with your help to improve my code, there is some improvement performance in my application. Thank you!
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!