i have made jtable contains data
"marc", "caissier", "0" "julien", "caissier", "1" "jason", "danceur", "2" "marie", "danceur", "3" "john", "vendeur", "4"
in order see jobs easily, paint 1 color job. alternativly
example : row 0 : color grey (because job caissier) row 1 : color grey (because job caissier) row 2 : color pink (because job has changed danceur) row 3 : color pink (because job again danceur) row 4 : color grey (because job vendeur...)
it seems works, don't know why, when click on row, renderer called again , color change , doesn't work.
so, have decided call jtable.repaint each time user click on row. that, there bad event. row pink become grey , row grey become pink. paste snippet of code. see bug if launch jtablecolor.java , clik on id 4 example, click on id 2.
i have put breakpoint , trying resolve problem. haven't found solution.
import java.awt.borderlayout; import java.awt.color; import java.awt.component; import java.util.hashtable; import java.util.vector; import javax.swing.jframe; import javax.swing.jscrollpane; import javax.swing.jtable; import javax.swing.table.defaulttablecellrenderer; public class jtablecolor { public static void main(string args[]) { jframe frame = new jframe(); frame.setdefaultcloseoperation(jframe.exit_on_close); object rowdata[][] = { { "marc", "caissier", "0" }, { "julien", "caissier", "1" }, { "jason", "danceur", "2" }, { "marie", "danceur", "3" }, { "john", "vendeur", "4" } , }; object columnnames[] = { "prenom", "job", "id" }; jtable table = new jtable(rowdata, columnnames); jscrollpane scrollpane = new jscrollpane(table); renderercolor jtca1 = new renderercolor(); (int i=0;i<table.getcolumncount();i++){ table.getcolumnmodel().getcolumn(i).setcellrenderer(jtca1); } table.addmouselistener(new java.awt.event.mouseadapter() { public void mousereleased(java.awt.event.mouseevent evt) { jtable table = (jtable) evt.getsource(); table.repaint(); } }); frame.add(scrollpane, borderlayout.center); frame.setsize(300, 150); frame.setvisible(true); } } class renderercolor extends defaulttablecellrenderer { private string valuetostring = ""; int colascruter=-1; int couleur =0; color couleuramettre =color.green; hashtable hash= new hashtable(); public component gettablecellrenderercomponent(jtable table, object value, boolean isselected, boolean hasfocus, int row, int columnt) { component c = super.gettablecellrenderercomponent(table, value, isselected, hasfocus, row, columnt); //association d'une couleur à la table int rowcount = table.getrowcount(); string jobbefore = (string) hash.get("jobbefore"); vector listecouleur = new vector(); listecouleur.add(0,color.pink); listecouleur.add(1,color.light_gray); couleur = (integer) hash.get("couleur"); boolean change= true; string job= (string) table.getvalueat(row,1); if (jobbefore.equals("")){ setbackground((color)listecouleur.get(couleur)); } else if (job.equals(jobbefore)){ setbackground((color)listecouleur.get(couleur)); } else{ if (couleur == 0) couleur=1; else if (couleur == 1) couleur=0; setbackground((color)listecouleur.get(couleur)); } hash.put("jobbefore",job); hash.put("couleur",couleur); return c; } public renderercolor() { super(); hash.put("jobbefore", ""); hash.put("couleur",0); } }
in order see jobs easily, paint 1 color job.
example : row 0 : color grey (because job caissier) row 1 : color grey (because job caissier) row 2 : color pink (because job has changed danceur) row 3 : color pink (because job again danceur) row 4 : color grey (because job vendeur...)
consider coloring cell value (your code right seems dependent upon prior rendering eg jobbefore
). example, color rows pink have job
value "danceur":
string job = (string)table.getvalueat(row,1); if ( job.equals("danceur") ){ setbackground(color.pink); }else{ setbackground(color.light_gray); }
this not take consideration selections - if wish customize color selections, can checking isselected value. alternatively use ui default color
if ( isselected ){ setbackground(uimanager.getcolor("table.selectionbackground")); }
there should not need repaint mouselistener
unless need repaint due change in logic coloring upon mouseevent
.
Comments
Post a Comment