It's not a secret anymore!*
The moose likes Swing / AWT / SWT and the fly likes Color in JTable Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Java » Swing / AWT / SWT
Bookmark "Color in JTable" Watch "Color in JTable" New topic
Author

Color in JTable

Don Kiddick
Ranch Hand

Joined: Dec 12, 2002
Posts: 580
Hope you are feeling better !
Now it is time to finish the SeverityRenderer class we started a few posts back.
After that we tell your table that you want to render Severity objects with a SeverityRenderer. (Hint - look at setDefaultRenderer to achieve this).
Then, we're done !
Two, things to do - have a go, post your code and we can discuss.
D.
Mathews P Srampikal
Ranch Hand

Joined: Nov 26, 2002
Posts: 211
Hope it is Done. Look
table.setDefaultRenderer(Severity.class, renderer );
//renderer means our SeverityRenderer.
Next what to do in the comment place.
Severity.java
import java.awt.Color;
import java.util.HashMap;
import java.util.Map;
public class Severity {

private final static Map VALUES = new HashMap();
public final static Severity MAJOR = new Severity("Major");
public final static Severity MINOR = new Severity("Minor");
public final static Severity INFORMATION = new Severity("Information");
public final static Severity CRITICAL = new Severity("Critical");
public final static Severity WARNING = new Severity("Warning");

private final String description;

private Severity(String description){
this.description = description;
VALUES.put( description, this );
}

public String getDescription() {
return this.description;
}

public String toString() {
return getDescription();
}

public boolean equals( Object obj ) {
if( ! (obj instanceof Severity ) ) {
return false;
}
Severity s = ( Severity ) obj;
return s.getDescription().equals( getDescription() );
}

public int hashCode() {
return getDescription().hashCode();
}

public static Severity getSeverity( String description ) {
return ( Severity ) VALUES.get( description );
}
}
SeverityRenderer.java
import java.awt.Component;
//import java.awt.Color;
import javax.swing.JTable;
import javax.swing.table.DefaultTableCellRenderer;
//import javax.swing.table.TableCellRenderer;
//import javax.swing.table.TableCellEditor;
public class SeverityRenderer extends DefaultTableCellRenderer {

public SeverityRenderer(){
}
public Component getTableCellRendererComponent(
JTable table,Object value,boolean isSelected,boolean hasFocus,
int row,int column){

Severity severity = ( Severity ) value;

Component cell = super.getTableCellRendererComponent(
table,severity.getDescription(),isSelected,hasFocus,
row, column);
// Decide what colour to paint the cell
// Colour the cell
return cell;
}
}


Thanks,
Mathews
Don Kiddick
Ranch Hand

Joined: Dec 12, 2002
Posts: 580
Looks good, you just need to replace these comments :
// Decide what colour to paint the cell
// Colour the cell
with code. Should be easy.
D.
Mathews P Srampikal
Ranch Hand

Joined: Nov 26, 2002
Posts: 211
But Nothing is coming to our SeverityRenderer's
public Component getTableCellRendererComponent(
JTable table,Object value,boolean isSelected,boolean hasFocus,
int row,int column) Method
Even the
System.out.println("Mathews");//is not printing
So where it is going.then!!!
Don Kiddick
Ranch Hand

Joined: Dec 12, 2002
Posts: 580
Do you have any Severity objects in your table model ? I suspect not.
In your method FaultTableModel.CreateData change the line :
data[iY][0] = Severity.getSeverity(strStatus);
Try it, this should work !
If your interested, the better solution is to not use the string representation of Severity and just use your Severity class instead. This would mean changing the type of the gstatus attribute of the FaultData class to Severity.
Then to create a instance of FaultData, instead of :
new FaultData("Information",1004,new Date(),"3S3","desc3",598);
You would do :
new FaultData(Severity.INFORMATION,1004,new Date(),"3S3","desc3",598);
make sense ?
D.
Mathews P Srampikal
Ranch Hand

Joined: Nov 26, 2002
Posts: 211
Yes Don..Done It. The changes are as you said second i was not able to follow first.cos i did not find that syntax anywhere in the program.
I made it in the following way
public Severity gstatus;
public FaultData(Severity status,int faultID,int deviceID,Date date,String source,String desc,String category) {
Then while creating and adding tom vector...
vectData.addElement(new FaultData(Severity.MAJOR,1000,1,new Date(),"S23","desc4","Cat1"));
Hoi hoi then so simple.......in Renderer Class
Severity severity = ( Severity ) value;

Component cell = super.getTableCellRendererComponent(
table,severity.getDescription(),isSelected,hasFocus,
row, column);

String str1=severity.toString();
String strMajor=Severity.MAJOR.toString();
String strMinor=Severity.MINOR.toString();
String strCritical=Severity.CRITICAL.toString();
String strWarning=Severity.WARNING.toString();
String strInfo=Severity.INFORMATION.toString();
//System.out.println(Severity.MAJOR.toString());
if(str1.equals(strMajor))
cell.setBackground(Color.YELLOW);
else if(str1.equals(strMinor))
cell.setBackground(Color.GREEN);
else if(str1.equals(strCritical))
cell.setBackground(Color.RED);
else if(str1.equals(strWarning))
cell.setBackground(Color.ORANGE);
else if(str1.equals(strInfo))
cell.setBackground(Color.CYAN);
Thanks a Lot.......I am going add another thread of Refreshing the same JTable from the database using the same data.
IT IS VERY DIFFICULT TO FIND OUT PEOPLE LIKE YOU HELPING CROSSING OCEAN AND MILES.
Don Kiddick
Ranch Hand

Joined: Dec 12, 2002
Posts: 580
Excellent ! But why do you convert to Strings to compare your Severity objects ?
instead of :
String str1=severity.toString();
String strMajor=Severity.MAJOR.toString();
if( str1.equals( strMajor ) )
cell.setBackground( Color.YELLOW );
else if( str1.equals(strMinor) )
etc..

just do :
if( severity.equals( Severity.MAJOR ) )
cell.setBackground( Color.YELLOW );
else if( severity.equals(Severity.MINOR) )
etc...
D.
Mathews P Srampikal
Ranch Hand

Joined: Nov 26, 2002
Posts: 211
Yes That's Done.Thanks Again.
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: Color in JTable
 
Similar Threads
JTable
JTable
JTable
JTable
JTable