2

I created a JFrame in Netbeans and added a JPanel (automatically declared in non-editable code as private javax.swing.JPanel jPanel1).

I have a button on my form and would like to display an image in the panel upon clicking the button - however I'm not sure what code I need to display the image.

Paul Samsotha
  • 205,037
  • 37
  • 486
  • 720
Magda
  • 147
  • 1
  • 2
  • 12
  • 3
    You don't need code, you need a [tutorial](http://docs.oracle.com/javase/tutorial/uiswing/). – takendarkk Jan 02 '14 at 15:45
  • 1
    `panel.add(new JLabel(new ImageIcon("imagePath")));` – nachokk Jan 02 '14 at 15:48
  • It has already been discussed: – Aroon Jan 02 '14 at 15:48
  • There are a dozen different ways to do it. I prefer simply putting the image into a centered `JLabel`. You can see an example of that in [this answer](http://stackoverflow.com/a/13463684/418556). [What have you tried?](http://www.whathaveyoutried.com/) – Andrew Thompson Jan 02 '14 at 15:51
  • *"Please help!"* Please ask a (specific) question. – Andrew Thompson Jan 02 '14 at 15:53
  • I have tried using `BufferedImage`, `ImageIcon` and the code @nachokk mentioned above but nothing seems to display in the panel – Magda Jan 02 '14 at 16:05
  • 2
    @Magda it's a good time to edit your post and show us what have you tried so far – nachokk Jan 02 '14 at 16:07
  • Tips: 1) Add @csmckelvey (or whoever) the `@` is important) to *notify them* of a new comment. 2) Show what you tried in code as an [edit to the question](http://stackoverflow.com/posts/20886415/edit). 3) Report how you went adapting the SSCCE I linked to. – Andrew Thompson Jan 02 '14 at 16:09
  • Thanks for all the suggestions, I ended up using the following code in my `ActionPerformed` method: `try { BufferedImage myPicture = ImageIO.read(new File("test-data\\mozart.jpg")); jLabel1.setIcon(new ImageIcon(myPicture)); } catch (IOException e) { System.err.println(e); }` – Magda Jan 02 '14 at 20:20

2 Answers2

5

Follow these steps

  1. Create new JFrame Form (DUH)
  2. Drag a JPanel to your frame (jPanel1);
  3. Drag a JLabel into that JPanel (jLabel1);

  4. Right - click on your project, and create a new package named "resources". You do this so the image will be imported into your project in the jar

  5. Hightlight your JLabel and open your properties pane

  6. Click on the ... button to the right of the icon property.
  7. Select "External Image", click the ... button to select an image, then click "Import to Project" and click OK
  8. You should see the icon in the frame

  9. Drag a JButton into the frame

  10. Right - click the button, select "Events -> Actions -> actionPerformed"

  11. Go your source code, in your constructor add this

    initComponents();
    jPanel1.setVisible(false);  <------
    
  12. In your actionPerfomed, add this

    private void jButton1ActionPerformed(java.awt.event.ActionEvent evt)   {                                         
        jPanel1.setVisible(true);   <-------
    } 
    
  13. Run your masterpiece. Try and click the button.

Paul Samsotha
  • 205,037
  • 37
  • 486
  • 720
2

One possible solution

jPanel1 = new JPanel();
jPanel1.add(new JLabel(new ImageIcon("imagePath")));
jPanel.setVisible(false);
//add this panel to the frame

And then when your button is clicked.

myButton.addActionListener(new ActionListener(){ 
     @Override
     public void actionPerformed(ActionEvent e) {
          jPanel1.setVisible(true);
     }
});
nachokk
  • 14,363
  • 4
  • 24
  • 53