aspose file tools*
The moose likes Swing / AWT / SWT and the fly likes show image on button click Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Java » Swing / AWT / SWT
Bookmark "show image on button click" Watch "show image on button click" New topic
Author

show image on button click

sigireddi sowmya
Greenhorn

Joined: Mar 18, 2007
Posts: 3
Hi,

when i am clicking on a button a label should show an image,but its not working.CAn anyone help me with this
ActionListener ac=new ActionListener() {public void actionPerformed(ActionEvent ae)
{
ImageIcon I1=new ImageIcon();
String s1=((JButton)ae.getSource()).getText();
if(s1.equals("Classic")) { I1=new ImageIcon("G:\\java cd1\\prgs\\Classic.jpg"); ShowImageLabel=new JLabel(I1);}
pete stein
Bartender

Joined: Feb 23, 2007
Posts: 1561
Have you checked to see if your prog is reading in the image file ok?? You might want to slip in a call to see if the image file is being recognized as existing like so:

[ April 01, 2007: Message edited by: pete stein ]
Jean-Yves Tinevez
Greenhorn

Joined: Feb 12, 2007
Posts: 22
Hi Sigireddi! (everyone on this forum deserve one capital in its name, even my mom)

Try this:

pete stein
Bartender

Joined: Feb 23, 2007
Posts: 1561
Also, rather than the line "ShowImageLabel=new JLabel(I1);" Why not use:
sigireddi sowmya
Greenhorn

Joined: Mar 18, 2007
Posts: 3
Tnaks Pete,its working now.
narender reddy
Greenhorn

Joined: Mar 31, 2007
Posts: 22
try these
ImageIcon image = new ImageIcon("C:/images/your_image.gif");


or
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class AppIconFrame extends JFrame {
public AppIconFrame() {
enableEvents(AWTEvent.WINDOW_EVENT_MASK);
initFrame();
}
//Frame initialization
private void initFrame(){
this.setSize(new Dimension(400, 300));
this.setTitle("Custom Icon");
ImageIcon image = new ImageIcon("c:\yourpath\yourfile.gif");
this.setIconImage(image.getImage());
}
//Overridden so we can exit on System Close
protected void processWindowEvent(WindowEvent e) {
super.processWindowEvent(e);
if(e.getID() == WindowEvent.WINDOW_CLOSING) {
System.exit(0);
}
}
public static void main(String[] args){
AppIconFrame frame = new AppIconFrame();
frame.setVisible(true);
}
}


Thank you everybody. Much appreciated.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: show image on button click