[swing] JscrollPane to display a changing image

O

oliviergir

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 ??
 
K

Knute Johnson

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);
}
}
 
O

oliviergir

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

Lew

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
 
L

Lars Enderin

Lew skrev:
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.
 
L

Luc The Perverse

Lars Enderin said:
Lew skrev:

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.
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,744
Messages
2,569,483
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top