java - Jtable cellRenderer change background of row -


i have been tryng time , looking on internet find solution have failed. i'm trying change row background of jtable dynamically. have created arraylist keeps numbers of selected rows (adding them everytime user press alt+click on cell) , in own tablecellrenderer have added

for(integer c: leftselectedcells){ if(c.equals(row)){comp.setforeground(color.red); } else { comp.setforeground(color.black);} } 

it working, few cells, or time after selected columns oryginal color, have checked, ints still in array thats not problem, idea might cause issue?

as suggested in comments above, need provide custom renderer required columns. alternative, can override jtable.preparerenderer set background according list of affected rows. table row rendering @camickr explains approach. below example highlights rows clicked mouse + alt key. simplicity, list of highlighted rows kept client property.

import java.awt.color; import java.awt.component; import java.awt.event.mouseadapter; import java.awt.event.mouseevent; import java.util.arraylist; import java.util.list;  import javax.swing.*; import javax.swing.table.defaulttablemodel; import javax.swing.table.tablecellrenderer;  public class tablehighlight {     tablehighlight() {         jframe frame = new jframe("tablehighlight");         frame.setdefaultcloseoperation(jframe.exit_on_close);          object[][] data = { { "column 1", "column 2" },                 { "column 1", "column 2" }, { "column 1", "column 2" },                 { "column 1", "column 2" } };          object[] columnnames = { "column 1", "column 2" };          defaulttablemodel model = new defaulttablemodel(data, columnnames);          final jtable table = new jtable(model) {             @override             public component preparerenderer(tablecellrenderer renderer,                     int row, int column) {                 component c = super.preparerenderer(renderer, row, column);                 list<integer> selectedrows = (list<integer>) getclientproperty("highlightrows");                 c.setbackground(selectedrows.contains(row) ? color.cyan : getbackground());                 return c;             }         };          table.putclientproperty("highlightrows", new arraylist<integer>());          table.addmouselistener(new mouseadapter() {             @override             public void mouseclicked(mouseevent evt) {                 if (!evt.isaltdown())                     return;                 int row = table.rowatpoint(evt.getpoint());                 if (row == -1)                     return;                 list<integer> selectedrows = (list<integer>) table                         .getclientproperty("highlightrows");                 int index = selectedrows.indexof(row);                 if (index != -1)                     selectedrows.remove(index);                 else                     selectedrows.add(row);                 table.repaint();             }         });          frame.add(new jscrollpane(table));         frame.setlocationbyplatform(true);         frame.pack();         frame.setvisible(true);     }      public static void main(string args[]) {         swingutilities.invokelater(new runnable() {             public void run() {                 new tablehighlight();             }         });     } } 

Comments

Popular posts from this blog

android - getbluetoothservice() called with no bluetoothmanagercallback -

sql - ASP.NET SqlDataSource, like on SelectCommand -

ios - Undefined symbols for architecture armv7: "_OBJC_CLASS_$_SSZipArchive" -