Java
--
Questions
--
Followers
Top Experts
This is the class code:
import javax.swing.*;
import java.io.*;
import java.awt.*;
import java.awt.event.*;
public class FilePanel extends JPanel {
protected Image currentImage;
private boolean isFile = false;
private final int fileWidth = 100, fileHeight = 60;
private int count = 0;
public FilePanel() {
setBorder (BorderFactory.createTitle
}
public void setFile(String fileName) {
Toolkit toolkit = Toolkit.getDefaultToolkit(
currentImage = toolkit.getImage(fileName)
System.out.println(fileNam
isFile = true;
repaint();
}
public void refreshPanel ()
{
isFile = false;
repaint();
}
public void paintComponent (Graphics graphics)
{
super.paintComponent(graph
if (isFile)
{
graphics.drawImage(current
0, fileWidth, fileHeight, this);
}
}
}
I'll be happy to hear any suggestions.
Thanks
Zero AI Policy
We believe in human intelligence. Our moderation policy strictly prohibits the use of LLM content in our Q&A threads.
I'm not using ImageIcon, because I want to control the size of the image.
If you have any other ideas I'll be happy to hear them.
Thanks, Gabi
new Exception().printStackTrac






EARN REWARDS FOR ASKING, ANSWERING, AND MORE.
Earn free swag for participating on the platform.
g.drawImage(imageicon.getI

Get a FREE t-shirt when you ask your first question.
We believe in human intelligence. Our moderation policy strictly prohibits the use of LLM content in our Q&A threads.
It won't change how often paintComponent() gets called.
object is right, paintComponent is always called multiple times when swing desides that it's time to repaint component. Any time your component is covered by another component then uncovered it needs to be repainted. Any time your component resized or moved it needs to be repainted. Any time foreground or background color changes, swing component will be repainted. There are other properties which trigger repaint when changed.
I don't believe that hundreds of times is a norm for paintComponent calls. But plain search for paintComponent() calls won't help you to find problem. I think that printing stack trace from the repaint method would help to find what triggers call to paintComponent()--typicall
Regards,
Igor Bazarny
I'm sorry I couldn't get back to you yesterday.
The exception is always the same and it says:
at com.Gabi.FilePanel.paintCo
at javax.swing.JComponent.pai
at javax.swing.JComponent.pai
at javax.swing.JComponent._pa
at javax.swing.JComponent.pai
at javax.swing.RepairManager.
at javax.swing.SystemEventQue
...and it goes on.
If you have any ideas or suggestions, please share them with me.
Thanks, Gabi






EARN REWARDS FOR ASKING, ANSWERING, AND MORE.
Earn free swag for participating on the platform.
Regards,
Igor Bazarny

Get a FREE t-shirt when you ask your first question.
We believe in human intelligence. Our moderation policy strictly prohibits the use of LLM content in our Q&A threads.
objects I did not understand your question. When paintComponent is not called by me it is not active, but when I call it once, it performs a few hundred loops.
Thanks for your help, Gabi
You should never need to call paintComponent().
Why are you calling it?
It was trace form the paintComponent(). It was initiated by swing, because some part of your component was marked as 'dirty'. Normal way to mark part of the component 'dirty' is call to repaint(), which is not the same as call to paint() or paintComponent(). I'd like to know why some part of your component was marked 'dirty'. I hope it will help us to understand why your paintComponent() is called so many times.
And objects is right, normally you don't need to call paintComponent().
Regards,
Igor Bazarny






EARN REWARDS FOR ASKING, ANSWERING, AND MORE.
Earn free swag for participating on the platform.
First of all, how can I check which part of my component is dirty, or why has it become dirty?
Second, I'm using paintComponent in order to display an Image (a gif file) on a panel. This is the most straightforward way I know, but I'm open to any suggestions.
Thanks, Gabi
You can (and should) remove all calls to paintComponent().
Gabi, I'm getting nervous. Do you understand that repaint() and paintComponent() are different methods? Regions are marked dirty by one if repaint() methods. Can you override repaint methods in following way:
public void repaint(<arg list>){
super.repaint(<args>);
new Exception("repaint("+<comm
}
(for all variants of repaint) and see where repaint is called from? It will help us to understand why some region is marked dirty.
Regards,
Igor Bazarny

Get a FREE t-shirt when you ask your first question.
We believe in human intelligence. Our moderation policy strictly prohibits the use of LLM content in our Q&A threads.
This old question needs to be finalized -- accept an answer, split points, or get a refund. For information on your options, please click here-> http:/help/closing.jsp#1
EXPERTS:
Post your closing recommendations! No comment means you don't care.
I will leave a recommendation in the Cleanup topic area that this question is:
- Split points between bazarny and objects
Please leave any comments here within the next seven days.
PLEASE DO NOT ACCEPT THIS COMMENT AS AN ANSWER!
Venabili
EE Cleanup Volunteer
Java
--
Questions
--
Followers
Top Experts
Java is a platform-independent, object-oriented programming language and run-time environment, designed to have as few implementation dependencies as possible such that developers can write one set of code across all platforms using libraries. Most devices will not run Java natively, and require a run-time component to be installed in order to execute a Java program.