[swing] JscrollPane to display a changing image

Discussion in 'Java' started by oliviergir@gmail.com, Mar 10, 2007.

  1. Guest

    Hi,
    I am displaying a big image within a smaller JcrollPane component like
    this :

    panel = new MyJPanel(this,image);
    jScrollPane=new JScrollPane(panel);
    getContentPane().add(BorderLayout.CENTER, jScrollPane);

    And within the constructor of MyJPanel (class that extends JPanel) :
    this.image=image;
    imageIcon=new ImageIcon(captureImage);
    jLabel=new JLabel(imageIcon);
    add(jLabel);

    The image appears with the scrollbars and that's fine.

    Now there is button (in the north) and when it is clicked I want to
    update the jscrollPane with a new image but that is not working.
    I tried changing the image and repainting but it does not work..
    I tried removing the jscrollPane component, creating a new one and
    adding it to the contentPane and repainting but the display does not
    refresh..

    Any idea ??
    , Mar 10, 2007
    #1
    1. Advertising

  2. wrote:
    > Hi,
    > I am displaying a big image within a smaller JcrollPane component like
    > this :
    >
    > panel = new MyJPanel(this,image);
    > jScrollPane=new JScrollPane(panel);
    > getContentPane().add(BorderLayout.CENTER, jScrollPane);
    >
    > And within the constructor of MyJPanel (class that extends JPanel) :
    > this.image=image;
    > imageIcon=new ImageIcon(captureImage);
    > jLabel=new JLabel(imageIcon);
    > add(jLabel);
    >
    > The image appears with the scrollbars and that's fine.
    >
    > Now there is button (in the north) and when it is clicked I want to
    > update the jscrollPane with a new image but that is not working.
    > I tried changing the image and repainting but it does not work..
    > I tried removing the jscrollPane component, creating a new one and
    > adding it to the contentPane and repainting but the display does not
    > refresh..
    >
    > Any idea ??
    >


    I was going to make a snarky comment about the problem being in your
    code, which we can't see. But I got to thinking about what you could
    have done to cause it not to work and remembered just how complicated
    updating Swing components can be. So I think your problem is that you
    need to call revalidate() on your JLabel after you change the image in
    your ImageIcon. But I'm just guessing without being able to see your
    code :).

    import java.awt.*;
    import java.awt.event.*;
    import javax.swing.*;
    import javax.swing.event.*;

    public class test extends JFrame {
    public test() {
    setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

    final Image newImage = getToolkit().createImage("kittens.jpg");
    final ImageIcon i = new ImageIcon("saturn.jpg");
    final JLabel l = new JLabel(i);
    JScrollPane sp = new JScrollPane(l);
    add(sp,BorderLayout.CENTER);

    JButton b = new JButton("Change");
    b.addActionListener(new ActionListener() {
    public void actionPerformed(ActionEvent ae) {
    i.setImage(newImage);
    l.revalidate();
    }
    });
    add(b,BorderLayout.NORTH);

    setSize(400,300);
    setVisible(true);
    }

    public static void main (String[] args) {
    Runnable r = new Runnable() {
    public void run() {
    new test();
    }
    };
    EventQueue.invokeLater(r);
    }
    }

    --

    Knute Johnson
    email s/nospam/knute/
    Knute Johnson, Mar 10, 2007
    #2
    1. Advertising

  3. Guest

    Re: JscrollPane to display a changing image

    On 10 mar, 17:08, Knute Johnson <>
    wrote:
    > wrote:
    > > Hi,
    > > I am displaying a bigimagewithin a smaller JcrollPane component like
    > > this :

    >
    > > panel = new MyJPanel(this,image);
    > > jScrollPane=newJScrollPane(panel);
    > > getContentPane().add(BorderLayout.CENTER,jScrollPane);

    >
    > > And within the constructor of MyJPanel (class that extends JPanel) :
    > > this.image=image;
    > > imageIcon=new ImageIcon(captureImage);
    > > jLabel=new JLabel(imageIcon);
    > > add(jLabel);

    >
    > > Theimageappears with the scrollbars and that's fine.

    >
    > > Now there is button (in the north) and when it is clicked I want to
    > > update thejscrollPanewith a newimagebut that is not working.
    > > I triedchangingtheimageand repainting but it does not work..
    > > I tried removing thejscrollPanecomponent, creating a new one and
    > > adding it to the contentPane and repainting but thedisplaydoes not
    > > refresh..

    >
    > > Any idea ??

    >
    > I was going to make a snarky comment about the problem being in your
    > code, which we can't see. But I got to thinking about what you could
    > have done to cause it not to work and remembered just how complicated
    > updatingSwingcomponents can be. So I think your problem is that you
    > need to call revalidate() on your JLabel after you change theimagein
    > your ImageIcon. But I'm just guessing without being able to see your
    > code :).
    >
    > import java.awt.*;
    > import java.awt.event.*;
    > import javax.swing.*;
    > import javax.swing.event.*;
    >
    > public class test extends JFrame {
    > public test() {
    > setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    >
    > finalImagenewImage = getToolkit().createImage("kittens.jpg");
    > final ImageIcon i = new ImageIcon("saturn.jpg");
    > final JLabel l = new JLabel(i);
    > JScrollPanesp = newJScrollPane(l);
    > add(sp,BorderLayout.CENTER);
    >
    > JButton b = new JButton("Change");
    > b.addActionListener(new ActionListener() {
    > public void actionPerformed(ActionEvent ae) {
    > i.setImage(newImage);
    > l.revalidate();
    > }
    > });
    > add(b,BorderLayout.NORTH);
    >
    > setSize(400,300);
    > setVisible(true);
    > }
    >
    > public static void main (String[] args) {
    > Runnable r = new Runnable() {
    > public void run() {
    > new test();
    > }
    > };
    > EventQueue.invokeLater(r);
    > }
    >
    > }
    >
    > --
    >
    > Knute Johnson
    > email s/nospam/knute/- Masquer le texte des messages précédents -
    >
    > - Afficher le texte des messages précédents -


    thanks a lot, Knute this works !
    now I have another subtil problem :
    I want to capture mouse coordinates when it moves over the picture and
    I need both coordinates :
    -the ones that relates to the picture (the ones I get if I only add a
    mousemotionlistener to the jLabel)
    - the ones that relates to the jScrollPane (the ones I get if I only
    add a mousemotionlistener to the jScrollPane)
    The problem is that each listener works alone but when I add them both
    at the same time, the first one hides the second one (the event does
    not reach the second listener)
    , Mar 11, 2007
    #3
  4. Lew Guest

    Re: JscrollPane to display a changing image

    wrote:
    >> - Masquer le texte des messages précédents -
    >>
    >> - Afficher le texte des messages précédents -


    What is the news client that inserts these comments? (This is the first time
    I've seen it in French, though.)

    It inserts them into the quoted block in a person's answer, and it never seems
    to make sense.

    Is it the same one that causes people's posts to appear twice, the second as a
    reply to the first?

    I want to know so as to be certain never to use it.

    -- Lew
    Lew, Mar 11, 2007
    #4
  5. Lars Enderin Guest

    Re: JscrollPane to display a changing image

    Lew skrev:
    > wrote:
    >>> - Masquer le texte des messages précédents -
    >>>
    >>> - Afficher le texte des messages précédents -

    >
    > What is the news client that inserts these comments? (This is the first
    > time I've seen it in French, though.)
    >
    > It inserts them into the quoted block in a person's answer, and it never
    > seems to make sense.
    >
    > Is it the same one that causes people's posts to appear twice, the
    > second as a reply to the first?
    >
    > I want to know so as to be certain never to use it.
    >


    It's Google Groups.
    Lars Enderin, Mar 11, 2007
    #5
  6. Re: JscrollPane to display a changing image

    "Lars Enderin" <> wrote in message
    news:...
    > Lew skrev:
    >> wrote:
    >>>> - Masquer le texte des messages précédents -
    >>>>
    >>>> - Afficher le texte des messages précédents -

    >>
    >> What is the news client that inserts these comments? (This is the first
    >> time I've seen it in French, though.)
    >>
    >> It inserts them into the quoted block in a person's answer, and it never
    >> seems to make sense.
    >>
    >> Is it the same one that causes people's posts to appear twice, the second
    >> as a reply to the first?
    >>
    >> I want to know so as to be certain never to use it.
    >>

    >
    > It's Google Groups.


    Oh great. Google hopes to revamp usenet to make it work best with their
    service. That is the kind of BS I was expecting from Microsoft.

    --
    LTP

    :)
    Luc The Perverse, Mar 12, 2007
    #6
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Big Daddy

    Java 1.2 Swing vs. Java 1.5 Swing

    Big Daddy, Apr 15, 2005, in forum: Java
    Replies:
    2
    Views:
    495
    Pete Barrett
    Apr 16, 2005
  2. mkrause
    Replies:
    0
    Views:
    669
    mkrause
    May 6, 2005
  3. Jelena Marin

    swing, JScrollPane

    Jelena Marin, Aug 15, 2006, in forum: Java
    Replies:
    2
    Views:
    427
    Thomas Hawtin
    Aug 15, 2006
  4. Bob
    Replies:
    1
    Views:
    905
    Knute Johnson
    Nov 28, 2006
  5. Replies:
    1
    Views:
    498
Loading...

Share This Page