java - How to change the foreground color of specific items in a List? -


when press button, want change foreground color of selected item in list.

so far, tried this:

list.setforeground(display.getsystemcolor(swt.color_red)); 

but changes foreground color of items, not selected one.

any ideas how solve this?

doing list require custom drawing. better off using table instead (or tableviewer depending on requirements). here example of table want:

public static void main(string[] args) {     final display display = new display();     shell shell = new shell(display);     shell.setlayout(new gridlayout(1, false));     shell.settext("stackoverflow");      final table table = new table(shell, swt.border | swt.multi);     table.setlayoutdata(new griddata(swt.fill, swt.fill, true, true));      (int = 0; < 10; i++)     {         tableitem item = new tableitem(table, swt.none);         item.settext("item " + i);     }      button button = new button(shell, swt.push);     button.settext("color selected");      button.addlistener(swt.selection, new listener()     {         @override         public void handleevent(event arg0)         {             list<tableitem> allitems = new arraylist<>(arrays.aslist(table.getitems()));             tableitem[] selitems = table.getselection();              (tableitem item : selitems)             {                 item.setforeground(display.getsystemcolor(swt.color_red));                 allitems.remove(item);             }              (tableitem item : allitems)             {                 item.setforeground(display.getsystemcolor(swt.color_list_foreground));             }         }     });      shell.pack();     shell.open();     while (!shell.isdisposed())     {         if (!display.readanddispatch())             display.sleep();     }     display.dispose(); } 

before button press:

enter image description here

after button press:

enter image description here


just note: not efficient way it, should give basic idea.


Popular posts from this blog

How to calculate SNR of signals in MATLAB? -

c# - Attempting to upload to FTP: System.Net.WebException: System error -

ios - UISlider customization: how to properly add shadow to custom knob image -