Question

New Details:

Repaint method of my JPanel object does not work when called from inside actionPerformed event of my main class. It DOES work inside the ImageViewer method of the main class ImageViewer but does not work inide the actionPerformed method of this class.

These are the essential parts of my code which are not working properly ( repainting part ):

Main Class:

/**
 * @(#)NeatImageViewer.java
 *
 * NeatImageViewer application
 *
 * @author
 * @version 1.00 2010/11/1
 */

import java.awt.*;
import java.awt.event.*;
import java.awt.image.*;
import java.io.*;
import javax.imageio.*;
import javax.swing.*;


public class NeatImageViewer extends JFrame implements ActionListener {

    GraphicsPanel gp;

    NeatImageViewer() {
               //... window components ...
    }

    public static void main(String[] args) {

        NeatImageViewer niv = new NeatImageViewer();
        niv.setSize(500,500);
        niv.setLocationRelativeTo(niv);
        niv.setVisible(true);
    }

    public void actionPerformed(ActionEvent e) {
            //...
                gp = new GraphicsPanel();
                gp.img = img;
                gp.repaint(); //<!--- Not Working!
                this.add(gp);
            //...
    }

}

GraphicsPanel Class:

/**
 * @(#)GraphicsPanel.java
 *
 *
 * @author
 * @version 1.00 2010/11/1
 */

import java.awt.*;
import java.awt.event.*;
import java.awt.image.*;
import java.io.*;
import javax.imageio.*;
import javax.swing.*;


public class GraphicsPanel extends JPanel {
    BufferedImage img;

    GraphicsPanel() {
        super();
    }

    protected void paintComponent(Graphics g) {
        //paint method isn't executed???
        super.paintComponent(g);
        System.out.println("Paint!");
    }

}
Was it helpful?

Solution

In Swing you don't use a Canvas. You do custom painting on a JPanel or JComponent and you override the paintComponent(...) method as already stated. Read the Swing tutorial on Custom Painting for more information and working examples.

Also, with Swing there is no need to create a custom components to show an image. You just use a JLabel with an ImageIcon. Read the section on "How to Use Icons".

Bookmark the tutorial for all the Swing basics.

Edit:

When you add a component to a visible GUI the basic code should be:

panel.add( ... );
panel.revalidate();
panel.repaint();

OTHER TIPS

You have to override paintComponent(Graphics g) instead of paint(Graphics g).
So, the method you posted should be renamed to paintComponent.

EDIT: Have you pack()'ed your frame when your initialization is done?

EDIT: The repaint() method does nothing when the component isn't visible. So, you have to add it first to the JFrame, pack() the frame. After packing it, repainting isn't needed anymore.

(I'm Skyfe but from another IP so ain't "logged in" and couldn't comment)

I'm sorry but I don't get what's the problem???

I offered my code and it is NOT executing the paintComponent function upon calling repaint from the actionPerformed method. Just copy paste my code and compile it with a java compiler and it does NOT execute the paintComponent method upon performing an action which I can tell because I used a System.out.println() method inside the paint method. And no it does not paint anything because I'm just trying to check whether it calls the paint method AT ALL because I put a system output inside the paintComponent method which is not executed when using repaint. It was just a test and it didn't work.

So what you mean by where is the SSCCE, this is all the code I have. I compiled exactly the code I posted in my main post and the problem with it is that it does not show any system output when an action was performed ( and the repaint event was called ). I fail to see what misses in my post?

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top