Solved

How to changing a JTable cell color with non default renderers

Posted on 2003-11-11
6
378 Views
Last Modified: 2010-03-31
Hello,

I have searched the internet high and low, but didnt come up with a solution to
my problem yet, so I hope someone here can help me out a bit.

Ok, the situation, I have a jtable, which uses an AbstractTableModel for handling
the data to be shown in my table. In this model I have overridden the getColumnClass
method so my number cells get aligned nicely to the right.

code:
public Class getColumnClass(int col) {
  return getValueAt(0, col).getClass();
}

Somewhere in my main code I have grabbed the following code from the internet to
change the eg background color of my cells.

code:
myTable.setDefaultRenderer(Object.class, new DefaultTableCellRenderer() {
  public Component getTableCellRendererComponent(JTable table,Object value,boolean isSelected,boolean hasFocus,int row,int column) {
    Component comp = this.getTableCellRendererComponent(table,value,isSelected,hasFocus,row,column);
    comp.setBackground(Color.red);
    return comp;
  }
});

This also works perfectly. But the big problem is, this only works for the default cell
renderer and doesnt seem to work with my number cells, which apparantly use a
different cell renderer. Same problem with booleans, which are rendered with a check box.


So how can I change the color of these kind of cells?

Thanks in advance for any push in de right direction :)

Kind regards

Ido
0
Comment
Question by:ilepper
[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
  • 2
6 Comments
 
LVL 86

Expert Comment

by:CEHJ
ID: 9721164
I'm not sure if the getColumnClass stuff is what you want to be doing. I think you should be able to do it all in your other method. Just add

if (column == yourNumericColumnIndex && comp instanceof JLabel) {
       JLabel label = (JLabel)comp;
       label.setHorizontalAlignment(Component.CENTER_ALIGNMENT);
}

or something ...;-)
0
 
LVL 86

Accepted Solution

by:
CEHJ earned 125 total points
ID: 9721171
Sorry, LOL!

label.setHorizontalAlignment(Component.RIGHT_ALIGNMENT);
0
 

Author Comment

by:ilepper
ID: 9731266
thanks for the quick reply CEHJ, gonna try it and see if it satisfies my needs :)
0
 
LVL 14

Expert Comment

by:sudhakar_koundinya
ID: 10049959
No comment has been added lately, so it's time to clean up this TA.
I will leave a recommendation in the Cleanup topic area that this question is:


[points to CEHJ]


Please leave any comments here within the next seven days.
 
PLEASE DO NOT ACCEPT THIS COMMENT AS AN ANSWER!
 
sudhakar_koundinya
EE Cleanup Volunteer
---------------------
If you feel that your question was not properly addressed, or that none of the comments received were appropriate answers, please post your concern in THIS thread.
0

Featured Post

Salesforce Has Never Been Easier

Improve and reinforce salesforce training & adoption using WalkMe's digital adoption platform. Start saving on costly employee training by creating fast intuitive Walk-Thrus for Salesforce. Claim your Free Account Now

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
web application structure 18 115
Java exception bubble up 2 24
How to execute a Python program and gather return output in Java 2 49
Html Table Looping (part 2) 5 35
After being asked a question last year, I went into one of my moods where I did some research and code just for the fun and learning of it all.  Subsequently, from this journey, I put together this article on "Range Searching Using Visual Basic.NET …
Are you developing a Java application and want to create Excel Spreadsheets? You have come to the right place, this article will describe how you can create Excel Spreadsheets from a Java Application. For the purposes of this article, I will be u…
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 …
Viewers learn about the scanner class in this video and are introduced to receiving user input for their programs. Additionally, objects, conditional statements, and loops are used to help reinforce the concepts. Introduce Scanner class: Importing…

733 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