Trey
Trey

Reputation: 73

How to correctly use repaint() and revalidate()?

I am new to JPanel and don't quite understand how to reset it. Once a picture is chosen and the user wants to select a new one, I want it to erase the old selected picture and paste the new one. But I'm not sure how. I've seen repaint() and revalidate() used, but I clearly don't know how to use it. So how do I accomplish this?

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import javax.imageio.ImageIO;
import javax.swing.ButtonGroup;
import javax.swing.ImageIcon;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JRadioButtonMenuItem;

public class Menu extends JFrame {
    JMenuBar menuBar;
    ButtonGroup pictureGroup, problemsGroup;
    BufferedImage picture1img, picture2img, picture3img;
    JMenu choiceOfThreePictures, numberOfProblems;
    JRadioButtonMenuItem picture1, picture2, picture3, fourProblems, nineProblems, sixteenProblems;
    // /Users/Administrator/Dropbox/Java/HW6Wilson/HW6Wilson/Picture1.jpg
    // /Users/Administrator/Dropbox/Java/HW6Wilson/HW6Wilson/Picture2.png
    // /Users/Administrator/Dropbox/Java/HW6Wilson/HW6Wilson/Picture3.jpg

    public Menu() {
        // Create the menu bar.
        menuBar = new JMenuBar();
        setJMenuBar(menuBar);

        // Create Picture choices on Menu Bar
        choiceOfThreePictures = new JMenu("Picture Choices");
        // Add Picture choices on Menu Bar
        menuBar.add(choiceOfThreePictures);
        // Create MenuItems onto Picture choices
        pictureGroup = new ButtonGroup();
        picture1 = new JRadioButtonMenuItem("Picture 1");
        picture2 = new JRadioButtonMenuItem("Picture 2");
        picture3 = new JRadioButtonMenuItem("Picture 3");
        // Add Picture Choices to Picutre choices menu
        choiceOfThreePictures.add(picture1);
        pictureGroup.add(picture1);
        choiceOfThreePictures.add(picture2);
        pictureGroup.add(picture2);
        choiceOfThreePictures.add(picture3);
        pictureGroup.add(picture3);

        // Create Number Of Problems on Menu Bar
        numberOfProblems = new JMenu("Number Of Problems");
        // Add Number Of problems on Menu Bar
        menuBar.add(numberOfProblems);
        // Create Menu Items onto Number Of problems
        problemsGroup = new ButtonGroup();
        fourProblems = new JRadioButtonMenuItem("4");
        nineProblems = new JRadioButtonMenuItem("9");
        sixteenProblems = new JRadioButtonMenuItem("16");
        // Add Number Of problems onto menu
        numberOfProblems.add(fourProblems);
        problemsGroup.add(fourProblems);
        numberOfProblems.add(nineProblems);
        problemsGroup.add(nineProblems);
        numberOfProblems.add(sixteenProblems);
        problemsGroup.add(sixteenProblems);

        // Start creating ActionListeners
        picture1.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent arg0) {
                System.out.println("Working");
                try {
                    picture1img = ImageIO.read(new File("/Users/Administrator/Dropbox/Java/HW6Wilson/HW6Wilson/Picture1.jpg"));
                    getContentPane().add(new JLabel(new ImageIcon(picture1img)));
                    revalidate();
                    repaint();
                    setVisible(true);

                } catch (IOException e) {
                    System.out.println("Couldn't find image.");
                }
            }
        });
        picture2.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent arg0) {
                System.out.println("Working");
                try {
                    picture2img = ImageIO.read(new File("/Users/Administrator/Dropbox/Java/HW6Wilson/HW6Wilson/Picture2.jpg"));
                    getContentPane().add(new JLabel(new ImageIcon(picture2img)));
                    revalidate();
                    repaint();
                    setVisible(true);
                } catch (IOException e) {
                    System.out.println("Couldn't find image.");
                }
            }
        });
        picture3.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent arg0) {
                System.out.println("Working");
                try {
                    picture3img = ImageIO.read(new File("/Users/Administrator/Dropbox/Java/HW6Wilson/HW6Wilson/Picture3.jpg"));
                    getContentPane().add(new JLabel(new ImageIcon(picture3img)));
                    revalidate();
                    repaint();
                    setVisible(true);

                } catch (IOException e) {
                    System.out.println("Couldn't find image.");
                }
            }
        });
    }

    public static void main(String[] args) {
        Menu mb = new Menu();
        mb.setSize(900, 700);
        mb.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        mb.setVisible(true);
    }
}

Upvotes: 0

Views: 99

Answers (1)

Antoniossss
Antoniossss

Reputation: 32517

You are using it just fine, you can even remote repaint() call as revalidate is more then enought. The problem is, that you are adding new images but you are not removing previously added images from the contentPane that is why it is not switching.

So if you do

                    picture3img = ImageIO.read(new File("/Users/Administrator/Dropbox/Java/HW6Wilson/HW6Wilson/Picture3.jpg"));
                    getContentPane().removeAll(); // HERE IS IMPORTANT PART
                    getContentPane().add(new JLabel(new ImageIcon(picture3img)));
                    revalidate();

It will work

The better approach would be to hold single label as a "image display" and use setIcon(icon) on JLabel insteed of changing whole labels.

Upvotes: 1

Related Questions