java - fixing JScrollPane with JLabel -


in order add background image , work on jlabels instead of jpanel .

    jframe frame=new jframe();     frame.setdefaultcloseoperation(jframe.exit_on_close);     jlabel label=new jlabel(new imageicon("bg.png"));     label.setlayout(new boxlayout(label, boxlayout.y_axis));     for(int i=0;i<20;i++)          label.add(new jlabel(i.tostring()));     frame.add(label); 

the problem here components fit background image shown (in example 10 first jlabels)

i tried using jscrollpane

    jscrollpane scroll=new jscrollpane(l);     for(int i=0;i<20;i++)          label.add(new jlabel(i.tostring()));     frame.add(scroll); 

but didn't change , tried using jpanel

    jpanel panel=new jpanel();     panel.setlayout(new boxlayout(panel, boxlayout.y_axis));     jscrollpane scroll=new jscrollpane(panel);     for(int i=0;i<20;i++)          panel.add(new jlabel(i.tostring()));     label.add(scroll);     frame.add(label); 

so got jscrollpane working jpanel has covered background image

what should ?

here's result adding jlabels label:

enter image description here

the result after adding them scroll:

enter image description here

and after adding setopaque(false) jpanel:

enter image description here

you can add image part of jpanel's paintcomponent() method, add labels above it, put pane inside jscrollpane , add jframe, example:

import java.awt.dimension; import java.awt.graphics; import java.awt.image; import java.io.fileinputstream; import java.io.filenotfoundexception; import java.io.ioexception;  import javax.imageio.imageio; import javax.swing.boxlayout; import javax.swing.jframe; import javax.swing.jlabel; import javax.swing.jpanel; import javax.swing.jscrollbar; import javax.swing.jscrollpane; import javax.swing.swingutilities;  public class panelwithbackgroundimage {      private jframe frame;     private jpanel pane;     private image img;     private jscrollpane scroll;      public static void main(string[] args) {         swingutilities.invokelater(new runnable() {              @override             public void run() {                 new panelwithbackgroundimage().createandshowgui();             }         });     }      public void createandshowgui() {         frame = new jframe("jframe bg example");         try {             img = imageio.read(new fileinputstream("/home/jesus/pictures/tokyo.jpg"));         } catch (filenotfoundexception e) {             // todo auto-generated catch block             e.printstacktrace();         } catch (ioexception e) {             // todo auto-generated catch block             e.printstacktrace();         }         pane = new jpanel() {             @override             protected void paintcomponent(graphics g) {                 super.paintcomponent(g);                 g.drawimage(img, 0, 0, getwidth(), getheight(), this);             }         };          pane.setlayout(new boxlayout(pane, boxlayout.page_axis));          (int = 0; < 20; i++) {             pane.add(new jlabel("label " + i));         }          scroll = new jscrollpane(pane);          frame.add(scroll);         frame.setsize(frame.getpreferredsize());         frame.setvisible(true);         frame.setdefaultcloseoperation(jframe.exit_on_close);     } } 

which gives following output:

enter image description hereenter image description here

if want jlabels centered, can change for loop with:

for (int = 0; < 20; i++) {     jlabel label = new jlabel("label " + i);     label.setalignmentx(component.center_alignment);     pane.add(label); } 

which should similar this:

enter image description here


note:

you can copy-paste code above, should work, change image path, , code, called minimal, complete , verifiable example (mcve), next time, please post 1 demonstrates have tried, not parts of it, way you'll more, better , faster answers. you! :)


Comments

Popular posts from this blog

php - isset function not working properly -

javascript - Thinglink image not visible until browser resize -

firebird - Error "invalid transaction handle (expecting explicit transaction start)" executing script from Delphi -