java - Change JCombobox item color when choose by keyboard or mouse -


normally when choose items in jcombobox mouse or keyborad , down keys, background of item 'blue' color.

enter image description here

i want change color blue other color follows.

enter image description here

another option use jlist#setselectionbackground(color).

screenshot

import java.awt.*; import javax.accessibility.accessiblecontext; import javax.swing.*; import javax.swing.plaf.basic.basiccombopopup;  public class combotest {   public jcomponent makeui() {     // test:     // uimanager.put("combobox.selectionbackground", color.green);     // uimanager.put("combobox.selectionforeground", color.cyan);      string[] model = {"male", "female"};      jcombobox<string> combo1 = new jcombobox<>(model);     accessiblecontext ac = combo1.getaccessiblecontext();     basiccombopopup pop = (basiccombopopup) ac.getaccessiblechild(0);     jlist list = pop.getlist();     list.setselectionforeground(color.white);     list.setselectionbackground(color.orange);      // ???: focus border of jcombobox not drawn.     jcombobox<string> combo2 = new jcombobox<>(model);     combo2.setrenderer(new defaultlistcellrenderer() {       @override public component getlistcellrenderercomponent(           jlist list, object value, int index,           boolean isselected, boolean hasfocus) {         jlabel l = (jlabel) super.getlistcellrenderercomponent(             list, value, index, isselected, hasfocus);         if (isselected) {           l.setforeground(color.white);           l.setbackground(color.orange);         } else {           l.setforeground(color.black);           l.setbackground(color.white);         }         return l;       }     });  //     // test: //     jcombobox<string> combo3 = new jcombobox<>(model); //     combo3.setrenderer(new defaultlistcellrenderer() { //       @override public component getlistcellrenderercomponent( //           jlist list, object value, int index, //           boolean isselected, boolean hasfocus) { //         list.setselectionforeground(color.white); //         list.setselectionbackground(color.orange); //         return super.getlistcellrenderercomponent( //             list, value, index, isselected, hasfocus); //       } //     });      jpanel box = new jpanel(new gridlayout(0, 1, 10, 10));     box.add(combo1);     box.add(combo2); //     box.add(combo3);      jpanel p = new jpanel(new borderlayout());     p.setborder(borderfactory.createemptyborder(20, 20, 20, 20));     p.add(box, borderlayout.north);     return p;   }   public static void main(string[] args) {     eventqueue.invokelater(() -> {       try {         uimanager.setlookandfeel(uimanager.getsystemlookandfeelclassname());         // uimanager.setlookandfeel("javax.swing.plaf.nimbus.nimbuslookandfeel");       } catch (exception e) {         e.printstacktrace();       }       jframe f = new jframe();       f.setdefaultcloseoperation(windowconstants.exit_on_close);       f.getcontentpane().add(new combotest().makeui());       f.setsize(320, 240);       f.setlocationrelativeto(null);       f.setvisible(true);     });   } } 

Comments

Popular posts from this blog

ios - MKAnnotationView layer is not of expected type: MKLayer -

ZeroMQ on Windows, with Qt Creator -

unity3d - Unity SceneManager.LoadScene quits application -