Results 1 to 5 of 5
  1. #1
    SuperMilas is offline Member
    Join Date
    May 2012
    Posts
    2
    Rep Power
    0

    Question How can I display an image in a JPanel which is attached to a JScrollPane?

    Hi i am quiet new in This Forum and i got a problem with images.
    I work with a JFrame were i got this JPanel which is attached to a JScrollPane.
    Now i want to paint a large .bmp image on it so that i can scroll through it.
    So i need a method which updates the position and displayed the right part in the JScollPane.
    something like repaint etc.

    Thanks in advance!

    Sincerely
    Milas

  2. #2
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default Re: How can I display an image in a JPanel which is attached to a JScrollPane?

    I'm not sure what you need exactly. If you place an Image into an ImageIcon, place the ImageIcon into a JLabel, and the JLabel into a JScrollPane's viewport view, you should see it and be able to scroll it.

    For better help, please show us your code attempt to solve this, any errors or exceptions you may get, or any misbehaviors.

  3. #3
    SuperMilas is offline Member
    Join Date
    May 2012
    Posts
    2
    Rep Power
    0

    Default Re: How can I display an image in a JPanel which is attached to a JScrollPane?

    First thanks for the fast respond. And that was exactly my problem now everything works fin thanks.
    I tried it with so many different methods and the solution was that easy.
    Here is what i tried(the part which is not a comment is all i needed):
    private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {
    // Graphics g= jPanel1.getGraphics();
    // jPanel1.setSize(bilder[4].getHeight(null), bilder[4].getWidth(null));
    // g.drawImage(bilder[4],WIDTH, WIDTH, rootPane);
    // g.dispose();

    // JButton x= new JButton("Hallo");
    // jPanel1.setLayout(new GridLayout(1, 1));
    // JLabel a= new JLabel();
    // Graphics g= a.getGraphics();
    // g.drawImage(bilder[0],WIDTH, WIDTH, this.getContentPane());
    // g.dispose();
    // jPanel1.add(a);

    // BufferedImage tempImage= new BufferedImage(300,300,BufferedImage.TYPE_INT_BGR);
    // Graphics g= tempImage.getGraphics();
    // g.drawImage(bilder[0], 0, 0, WIDTH, WIDTH, null);
    // g.dispose();
    // Graphics x= this.getGraphics();
    // x.drawImage(tempImage, 100, 100, null);

    // JLabel a = new JLabel(new ImageIcon(bilder[4]));
    // add(a);
    // a.setLocation(20, 20);
    // a.setVisible(true);

    // Graphics g=this.getGraphics();
    // g.drawImage(bilder[4], WIDTH, WIDTH, rootPane);
    // g.dispose

    ImageIcon test = new ImageIcon(bilder[4]);
    jLabel1.setIcon(test);

    }
    Edit:
    private Image[] bilder = {
    ImageIO.read(new File ( "Bilder/ObenLinks.bmp" )), //0
    ImageIO.read(new File ( "Bilder/ObenRechts.bmp" )), //1
    ImageIO.read(new File ( "Bilder/UntenLinks.bmp" )), //2
    ImageIO.read(new File ( "Bilder/UntenRechts.bmp" )), //3
    ImageIO.read(new File ( "Bilder/34223.jpg" ))
    };
    Thanks again

    Sincerely

    Milas

  4. #4
    DarrylBurke's Avatar
    DarrylBurke is online now Forum Police
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,422
    Rep Power
    20

    Default Re: How can I display an image in a JPanel which is attached to a JScrollPane?

    If you're forever cleaning cobwebs, it's time to get rid of the spiders.

  5. #5
    DarrylBurke's Avatar
    DarrylBurke is online now Forum Police
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,422
    Rep Power
    20

    Default Re: How can I display an image in a JPanel which is attached to a JScrollPane?

    Moved from New to Java

    db
    If you're forever cleaning cobwebs, it's time to get rid of the spiders.

Similar Threads

  1. image correctly loaded, but JPanel wont display
    By apchar in forum AWT / Swing
    Replies: 3
    Last Post: 04-13-2011, 03:15 AM
  2. JPanel and JScrollPane inheritance
    By ohiggins in forum AWT / Swing
    Replies: 1
    Last Post: 02-15-2011, 02:53 PM
  3. JPanel/JScrollPane issue
    By Moncleared in forum New To Java
    Replies: 1
    Last Post: 09-23-2010, 04:02 AM
  4. How to display image from byte array in JPANEL
    By waqasdaar in forum AWT / Swing
    Replies: 0
    Last Post: 03-22-2009, 12:11 AM
  5. Replies: 5
    Last Post: 08-02-2008, 08:05 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •