Win a copy of Functional Reactive Programming this week in the Other Languages forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

show image on button click

 
sigireddi sowmya
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 1561
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 22
Eclipse IDE Mac Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Sigireddi! (everyone on this forum deserve one capital in its name, even my mom)

Try this:

 
pete stein
Bartender
Posts: 1561
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Also, rather than the line "ShowImageLabel=new JLabel(I1);" Why not use:
 
sigireddi sowmya
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Tnaks Pete,its working now.
 
narender reddy
Greenhorn
Posts: 22
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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);
}
}
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic