0

I'm trying to code a JButton in Netbeans and I want to display an image from my local disk when the button is clicked. This button is inside a panel.

How to display an image when I click a button?

Andrew Thompson
  • 168,117
  • 40
  • 217
  • 433
Pan24112012
  • 307
  • 2
  • 5
  • 10
  • 3
    [What have you tried?](http://www.whathaveyoutried.com/) Where are you stuck? – Andrew Thompson Nov 30 '12 at 03:46
  • 1
    Add a `JLabel` to a `JFrame`. Set the image to the `JLabel`, show the frame. You might like to have read through [this](http://docs.oracle.com/javase/tutorial/uiswing/components/icon.html) – MadProgrammer Nov 30 '12 at 03:50
  • 1
    See [this answer](http://stackoverflow.com/a/13512826/418556) for a GUI that can display images, and [this answer](http://stackoverflow.com/a/13521592/418556) for a file chooser suited to selecting images. Pop the file chooser on action event, then show the image(s) in the GUI. – Andrew Thompson Nov 30 '12 at 03:51
  • Sorry my question was not clear in fact i want to display an image from my local disk in a Message dialog, so this is what i tried private void jButtonHelpActionPerformed(java.awt.event.ActionEvent evt) { final ImageIcon icon = new ImageIcon("D:\\folder\\help.png"); JOptionPane.showMessageDialog(null, JOptionPane.INFORMATION_MESSAGE, icon); – Pan24112012 Nov 30 '12 at 04:20
  • 1) Edit code into questions and use code formatting. It is almost unreadable in comments. 2) The only new information (short of code snippets) is 'Message dialog' or `JOptionPane` instead of `JFrame` as it might more commonly be displayed in. 3) What happened when you ran that code? What did you expect to happen? -- We need more information from you, if we are to help you solve whatever the problem is. – Andrew Thompson Nov 30 '12 at 04:23
  • Thank you sir, it's working in fact my path was not correct – Pan24112012 Nov 30 '12 at 04:30

0 Answers0