• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Color in JTable

 
Ranch Hand
Posts: 580
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Ranch Hand
Posts: 211
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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;
}
}
 
Don Kiddick
Ranch Hand
Posts: 580
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 211
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 580
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 211
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 580
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 211
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Yes That's Done.Thanks Again.
 
reply
    Bookmark Topic Watch Topic
  • New Topic