java - Trying to Overwrite/Replace JPanels -
in order display series of images, creating brand new jpanel in active listener method, , sending panel method add images. works great, except when hit "search" again , need whole new set of images. new panel, old panel exists underneath it, instead of new panel replacing old panel, stack.
i tried assigning blank panel spot @ beginning of active listener, doesn't seem work. panel.removeall() wouldn't work, i'm creating new imagespanel each time. have suggestions? thank you!
searchbutton.addactionlistener(new actionlistener() { @override public void actionperformed(actionevent e) { string searchstring = searchtext.gettext(); if (searchdroplist.getselecteditem().equals(character)) { arraylist<character> characterdetails = wikidb.searchcharacter(searchstring); if (characterdetails.size() == 0) { //do other stuff } else { editcharacterbutton.setvisible(true); (int = 0; < characterdetails.size(); i++) { characterid = characterdetails.get(i).getcharacterid(); } arraylist<string> characterimages = wikidb.searchimages(characterid); jpanel imagespanel = displayimages(characterimages, c); c.fill = gridbagconstraints.horizontal; c.gridx = 0; c.gridy = 6; rootpanel.add(imagespanel, c); rootpanel.repaint(); setcontentpane(rootpanel); pack(); setvisible(true); system.out.println("repainting."); } } } }); private jpanel displayimages(arraylist<string> characterimages, gridbagconstraints c){ bufferedimage img1 = null; jpanel imagespanel = new jpanel(); imagespanel.setlayout(new gridbaglayout()); (int x = 0; x < characterimages.size(); x++) { string imageurl = characterimages.get(x); try { url url1 = new url(imageurl); urlconnection conn1 = url1.openconnection(); conn1.setrequestproperty("user-agent", "mozilla/5.0 (windows nt 6.1; wow64) applewebkit/537.11 (khtml, gecko) chrome/23.0.1271.95 safari/537.11"); inputstream in1 = conn1.getinputstream(); img1 = imageio.read(in1); } catch (ioexception ioe) { ioe.printstacktrace(); } if (img1 != null) { //target size no bigger 100 x 100 int w; int h; if (img1.getwidth() > img1.getheight()) { //if source wider tall, make width 100, w = 100; //and scale height accordingly h = (int) ((double) img1.getheight() / img1.getwidth() * 100.0); } else { //otherwise, vice versa (and if w == h, both 100) h = 100; int myh = img1.getheight(); int myw = img1.getwidth(); w = (int) ((double) img1.getwidth() / img1.getheight() * 100.0); w= w*3; h = h*3; } bufferedimage img2 = new bufferedimage(w, h, bufferedimage.type_int_rgb); graphics2d g2 = img2.creategraphics(); g2.setrenderinghint(renderinghints.key_interpolation, renderinghints.value_interpolation_bilinear); g2.drawimage(img1, 0, 0, w, h, null); g2.dispose(); c.fill = gridbagconstraints.horizontal; c.gridx = x%4; c.gridy = x/4; border b1 = new bevelborder( bevelborder.lowered, color.light_gray, color.dark_gray); border b2 = new lineborder(color.gray, 12); border btemp = new compoundborder(b1,b2); jlabel cicon = new jlabel(new imageicon(img2)); cicon.setborder(btemp); imagespanel.add(cicon, c); } } return imagespanel; }
create 1 panel , use cardlayout cardlayout tutorial
it deck of cards. can add cards name, show last 1 , delete cards, if many images.
for further information check here: cardlayout doc
Comments
Post a Comment