Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / General / March 2007

Tip: Looking for answers? Try searching our database.

[swing] JscrollPane to display a changing image

Thread view: 
oliviergir@gmail.com - 10 Mar 2007 10:58 GMT
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 ??
Knute Johnson - 10 Mar 2007 16:08 GMT
> Hi,
> I am displaying a big image within a smaller JcrollPane component like
[quoted text clipped - 20 lines]
>
> 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);
    }
}

Signature

Knute Johnson
email s/nospam/knute/

oliviergir@gmail.com - 11 Mar 2007 12:57 GMT
On 10 mar, 17:08, Knute Johnson <nos...@rabbitbrush.frazmtn.com>
wrote:
> olivier...@gmail.com wrote:
> > Hi,
[quoted text clipped - 75 lines]
>
> - 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)
Lew - 11 Mar 2007 15:44 GMT
>> - 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
Lars Enderin - 11 Mar 2007 23:33 GMT
Lew skrev:
>>> - Masquer le texte des messages précédents -
>>>
[quoted text clipped - 10 lines]
>
> I want to know so as to be certain never to use it.

It's Google Groups.
Luc The Perverse - 12 Mar 2007 03:11 GMT
> Lew skrev:
>>>> - Masquer le texte des messages précédents -
[quoted text clipped - 13 lines]
>
> 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

:)


Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.