dshrenik
asked on
Java Swing: Rendeder not being called
I have created a custom renderer class that extends DefaultTableCellRenderer.
This code works fine with the example I have attached.
But, the funny thing is that the ColorRenderer is not being called by to another table with a custom table model that I created (it extends DefaultTableModel).
I added this code to getTableCellRendererCompon ent():
System.out.println(row + " " + column);
When I run the test code to the example below, all the row and column numbers are printed. However, this does not happen for my table!
If possible, please let me know what I could be missing. Thanks!
This code works fine with the example I have attached.
But, the funny thing is that the ColorRenderer is not being called by to another table with a custom table model that I created (it extends DefaultTableModel).
I added this code to getTableCellRendererCompon
System.out.println(row + " " + column);
When I run the test code to the example below, all the row and column numbers are printed. However, this does not happen for my table!
If possible, please let me know what I could be missing. Thanks!
import javax.swing.*;
import javax.swing.table.DefaultTableCellRenderer;
import javax.swing.table.DefaultTableModel;
import java.awt.*;
public class TableRowColor2 extends JFrame
{
static JTable table;
public TableRowColor2()
{
table=new JTable(new DefaultTableModel(new Object[][]{
{"1","2","3","4"},
{"2","3","4","5"},
{"3","4","5","6"},
{"4","5","6","7"}},
new Object[]{"A","B","C","D"}));
// table.setDefaultRenderer(Object.class, new ColorRenderer(1,1));
// table.setDefaultRenderer(Object.class, new ColorRenderer(2,2));
JScrollPane scroll=new JScrollPane(table);
this.setContentPane(scroll);
this.setBounds(100,50,300,150);
}
public static void main (String arg[])
{
TableRowColor2 tes = new TableRowColor2();
tes.setVisible(true);
table.setDefaultRenderer(Object.class, new ColorRenderer(1,1));
System.out.println("here 1");
try{
Thread.currentThread().sleep(5000);
} catch(Exception ex){
ex.printStackTrace();
}
System.out.println("here 2");
table.setDefaultRenderer(Object.class, new ColorRenderer(2,2));
tes.repaint();
System.out.println("here 3");
tes.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
}
class ColorRenderer extends DefaultTableCellRenderer
{
private String columnName;
int row1;
int column1;
public ColorRenderer(int row1, int column1)
{
this.row1 = row1;
this.column1 = column1;
// this.columnName = column;
setOpaque(true);
}
public Component getTableCellRendererComponent(JTable table, Object value,boolean isSelected, boolean hasFocus, int row, int column)
{
if (row == row1 && column == column1)setBackground(Color.red);
else setBackground(Color.gray);
return this;
}
}
Do you have someyhing similar to this statemnet in your code:
table.setDefaultRenderer(O
add System.out.printn("somethi
add repaint() method to invoked on your table or even JFrame after you set cell renderer -
method getComponent... gets called when the table is repainted
method getComponent... gets called when the table is repainted
ASKER
I do have this line:
table.setDefaultRenderer(O bject.clas s, new ColorRenderer(1,1));
I am able to print text beofre and after it is called.
Adding repaint() does not seem to work.
table.setDefaultRenderer(O
I am able to print text beofre and after it is called.
Adding repaint() does not seem to work.
try to resize your window after it appears - doesn't it paint the cell after resizing ?
ASKER
No! :(
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Please post your actual code
ASKER
Sorry. It's my mistake. I have a custom renderer defined within the constructor!
:-/
:-/
? Accepting your own comment should be what you did...
Great!
ASKER
@CEHJ:
Sorry about that. Wanted to give for_yan some credit since he spent a lot time helping me fix the bug.
Sorry about that. Wanted to give for_yan some credit since he spent a lot time helping me fix the bug.
It is of little help to figure out why your different code is not working.
Are you sure you are setting renderer to your table?