Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Java > [swing] JscrollPane to display a changing image

Reply
Thread Tools

[swing] JscrollPane to display a changing image

 
 
oliviergir@gmail.com
Guest
Posts: n/a
 
      03-10-2007
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 ??

 
Reply With Quote
 
 
 
 
Knute Johnson
Guest
Posts: n/a
 
      03-10-2007
http://www.velocityreviews.com/forums/(E-Mail Removed) 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/
 
Reply With Quote
 
 
 
 
oliviergir@gmail.com
Guest
Posts: n/a
 
      03-11-2007
On 10 mar, 17:08, Knute Johnson <(E-Mail Removed)>
wrote:
> (E-Mail Removed) 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,jScrollPa ne);

>
> > 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)

 
Reply With Quote
 
Lew
Guest
Posts: n/a
 
      03-11-2007
(E-Mail Removed) 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
 
Reply With Quote
 
Lars Enderin
Guest
Posts: n/a
 
      03-11-2007
Lew skrev:
> (E-Mail Removed) 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.
 
Reply With Quote
 
Luc The Perverse
Guest
Posts: n/a
 
      03-12-2007
"Lars Enderin" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed)...
> Lew skrev:
>> (E-Mail Removed) 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




 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Can I nest a JEditorPane inside a JScrollPane inside a JPanel? Mike Java 5 12-02-2003 01:45 PM
Problem with JList, DefaultListModel and JScrollPane Tobi Krausl Java 0 11-12-2003 11:20 AM
change jscrollpane bar? soni29 Java 0 08-21-2003 07:39 PM
TextArea in JScrollPane extends, not shrinks Miguel De Anda Java 0 08-13-2003 08:40 PM
JScrollPane scroll to top Richard Trahan Java 0 07-26-2003 04:01 PM



Advertisments