# Off-Topic Discussion > The Lounge > Tech Talk >  >  Java Swing, GUI locks up when painting component

## Rakjavik

So I'm working on a crappy little 2d RPG in java to try to increase my knowledge. Ran into a problem with Swing. I've got a JFrame with 2 JScrollPanes, each JScrollPane has a custom JPanel attached for custom painting. Whenever the paint method gets called now, the GUI freezes. I've tried doing step by step debugging and still can't find the issue.

I'll try to keep the pasted code as little as possible. If anyone has any ideas, or needs me to post more of my classes in the project let me know!


// The JFrame and constructor //
public class EditMaps extends javax.swing.JFrame {

    public static Board map;
    private MapJPanel mapPanel;
    private PalletteJPanel palletteJPanel;
    public static int selectedPallette = -1;
    public static int selectedTile = 0;

    public EditMaps() throws IOException, ClassNotFoundException
    {
        initComponents();
        this.setResizable(false);
        this.setLayout(null);
        map = new Board();


        mapPanel = new MapJPanel();
        mapPanel.setPreferredSize(new Dimension(1000, 1000));
        mapPanel.setBorder(BorderFactory.createLineBorder(  Color.red));
        mapScrollPane.getViewport().add(mapPanel);
        mapScrollPane.setViewportView(mapPanel);
        palletteJPanel = new PalletteJPanel();
        palletteScrollPanel.getViewport().add(palletteJPan  el);
        palletteScrollPanel.setViewportView(palletteJPanel  );








        tileSetDropDown.addItem("Choose Tileset");
        for (String item : MapTool.getTileSetNames())
        {
            tileSetDropDown.addItem(item);
        }

    }

//Method that causes freezing due to calling the revalidate methods on the panels//

private void tileSetDropDownActionPerformed(java.awt.event.Acti  onEvent evt) {                                                
        if (tileSetDropDown.getSelectedIndex() > 0)
        {
            try
            {
                map.setTileSet(MapTool.getTileSet(tileSetDropDown.  getSelectedIndex() - 1));
                int height = 35*(map.getTileSet().getTiles().size()/5)+10;
                palletteJPanel.setPreferredSize(new Dimension(palletteScrollPanel.getWidth(), height));
                palletteScrollPanel.setPreferredSize(new Dimension(palletteScrollPanel.getWidth(), height));
                palletteScrollPanel.revalidate();
                palletteJPanel.revalidate();

            }
            catch (IOException ex)
            {

            }
            catch (ClassNotFoundException ex)
            {

            }
        }
    }        

//The custom JPanel being updated in the previous method //
/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package rpg.maps;

import java.awt.Color;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.util.ArrayList;
import javax.swing.JPanel;


/**
 *
 * @author 501282234
 */
public class PalletteJPanel extends JPanel
{

    public PalletteJPanel()
    {
        setSize(220, 530);
        setPreferredSize(new Dimension(220, 530));

    }

    @Override
    public void paintComponent(Graphics g)
    {

        Graphics2D g2d = (Graphics2D) g;
        super.paintComponent(g2d);



        try
        {
            if (EditMaps.map.getTileSet().getTiles().get(0).getIm  age() != null)
            {
                ArrayList<Tile> tiles = EditMaps.map.getTileSet().getTiles();
                int y = 10;
                int x = 16;
                int count=0;

                for (count = 0; count < tiles.size() :wink2: 
                {
                    if (count % 5 == 4)
                    {
                        y+=35;
                        x=16;
                    }
                    tiles.get(count).paint(x, y, 32, g2d);
                    x+=35;
                }
                for (Tile tile : tiles)
                {


                    if (count % 5 == 4)
                    {
                        y+=35;
                        x=16;
                    }
                    tile.paint(x, y, 32, g2d);
                    x+=35;
                    count++;
                }
            }
            if (EditMaps.selectedPallette > -1)
            {


                int x = (EditMaps.selectedPallette % 5);
                int y = ((EditMaps.selectedPallette-x) / 5);


                x = x*35+16;
                y = y*35+10;

                g2d.setColor(Color.red);
                g2d.drawRect(x-1, y-1, 33, 33);
            }
        }
        catch(NullPointerException ex)
        {
            System.out.println("Null pointer when scanning tiles");
        }
    }

}

----------


## reci

Do you think it is a problem with your code or with Swing?

Do you use an IDE? (as they can help with errors)

----------


## Rakjavik

I use Netbeans as an IDE. I honestly don't think it's my code, but I may be wrong, hence my posting it on here.

----------


## reci

When you say the GUI freezes, do you mean that the JFrame stops responding to any input, or does the java process (javaw.exe or something?) stop responding?  Or both?

----------


## Rakjavik

JFrame stop responding. Infinite loop. One of my for loops wasn't incrementing the counting int. Ugg. Thanks for the replies though  :smiley:

----------

