0

I have a code that is working only in showing a JLabel icon on a JPanel in case of printing the label in a showMessageDialog before adding the label to the JPanel and then revalidate and repaint. If I removed the message dialog, it wouldn't be viewed on the panel, what does that mean?

Is it that the message dialog does a step that i should do in some missing statement(s)?

(Note: jPanel4 is contained by jPanel1)

This is the code, which I use:

    BufferedImage workerImage = ImageIO.read(workerImageFile);
    ImageIcon icon = new ImageIcon(workerImage.getScaledInstance(jPanel4.getWidth(), jPanel4.getHeight(), BufferedImage.SCALE_SMOOTH));

    workerImageLabel = new JLabel("", icon, JLabel.CENTER);

    JOptionPane.showMessageDialog(null, workerImageLabel);

    if(jPanel4.getComponentCount() == 0)
    {
       jPanel4.add(workerImageLabel, BorderLayout.CENTER );

       jPanel1.revalidate();
       jPanel1.repaint();
    }else
    {
       jPanel4.remove(0);
       jPanel4.add(workerImageLabel, BorderLayout.CENTER );

       jPanel1.revalidate();
       jPanel1.repaint();                    
    }
  • 1
    Consider calling `revalidate` and `repaint` on `jPanel4`, since it's the panel which has actually changed – MadProgrammer Oct 17 '17 at 21:14
  • A `JLabel` with no text, icon or visible border is invisible, so it would be better to add it to the GUI at start-up and simply change the icon as needed. General tips: 1) If *swapping* components, use a [`CardLayout`](http://download.oracle.com/javase/8/docs/api/java/awt/CardLayout.html) as shown in [this answer](http://stackoverflow.com/a/5786005/418556). 2) For better help sooner, post a [MCVE] or [Short, Self Contained, Correct Example](http://www.sscce.org/). 3) 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 Oct 18 '17 at 00:46
  • BTW - `workerImageLabel` this suggests that the Event Dispatch Thread might be blocked. For anything more than guesses though, post that MCVE / SSCCE. – Andrew Thompson Oct 18 '17 at 00:51
  • Thank you @MadProgrammer , Actually i tried this before asking, it doesn't work. However, revalidate affect ancestors and subcomponents. – Hamza Muhammed Oct 18 '17 at 12:09
  • Thank you @AndrewThompson, very much, i did your advice [ it would be better to add it to the GUI at start-up and simply change the icon as needed ] , and solved the problem, icon changed with just set it to jLabel and revalidate and repaint JPanel, I added jLabel to JPanel at start-up As you said. Any one have an explaination of that ? why should be added at startup , and in case of not adding it, why should showMessageDialog before changing the icon to be shown ? – Hamza Muhammed Oct 18 '17 at 12:25
  • Thanks Also @AndrewThompson for your advice to post minimal, complete and verifiable example or short self contained, correct example. – Hamza Muhammed Oct 18 '17 at 12:29
  • *"why should be added at startup , and in case of not adding it, why should showMessageDialog before changing the icon to be shown ?"* Packing a top level container will automatically validate the location and size of the components it contains. I would tend to 'pre-assign' space for a label intended to hold an image by giving it a transparent (i.e. invisible) image of the same size. That transparent image can be generated in code this easily.. `BufferedImage invisibleImage = new BufferedImage(w,h,BufferedImage.TYPE_INT_ARGB); // the A of ARGB stands for Alpha (transparency)` – Andrew Thompson Oct 18 '17 at 12:35

0 Answers0