Tiled Image Gallery in Java -


i wondering how go making image gallery one:

enter image description here

all need display images in 5x5 square above click event each image (i know how actionevents)

i tried making gridlayout constructor 5, 5, adding images panel.add(image, 0, 0); , on, no avail. here's code:

        imagelayout = new gridlayout(5, 5, 5, 5);          imagepanel = new jpanel(imagelayout);          jlabel image = new jlabel(logomanager.getinstance().getlogo("apple"));         jlabel image1 = new jlabel(logomanager.getinstance().getlogo("mcdonalds"));         jlabel image2 = new jlabel(logomanager.getinstance().getlogo("fox"));         jlabel image3 = new jlabel(logomanager.getinstance().getlogo("microsoft"));         imagepanel.add(image, 0, 0);         imagepanel.add(image1, 1, 0);         imagepanel.add(image2, 1, 1);         imagepanel.add(image3, 2, 0); 

and get:

enter image description here

thanks guys!

if going doing layouts, instead of borderlayout, gridlayout. literally sets items on screen in grid fashion. set panel's layout so:

    panel.setlayout(new gridlayout(5,5)); 

and should create output looking for. hope helps!

edit:

you can use base panel, , add jbutton instead of jlabels. , have images appear, do:

  jbutton image1 = new jbutton(new imageicon(//apple logo));   jbutton image2 = new jbutton(new imageicon(//next logo));         jbutton image3 = new jbutton(new imageicon(//next logo));        jbutton image4 = new jbutton(new imageicon(//next logo));   jbutton image5 = new jbutton(new imageicon(//next logo));    panel.setlayout(new gridlayout(5,5));   panel.add(image1);   panel.add(image2);   panel.add(image3);   panel.add(image4);   panel.add(image5); 

don't worry putting images in specific spots (unless of course have reason that), program put images in correct spot, , in order place them adding them onto panel, it's waste of time worry putting them in specific spot. hope helps you!


Comments