Java repaint does not render component correctly

im developing a GUI application using JSWing, load the XML file, then deserialize it, then add the whole created object to the JPanel. However, until I go to the window or click on the panel, it looks like this:

enter image description here

After moving the window, they look correct, so how to fix this problem <

I have looked at this link and this might be the answer since in the JComponent constructor I am using



but I'm still not sure how to fix the problem as this part of the documentation is very hard to understand (it somehow just doesn't make any sense to me: -D)

By the way, the picture itself looks something like this:

for (NetObject o : objects) {



and addNewObject (not all code)

public void addNewObject(NetObject o) {

         DraggableComponent object = new DraggableComponent(o, editorIndex); //Creates a draggableComponent
        this.add(object);//Adds this component to main container

        object.setOverbearing(true); //On click ,this panel gains lowest z-buffer

            object.setSize(48, 48);
            object.setLocation(o.x - 23, o.y - 23);
            this.repaint(); //this = JPanel



and the overridden paint code

    protected void paintComponent(Graphics g) {
        if (isOpaque()) {
            if (object.type == 2) { //tarnsition
                g.drawImage(transition, 0, 0, this);

            } else if (object.type == 1) {
                boolean test = g.drawImage(place, 0, 0, this); //place
                g.drawString(object.loadTokens(), 3, 27); // 1,2,3,4...



I tried to call this.revalidate - after FOR EACH LOOP - didn't help, the only way that works is to navigate with the window somehow, oddly enough, this problem only exists @Windows, my college is developing this exactly the same application on Linux and it doesn't run into graphical problems.

I know there have been so many topics like this, but I honestly couldn't figure out the solution.

Thanks for the answer, Osiris


source to share

1 answer


The API mentions that "it depends on the look and feel of this property, some may ignore it." Set the graphics context color explicitly to paintComponent()

and call fillRect()




All Articles