java - Image as a background in JScrollPane -


how add image background in jscrollpane? tried image doesn't display:

bufferedimage img = null;     try {         img = imageio.read(new file("c:\\users\\suraj\\documents\\netbeansprojects\\javaapplication2\\src\\javaapplication2\\images\\2.jpg"));     }      catch (ioexception e) {         e.printstacktrace();     }     image imag = img.getscaledinstance(d.width, d.height, image.scale_smooth);     imageicon imageback = new imageicon(imag);     flowlayout fl = new flowlayout();     frame.getcontentpane().setlayout(fl);     fl.addlayoutcomponent(null, new jlabel(imageback)); 

edit : add jlabels , jbuttons on jscrollpane background

if goal show image in jscrollpane without showing other components (such jtable) in jscrollpane, should:

  • make imageicon out of image via new imageicon(myimage)
  • add icon jlabel
  • place jlabel jscrollpane's viewport, can done passing jlabel jscrollpane's constructor.

and done.

if need else, describe problem in greater detail.

for example,

import java.awt.borderlayout; import java.awt.dimension; import java.awt.image.bufferedimage; import java.io.ioexception; import java.net.url; import javax.imageio.imageio; import javax.swing.*;  @suppresswarnings("serial") public class imageinscrollpane extends jpanel {    public static final string image_path = "http://image.desk7.net/"          + "space%20wallpapers/1422_1280x800.jpg";    private static final int pref_w = 500;    private static final int pref_h = 400;      public imageinscrollpane() throws ioexception {       url imageurl = new url(image_path);       bufferedimage img = imageio.read(imageurl);       icon icon = new imageicon(img);       jlabel label = new jlabel(icon);       jscrollpane scrollpane = new jscrollpane(label);        setlayout(new borderlayout());       add(scrollpane);    }     @override    public dimension getpreferredsize() {       if (ispreferredsizeset()) {          return super.getpreferredsize();       }       return new dimension(pref_w, pref_h);    }     private static void createandshowgui() {       imageinscrollpane mainpanel = null;       try {          mainpanel = new imageinscrollpane();       } catch (ioexception e) {          e.printstacktrace();          system.exit(-1);       }        jframe frame = new jframe("imageinscrollpane");       frame.setdefaultcloseoperation(jframe.dispose_on_close);       frame.getcontentpane().add(mainpanel);       frame.pack();       frame.setlocationbyplatform(true);       frame.setvisible(true);    }     public static void main(string[] args) {       swingutilities.invokelater(new runnable() {          public void run() {             createandshowgui();          }       });    } } 

you ask in comment:

what if have add other objects buttons?....how do it?

in situation, i'd create class extends jpanel , display image in jpanel drawing within paintcomponent method override (if search on this, you'll find many examples, me), add buttons/components image-drawing jpanel, , adding jpanel jscrollpane's viewport jlabel above.


Comments

Popular posts from this blog

c# - Validate object ID from GET to POST -

node.js - Custom Model Validator SailsJS -

php - Find a regex to take part of Email -