Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 3982460
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 20, 20262026-05-20T05:31:49+00:00 2026-05-20T05:31:49+00:00

I’m working on a Sudoku puzzle generator and am running into some intermittent swing

  • 0

I’m working on a Sudoku puzzle generator and am running into some intermittent swing exceptions after/during a call to the RemoveAll() method of a JPanel. When I run in eclipse’s debug mode, the exceptions do not appear. Here is the code for the class in question:

import java.awt.FlowLayout;
import java.awt.GridLayout;

import javax.swing.JLabel;
import javax.swing.JPanel;

/**
 * Represents a cell in the GUI Grid display
 * @author alex
 *
 */
public class CellGUI extends JPanel {



    public CellGUI()
    {
        super();

        this.setLayout(new GridLayout(3,3));

        for(int i = 1;i <=9;i++)
        {
            add(new JLabel("" + i));
        }

        setVisible(true);
    }

    public void clear()
    {
        this.removeAll();
        this.validate();
        this.setLayout(new GridLayout(3,3));

        for(int i = 1;i <= 9; i++)
        {
            add(new JLabel("" + i));
        }       

    }

    public void setValue(int newVal)
    {
        if (newVal == 0)
        {
            clear();
        }
        else
        {

            this.removeAll(); // this line appears to be the problem 

            //this.updateUI();
            //this.setLayout(new FlowLayout());

            //add(new   JLabel("" + newVal));
        }
    }
}

It usually spits out this exception:

Exception in thread "AWT-EventQueue-0" java.lang.ClassCastException
    at javax.swing.LayoutComparator.compare(Unknown Source)
    at java.util.Arrays.mergeSort(Unknown Source)
    at java.util.Arrays.mergeSort(Unknown Source)
    at java.util.Arrays.mergeSort(Unknown Source)
    at java.util.Arrays.mergeSort(Unknown Source)
    at java.util.Arrays.mergeSort(Unknown Source)
    at java.util.Arrays.mergeSort(Unknown Source)
    at java.util.Arrays.mergeSort(Unknown Source)
    at java.util.Arrays.sort(Unknown Source)
    at java.util.Collections.sort(Unknown Source)
    at javax.swing.SortingFocusTraversalPolicy.enumerateAndSortCycle(Unknown Source)
    at javax.swing.SortingFocusTraversalPolicy.getFirstComponent(Unknown Source)
    at javax.swing.LayoutFocusTraversalPolicy.getFirstComponent(Unknown Source)
    at javax.swing.SortingFocusTraversalPolicy.getDefaultComponent(Unknown Source)
    at java.awt.FocusTraversalPolicy.getInitialComponent(Unknown Source)
    at java.awt.DefaultKeyboardFocusManager.dispatchEvent(Unknown Source)
    at java.awt.Component.dispatchEventImpl(Unknown Source)
    at java.awt.Container.dispatchEventImpl(Unknown Source)
    at java.awt.Window.dispatchEventImpl(Unknown Source)
    at java.awt.Component.dispatchEvent(Unknown Source)
    at java.awt.EventQueue.dispatchEventImpl(Unknown Source)
    at java.awt.EventQueue.access$000(Unknown Source)
    at java.awt.EventQueue$1.run(Unknown Source)
    at java.awt.EventQueue$1.run(Unknown Source)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.security.AccessControlContext$1.doIntersectionPrivilege(Unknown Source)
    at java.security.AccessControlContext$1.doIntersectionPrivilege(Unknown Source)
    at java.awt.EventQueue$2.run(Unknown Source)
    at java.awt.EventQueue$2.run(Unknown Source)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.security.AccessControlContext$1.doIntersectionPrivilege(Unknown Source)
    at java.awt.EventQueue.dispatchEvent(Unknown Source)
    at java.awt.SequencedEvent.dispatch(Unknown Source)
    at java.awt.EventQueue.dispatchEventImpl(Unknown Source)
    at java.awt.EventQueue.access$000(Unknown Source)
    at java.awt.EventQueue$1.run(Unknown Source)
    at java.awt.EventQueue$1.run(Unknown Source)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.security.AccessControlContext$1.doIntersectionPrivilege(Unknown Source)
    at java.security.AccessControlContext$1.doIntersectionPrivilege(Unknown Source)
    at java.awt.EventQueue$2.run(Unknown Source)
    at java.awt.EventQueue$2.run(Unknown Source)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.security.AccessControlContext$1.doIntersectionPrivilege(Unknown Source)
    at java.awt.EventQueue.dispatchEvent(Unknown Source)
    at java.awt.EventDispatchThread.pumpOneEventForFilters(Unknown Source)
    at java.awt.EventDispatchThread.pumpEventsForFilter(Unknown Source)
    at java.awt.EventDispatchThread.pumpEventsForHierarchy(Unknown Source)
    at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
    at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
    at java.awt.EventDispatchThread.run(Unknown Source)

I figure in my naivety I’m probably going about doing this the wrong way. Any ideas?

Edit: By request, I’m posting additional code.

This class calls the setValue method of the CellGUI:

public class GridGUI extends JPanel {

    ArrayList<CellGUI> cells = new ArrayList<CellGUI>();

    public GridGUI()
    {

        this.setLayout(new GridLayout(9,9));


        for(int i = 0; i < 81;i++)
        {
            CellGUI cell = new CellGUI();
            cells.add(cell);
            add(cell);
        }

    }


    public void updateGrid(Grid g)
    {
        for(int i = 0;i<81;i++)
        {
            cells.get(i).setValue(g.getValue(i));
        }
    }
}

The GridGUI class is created and managed by a class that extends JFrame:

public class GUI extends JFrame {   
...
public GUI() 
    {
        this.setLayout(new BorderLayout());
        add(gridGUI,BorderLayout.CENTER);

        JPanel panel = new JPanel();
        panel.setLayout(new FlowLayout());
        panel.add(isValidLabel);
        panel.add(isSolvableLabel);
        panel.setVisible(true);

        add(panel, BorderLayout.SOUTH);

        this.setSize(600, 600);

        setVisible(true);       

    }
    public void loadGrid(String path)
    {
        grid = new Grid(path);
        grid.print();

        gridGUI.updateGrid(grid);

        updateLabels();
    }
...
}

Which is called by my main class:

public static void main(String[] args) 
{       
    GUI gui = new GUI();
    gui.loadGrid(args[0]);
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-20T05:31:50+00:00Added an answer on May 20, 2026 at 5:31 am

    So, looking at the stack-trace shows that the error occurs when the FocusManager is trying to get the first component. For this, your FocusTraversalPolicy tries to order the components in “layout order”, which means roughly in columns and rows. For a GridLayout, this should be totally trivial. Let’s look at the code of LayoutComparator. It throws a ClassCastException in some places:

            if (a == null) {
                // 'a' is not part of a Window hierarchy. Can't cope.
                throw new ClassCastException();
            }
    

    a is either the containing Window here, or null if no such Window exists.

    So, looks like your components are not yet totally registered in the Component tree. As the others said, this can occur if you do some changes in the GUI when not being in the AWT event dispatch thread.

    To avoid this, wrap all changes to the GUI (including creating the components, like your constructor call above) in a call of EventQueue.invokeLater(...). (Sometimes invokeAndWait is more useful, and you also can use the same-named methods in SwingUtilities instead.) In your case, the change is quite simple:

    public static void main(String[] args) 
    {
        EventQueue.invokeLater(new Runnable() { public void run() {
            GUI gui = new GUI();
            gui.loadGrid(args[0]);
        }});
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

link Im having trouble converting the html entites into html characters, (&# 8217;) i
For some reason, after submitting a string like this Jack’s Spindle from a text
I am currently running into a problem where an element is coming back from
I have a French site that I want to parse, but am running into
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I have just tried to save a simple *.rtf file with some websites and
this is what i have right now Drawing an RSS feed into the php,
I'm using v2.0 of ClassTextile.php, with the following call: $testimonial_text = $textile->TextileRestricted($_POST['testimonial']); ... and
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
I'm trying to convert HTML to plain text. I get many &\#8217; &\#8220; etc.

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.