Smaller code to test my problem:
/*Author: Ajay Partoti
This is the smallest code to illustrate my problem
Before running this program
Please make sure u had screen.jpg and actionsdemo.jpg in your
Test program directory*/
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
public class Test extends JFrame
{
//Declare variables....
private JPanel picpnl;
private JButton refresh;
private Image image;
public Test()
{
//Set container to BorderLayout....
Container c = getContentPane();
c.setLayout( new BorderLayout() );
//Add button to container
refresh=new JButton("refresh");
image = Toolkit.getDefaultToolkit( ).getImage("screen.jpg");
//Add actionListener to button refresh...
refresh.addActionListener(
new ActionListener(){
public void actionPerformed(ActionEvent e)
{
if(e.getActionCommand()=="refresh")
{
/*THIS IS WHERE THE PROBLEM IS...
AT RUNTIME I DIDN'T GET THE SECOND IMAGE TO PAINT ON JPANEL*/
image = Toolkit.getDefaultToolkit( ).getImage("actionsdemo.jpg");
picpnl.add(new JScrollPane(new
ImageComponent(image)),BorderLayout.CENTER);
picpnl.setVisible(true);
JOptionPane.showMessageDialog(Test.this,"I didn't get the new
image on JPanel...");
}
}
}
);
//Declare picpnl as JPanel
picpnl = new JPanel();
picpnl.setLayout(new BorderLayout());
//Add scrollPane component by using Imagecomponent class
picpnl.add(new JScrollPane(new
ImageComponent(image)),BorderLayout.CENTER);
picpnl.setVisible(true);
//add button and picpnl and set visible
c.add(refresh,BorderLayout.NORTH);
c.add(picpnl,BorderLayout.CENTER);
picpnl.setVisible(true);
setSize(300,300);
show();
}
class ImageComponent extends JComponent {
Image image;
Dimension size;
public ImageComponent(Image image) {
this.image = image;
//Set MediaTracker to observer images...
MediaTracker mt = new MediaTracker(this);
mt.addImage(image, 0);
try {
mt.waitForAll( );
}
catch (InterruptedException e) {
// error ...
};
//get and set Dimensions
size = new Dimension (image.getWidth(null),
image.getHeight(null));
setSize(size);}
public void paint(Graphics g) {
//repaint images on the screen....
g.drawImage(image, 0, 0, this);
}
public Dimension getPreferredSize( ) {
//Get the size...
return size;
}
}
public static void main(String[] args)
{
Test app = new Test();
app.addWindowListener(
new WindowAdapter(){
public void windowClosing( WindowEvent e )
{
System.exit( 0 );
}
}
);
}
};
Roedy Green - 03 Oct 2005 20:44 GMT
>Smaller code to test my problem:
Just noticing the rather jagged indentation. That might be tabs or it
might be that you don't have an IDE to maintain that for you. You are
in for quite treat if you have never used an IDE.
See http://mindprod.com/jgloss/ide.html

Signature
Canadian Mind Products, Roedy Green.
http://mindprod.com Again taking new Java programming contracts.
ajay - 03 Oct 2005 20:51 GMT
Thanks sir,
I got very happy by seeing you on this forum.
I like your site mindprod very much because it contain all the glossary
keywords of java.Please help me to solve my problem.
Thank You.
ajay - 03 Oct 2005 21:38 GMT
I used your imageviewer program with my code...
But it didn't solve my problem..
But the original problem is:
if AFTER RUNNING THE PROGRAM I DELETE AND REPLACE A FILE WITH THE SAME
NAME
AND TRY TO DISPLAY THAT FILE AFTER PRESSING REFRESH IT IS NOT
WORKING...
Change in the program...
if(e.getActionCommand()=="refresh")
{
/*BUT IF I CHANGE THE SECOND IMAGE TO THE SAME IMAGE NAME
image = Toolkit.getDefaultToolkit().getImage("screen.jpg");
picpnl.removeAll();
picpnl.add(new JScrollPane(new
ImageComponent(image)),BorderLayout.CENTER);
picpnl.setVisible(true);
validate();
}
I appreciate your help but the above is a original problem...
Roedy Green - 03 Oct 2005 20:49 GMT
> class ImageComponent extends JComponent
I wrote a similar class called ImageViewer.
You could plug my code in instead. If it works you know the problem
is in ImageComponent. You can compare your code and mine to find the
problem. If it does not work, then the problem is elsewhere.
Source is onted at:
http://mindprod.com/jgloss/image.html

Signature
Canadian Mind Products, Roedy Green.
http://mindprod.com Again taking new Java programming contracts.
Vova Reznik - 03 Oct 2005 21:52 GMT
> I wrote a similar class called ImageViewer.
It is awt, not swing.
Roedy Green - 04 Oct 2005 09:39 GMT
>> I wrote a similar class called ImageViewer.
>
>It is awt, not swing.
You could create a Swing version by changing Canvas to JPanel and
paint to paintComponent.

Signature
Canadian Mind Products, Roedy Green.
http://mindprod.com Again taking new Java programming contracts.
ajay - 04 Oct 2005 10:15 GMT
Problem is solved by Andrew Thanks...
Roedy Green - 04 Oct 2005 10:34 GMT
>Problem is solved by Andrew Thanks...
What was the problem?

Signature
Canadian Mind Products, Roedy Green.
http://mindprod.com Again taking new Java programming contracts.