0

I'm making a project that when you hit the button, an image appears in a JPanel.

But when I hit that button, nothing happens.

How to fix this code?

private void jButton3ActionPerformed(java.awt.event.ActionEvent evt) {                                         
    // button sluiten van de deur
     try {
           writer.println("execute(lock, \"aepu04:SI-Test\");");
           writer.flush(); // flushes the buffer

            String path = "http://chart.finance.yahoo.com/z?s=GOOG&t=6m&q=l";
            System.out.println("Get Image from " + path);
            URL url = new URL(path);
            BufferedImage image = ImageIO.read(url);
            System.out.println("Load image into frame...");
            JLabel label = new JLabel(new ImageIcon(image));
            JPanel panel = jPanel1;
            panel.add(label);

            panel.setVisible(true);
        } catch (Exception ex) {
            chatTextArea.append("Message was not sent. \n");
        }
Andrew Thompson
  • 168,117
  • 40
  • 217
  • 433
belmen
  • 35
  • 1
  • 7
  • 2
    One problem is that the panel is never added to anything. As a general solution though, declare the label as an attribute of the class. Add the label to the panel and the panel to the GUI at start-up. Then in the action performed method, load the image and call `label.setIcon(..)`. – Andrew Thompson Feb 22 '17 at 11:41
  • 1
    1) For better help sooner, post a [MCVE] or [Short, Self Contained, Correct Example](http://www.sscce.org/). 2) One way to get image(s) for an example is to hot link to images seen in [this Q&A](http://stackoverflow.com/q/19209650/418556). – Andrew Thompson Feb 22 '17 at 11:45
  • Change your url it must start with **https** to not a 'http' . I think secure connection needed... – tommybee May 09 '17 at 08:22

0 Answers0