java - adding background image to JComboBox -


i have program , want add background image combobox. have try many methods , cant make it, may help?

class myclass {      public static void main(string args[])     {     jframe myframe = new jframe();     myframe.setbounds(500,500,500,500);     myframe.setlayout(null);     myframe.setvisible(true);      jcombobox mycombobox = new jcombobox();     mycombobox.setbounds(100,100,100,20);     mycombobox.add("item1");     mycombobox.add("item2");     mycombobox.setvisible(true);      image comboboximage = new imageicon(         myclass.class.getresources("/image.png")).getimage();     }  } 

how set comboboximage background of mycombobox combobox?

you can set custom renderer combo box using:

mycombobox.setrenderer(...);

a possible implementation of renderer be:

class backgroundrenderer extends jlabel implements listcellrenderer<string> {     private final image image;      public backgroundrenderer(image image) {         this.image = image;     }      @override     protected void paintcomponent(graphics g) {         g.drawimage(image, 0, 0, this);          super.paintcomponent(g);     }      @override     public component getlistcellrenderercomponent(jlist<? extends string> list, string value, int index,             boolean isselected, boolean cellhasfocus) {         settext(value);          return this;     } } 

Comments

Popular posts from this blog

ubuntu - PHP script to find files of certain extensions in a directory, returns populated array when run in browser, but empty array when run from terminal -

php - How can i create a user dashboard -

javascript - How to detect toggling of the fullscreen-toolbar in jQuery Mobile? -