Hi,
Please let me know how to display an image to a click of a button.
Thanks
syed
Welcome to the Java Programming Forums
The professional, friendly Java community. 21,500 members and growing!
The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.
>> REGISTER NOW TO START POSTING
Members have full access to the forums. Advertisements are removed for registered users.
Hi,
Please let me know how to display an image to a click of a button.
Thanks
syed
Andrea, www.andbin.net — SCJP 5 (91%) – SCWCD 5 (94%)
Useful links for Java beginners – My new project Java Examples on Google Code
i have a image stored in my local pc. i should have to create a application which should have one button in it. If we click the button then it should fetch that image and show it. Could you please let me know how to do.
Sorry but there are still many obscure points:
1) You have not told if you want to create an AWT or Swing application (there is another framework called SWT but it's not very common).
2) You have not told what type of image is. Here it means effectively the format/extension. Is it a "common" .jpg/.png/.gif ? .... or some other more exotic format?
3) You have not told if the application must "know" exactly about that image or if the image can/should be choosen by the user.
4) You have not explained, in general, how the user interface should appear.
5) You have not even tried to write some code ....
Andrea, www.andbin.net — SCJP 5 (91%) – SCWCD 5 (94%)
Useful links for Java beginners – My new project Java Examples on Google Code
Here's the intro page to the Swing tutorials.
The Working with Graphics lesson should cover the essential elements you need to accomplish what you've described.
Come back when you have code and need help.
Please let me know whats wrong with my code, My code is as follows:
package my.showwhenpress;
import java.awt.Image;
import java.io.IOException;
import java.net.URL;
import javax.imageio.ImageIO;
import javax.swing.ImageIcon;
import javax.swing.JFrame;
import javax.swing.JLabel;
/**
*
* @author administrator
*/
public class showwhenpress extends javax.swing.JFrame {
/**
* Creates new form showwhenpress
*/
public showwhenpress() {
initComponents();
}
/**
* This method is called from within the constructor to initialize the form.
* WARNING: Do NOT modify this code. The content of this method is always
* regenerated by the Form Editor.
*/
@SuppressWarnings("unchecked")
// <editor-fold defaultstate="collapsed" desc="Generated Code">
private void initComponents() {
jButton1 = new javax.swing.JButton();
setDefaultCloseOperation(javax.swing.WindowConstan ts.EXIT_ON_CLOSE);
jButton1.setText("jButton1");
jButton1.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jButton1ActionPerformed(evt);
}
});
javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
getContentPane().setLayout(layout);
layout.setHorizontalGroup(
layout.createParallelGroup(javax.swing.GroupLayout .Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addGap(123, 123, 123)
.addComponent(jButton1)
.addContainerGap(205, Short.MAX_VALUE))
);
layout.setVerticalGroup(
layout.createParallelGroup(javax.swing.GroupLayout .Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addGap(57, 57, 57)
.addComponent(jButton1)
.addContainerGap(213, Short.MAX_VALUE))
);
pack();
}// </editor-fold>
private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {
public static void main( String[] args )
{
Image image = null;
try {
URL url = new URL("http://www.mkyong.com/image/mypic.jpg");
image = ImageIO.read(url);
} catch (IOException e) {
e.printStackTrace();
}
JFrame frame = new JFrame();
frame.setSize(300, 300);
JLabel label = new JLabel(new ImageIcon(image));
frame.add(label);
frame.setVisible(true);
}
// TODO add your handling code here:
}
/**
* @param args the command line arguments
*/
//public static void main(String args[]) {
/* Set the Nimbus look and feel */
//<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
/* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.
* For details see How to Set the Look and Feel (The Java™ Tutorials > Creating a GUI With JFC/Swing > Modifying the Look and Feel)
*/
try {
for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
if ("Nimbus".equals(info.getName())) {
javax.swing.UIManager.setLookAndFeel(info.getClass Name());
break;
}
}
} catch (ClassNotFoundException ex) {
java.util.logging.Logger.getLogger(showwhenpress.c lass.getName()).log(java.util.logging.Level.SEVERE , null, ex);
} catch (InstantiationException ex) {
java.util.logging.Logger.getLogger(showwhenpress.c lass.getName()).log(java.util.logging.Level.SEVERE , null, ex);
} catch (IllegalAccessException ex) {
java.util.logging.Logger.getLogger(showwhenpress.c lass.getName()).log(java.util.logging.Level.SEVERE , null, ex);
} catch (javax.swing.UnsupportedLookAndFeelException ex) {
java.util.logging.Logger.getLogger(showwhenpress.c lass.getName()).log(java.util.logging.Level.SEVERE , null, ex);
}
//</editor-fold>
/* Create and display the form */
// java.awt.EventQueue.invokeLater(new Runnable() {
// public void run() {
// new showwhenpress().setVisible(true);
//
//}
//});
// }
// Variables declaration - do not modify
private javax.swing.JButton jButton1;
// End of variables declaration
//}
Your code has serious syntax errors (just one: you are trying to declare the main method into jButton1ActionPerformed ...).
But apart this, your code is also overly complicated because you have used a GUI editor to create the user interface.
GUI editors are not bad by itself .... but they tend to generate code that is: a) complex, b) "smoky", c) too verbose, d) hard to read and maintain
Please, avoid the use of GUI editor and instead learn how to develop GUIs with simple/common components and layout managers.
Andrea, www.andbin.net — SCJP 5 (91%) – SCWCD 5 (94%)
Useful links for Java beginners – My new project Java Examples on Google Code