0

What I am trying to do is create a gui that allows you to select an image, display the image, and then perfom actions on it for a school project. This is what happens on startup. enter image description here Perfect! However, when I update the icon, it puts the new icon behind it: enter image description here

Here is the code I'm using on my Jlabel:

ImageIcon imageIcon = new ImageIcon();
        try {
            imageIcon = new ImageIcon(ImageIO.read(new File("/Users/ryanauger/Repos/JavaGUI/GUI/Images/cameraIcon.png")));
        } catch (IOException e2) {
            // TODO Auto-generated catch block
            imageIcon = new ImageIcon();
            e2.printStackTrace();
        }
        JLabel lblNewLabel = new MyJLabel(imageIcon);

        lblNewLabel.setBounds(0, 6, 600, 600);
        frame.getContentPane().add(lblNewLabel);

        JButton btnNewButton = new JButton("Pick Image File");
        btnNewButton.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                JFileChooser jfc;
                jfc = new JFileChooser();
                File f = new File(System.getProperty("user.dir"));
                jfc.setCurrentDirectory(f);
                jfc.setFileSelectionMode(JFileChooser.FILES_AND_DIRECTORIES);
                jfc.showOpenDialog(btnNewButton);
                File selFile = jfc.getSelectedFile();

                try {

                    lblNewLabel.setIcon(new ImageIcon(ImageIO.read(new File(selFile.getAbsolutePath()))));
                } catch (IOException e1) {
                    // TODO Auto-generated catch block
                    System.out.println(selFile.getAbsolutePath());
                    e1.printStackTrace();

                }



            }
        });

I am brand new to java, so any help is appreciated. Thanks!

EDIT: Here is the code for MyJLabel:

class MyJLabel extends JLabel
{
    ImageIcon imageIcon;
    public MyJLabel(ImageIcon icon)
    {
        super();
        this.imageIcon = icon;
    }
    @Override
    public void paintComponent(Graphics g)
    {
        super.paintComponent(g);
        g.drawImage(imageIcon.getImage(),0,0,getWidth(),getHeight(),this);
    }
}
MadProgrammer
  • 343,457
  • 22
  • 230
  • 366
Code Wiget
  • 1,540
  • 1
  • 23
  • 35
  • I tested this and it looks good on my screen. Although I notice you are using MyJLabel. Since I didn't have the code for that, I had to just use a JLabel. So I would suspect something in MyJLabel. Can you provide the code for that? – bcr666 Mar 17 '17 at 22:59
  • @Brianbcr666Ray Hey, thanks for the quick reply! I added an edit with MyJLabel. What this does is scales the image to fit the container. Thanks! – Code Wiget Mar 17 '17 at 23:03
  • 1
    `lblNewLabel.setBounds(0, 6, 600, 600);` Indicates you're using `null layout`. See the answers in [this question](http://stackoverflow.com/questions/6592468/why-is-it-frowned-upon-to-use-a-null-layout-in-swing) and [Null layout is evil](http://www.fredosaurus.com/notes-java/GUI/layouts/nulllayout.html) to know why you should avoid its use... Also there's no need to extend `JLabel` since you're not changing its behavior, so you could simply use a `JLabel` – Frakcool Mar 17 '17 at 23:15
  • I might suggest you use Canvas instead of JLabel. – bcr666 Mar 17 '17 at 23:28
  • @Brianbcr666Ray I wouldn't suggest mixing heavy wight components with lightweight containers, that just cause more issues – MadProgrammer Mar 17 '17 at 23:49
  • @Ryan You mean you want to do something like [this](http://stackoverflow.com/questions/14548808/scale-the-imageicon-automatically-to-label-size/14553003#14553003)? – MadProgrammer Mar 17 '17 at 23:54
  • Don't extend a JLabel. First of all your implementation is not complete. Your label no longer has a preferred size. You didn't override the setIcon() method so you have painting problems. Instead you can use the [Stretch Icon](https://tips4java.wordpress.com/2012/03/31/stretch-icon/). This Icon can be used on any component that supports an Icon and the Icon will dynamically resize to fill the space available. – camickr Mar 18 '17 at 00:13

1 Answers1

3

There's your problem, when you call

JLabel lblNewLabel = new MyJLabel(imageIcon);

You are storing the icon in your own and not passing it to the super class. So when you paint, it paints the camera. Then when you call:

lblNewLabel.setIcon(new ImageIcon(ImageIO.read(new File (selFile.getAbsolutePath()))));

You are actually calling the setIcon() method of the super class, you are not changing the Icon you are storing locally. Then the super class draws the phone, like it should, but you are also drawing the camera over it.

bcr666
  • 2,157
  • 1
  • 12
  • 23