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.

Results 1 to 8 of 8

Thread: How to display an image to a click of a button

  1. #1
    Junior Member
    Join Date
    Sep 2013
    Posts
    4
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default How to display an image to a click of a button

    Hi,

    Please let me know how to display an image to a click of a button.

    Thanks
    syed


  2. #2
    Member andbin's Avatar
    Join Date
    Dec 2013
    Location
    Italy
    Posts
    443
    Thanks
    4
    Thanked 122 Times in 114 Posts

    Default Re: How to display an image to a click of a button

    Quote Originally Posted by syedjavidali View Post
    Please let me know how to display an image to a click of a button.
    Sorry but your question is a bit vague and not much contextualized. Would you like to use AWT? Or, better, Swing? Can you explain, with words, the user interface of the application you want to do?
    Andrea, www.andbin.net — SCJP 5 (91%) – SCWCD 5 (94%)

    Useful links for Java beginners – My new project Java Examples on Google Code

  3. #3
    Junior Member
    Join Date
    Sep 2013
    Posts
    4
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: How to display an image to a click of a button

    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.

  4. #4
    Member andbin's Avatar
    Join Date
    Dec 2013
    Location
    Italy
    Posts
    443
    Thanks
    4
    Thanked 122 Times in 114 Posts

    Default Re: How to display an image to a click of a button

    Quote Originally Posted by syedjavidali View Post
    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

  5. #5
    Junior Member
    Join Date
    Sep 2013
    Posts
    4
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: How to display an image to a click of a button

    Quote Originally Posted by andbin View Post
    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 ....
    1.I have to create a swing application.
    2.The image is jpeg
    3.The image should be specified in the program itself.
    4.The UI should be with only one button in it.
    5.Since i am new to tjhis java, i am in learning phase. So coild you please help me out.

  6. #6
    Super Moderator
    Join Date
    Jun 2013
    Location
    So. Maryland, USA
    Posts
    5,135
    My Mood
    Mellow
    Thanks
    187
    Thanked 660 Times in 647 Posts

    Default Re: How to display an image to a click of a button

    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.

  7. #7
    Junior Member
    Join Date
    Sep 2013
    Posts
    4
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: How to display an image to a click of a button

    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
    //}

  8. #8
    Member andbin's Avatar
    Join Date
    Dec 2013
    Location
    Italy
    Posts
    443
    Thanks
    4
    Thanked 122 Times in 114 Posts

    Default Re: How to display an image to a click of a button

    Quote Originally Posted by syedjavidali View Post
    Please let me know whats wrong with my code
    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

Similar Threads

  1. How can I incorporate images when I click a button
    By SwagKingzMon in forum Java Theory & Questions
    Replies: 1
    Last Post: May 18th, 2013, 07:17 PM
  2. How to close a frame on a button click ??
    By rk0887 in forum AWT / Java Swing
    Replies: 1
    Last Post: August 22nd, 2012, 08:58 AM
  3. Lock up code to the click of a button!!!
    By Allen Walker in forum Java Theory & Questions
    Replies: 2
    Last Post: July 1st, 2011, 09:12 AM
  4. How to display a button with an image on it?
    By ice in forum AWT / Java Swing
    Replies: 10
    Last Post: November 13th, 2010, 05:20 AM
  5. close JDialog on button click
    By Christophe in forum AWT / Java Swing
    Replies: 4
    Last Post: April 4th, 2010, 11:04 PM