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
Post a Comment