Solved

Java Swing: Rendeder not being called

Posted on 2011-09-11
12
337 Views
Last Modified: 2012-05-12
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 getTableCellRendererComponent():
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;
     }
}

Open in new window

0
Comment
Question by:dshrenik
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 6
  • 4
  • 2
12 Comments
 
LVL 47

Expert Comment

by:for_yan
ID: 36519968
But you posted the code which is working?
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?
0
 
LVL 47

Expert Comment

by:for_yan
ID: 36519975


Do you have someyhing similar to this statemnet in your code:
            table.setDefaultRenderer(Object.class, new ColorRenderer(1,1));
add System.out.printn("something") - just before and just after this line to make sure this line gert executed
0
 
LVL 47

Expert Comment

by:for_yan
ID: 36519984
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
0
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

Author Comment

by:dshrenik
ID: 36519999
I do have this line:
 table.setDefaultRenderer(Object.class, new ColorRenderer(1,1));

I am able to print text beofre and after it is called.

Adding repaint() does not seem to work.
0
 
LVL 47

Expert Comment

by:for_yan
ID: 36520027

try to resize your window after it appears - doesn't it paint the cell after resizing ?
0
 

Author Comment

by:dshrenik
ID: 36520039
No! :(
0
 
LVL 47

Accepted Solution

by:
for_yan earned 500 total points
ID: 36520044
Well, it is difficult to gess this way - I think the way to go - if it is complex program - try to simplify it gradually (say simplify contents of your sells) up to the point when it  either
starts working correctly or will be suitable to post it here. If it starts working then you'll have a clue
what was obstructiong, otherwise I  would be able to try the simplified version in my IDE and
help to trouble shoot it for you. Well, there is only one way of dioing things right; one can screw up things in zillion ways -
it is very difficult to guess what you may be doing wrong
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 36520046
Please post your actual code
0
 

Author Comment

by:dshrenik
ID: 36520049
Sorry. It's my mistake. I have a custom renderer defined within the constructor!
:-/
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 36520054
? Accepting your own comment should be what you did...
0
 
LVL 47

Expert Comment

by:for_yan
ID: 36520055
Great!
0
 

Author Comment

by:dshrenik
ID: 36520057
@CEHJ:
Sorry about that. Wanted to give for_yan some credit since he spent a lot time helping me fix the bug.
0

Featured Post

MS Dynamics Made Instantly Simpler

Make Your Microsoft Dynamics Investment Count  & Drastically Decrease Training Time by Providing Intuitive Step-By-Step WalkThru Tutorials.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This was posted to the Netbeans forum a Feb, 2010 and I also sent it to Verisign. Who didn't help much in my struggles to get my application signed. ------------------------- Start The idea here is to target your cell phones with the correct…
Basic understanding on "OO- Object Orientation" is needed for designing a logical solution to solve a problem. Basic OOAD is a prerequisite for a coder to ensure that they follow the basic design of OO. This would help developers to understand the b…
Viewers learn about the “for” loop and how it works in Java. By comparing it to the while loop learned before, viewers can make the transition easily. You will learn about the formatting of the for loop as we write a program that prints even numbers…
Video by: Michael
Viewers learn about how to reduce the potential repetitiveness of coding in main by developing methods to perform specific tasks for their program. Additionally, objects are introduced for the purpose of learning how to call methods in Java. Define …

729 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question