0

When i run my swing application, sometimes at the beginning i got following exception:

Exception in thread "AWT-EventQueue-0" java.lang.ClassCastException
at javax.swing.LayoutComparator.compare(Unknown Source)
at javax.swing.LayoutComparator.compare(Unknown Source)
at java.util.TimSort.countRunAndMakeAscending(Unknown Source)
at java.util.TimSort.sort(Unknown Source)
at java.util.Arrays.sort(Unknown Source)
at java.util.ArrayList.sort(Unknown Source)
at java.util.Collections.sort(Unknown Source)
at javax.swing.SortingFocusTraversalPolicy.enumerateAndSortCycle(Unknown Source)
at javax.swing.SortingFocusTraversalPolicy.getFocusTraversalCycle(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$500(Unknown Source)
at java.awt.EventQueue$3.run(Unknown Source)
at java.awt.EventQueue$3.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(Unknown Source)
at java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(Unknown Source)
at java.awt.EventQueue$4.run(Unknown Source)
at java.awt.EventQueue$4.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$JavaSecurityAccessImpl.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$500(Unknown Source)
at java.awt.EventQueue$3.run(Unknown Source)
at java.awt.EventQueue$3.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(Unknown Source)
at java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(Unknown Source)
at java.awt.EventQueue$4.run(Unknown Source)
at java.awt.EventQueue$4.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$JavaSecurityAccessImpl.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 have found solution to this, but i am not sure if this completely solves the problem. If i change:

public class MainFrame extends JFrame {
    ...
}
public static void main(String[] args){
    new MainFrame();
}

to:

public static void main(String[] args){
    SwingUtilities.invokeLater(new Runnable() {
        public void run() {
            new MainFrame();
        }
    });
}

can i be sure this eliminates above exception from occuring completely, and that creating form outside The Event Dispatch Thread was the only reason causing error?

Here is simplified code for my application:

public class App {

    //MyFrame can be show independently or in a TabbedPane
    public static class MyFrame extends JFrame {
        public MyFrame() {
            setLayout(new BoxLayout(getContentPane(), BoxLayout.Y_AXIS));

            for (int i = 0; i < 8; i++) {
                JPanel panel = new JPanel();
                panel.setBorder(BorderFactory.createTitledBorder("Panel " + i));
                panel.add(new JLabel("label " + i));
                add(panel);
            }

            pack();
            setVisible(true);
        }
    }

    public static class MainTabsFrame extends JFrame {
        public MainTabsFrame() {
            JTabbedPane tabsPane = new JTabbedPane();

            JFrame frame = new MyFrame();
            tabsPane.addTab("My Frame 1", frame.getContentPane());
            frame.setVisible(false);

            add(tabsPane);
            pack();
            setLocationRelativeTo(null);
            setDefaultCloseOperation(EXIT_ON_CLOSE);

            setVisible(true);
        }
    }

    public static void main(String[] args) {
        new MainTabsFrame();
    }
}
5
  • Please provide a SSCCE so we can help you. Commented Jun 15, 2016 at 10:25
  • can you clarify what you mean by, "sometimes at the beginning"? Commented Jun 15, 2016 at 12:33
  • Errors occur while starting the application. I can't say when exactly because it is thrown in swing EDT, moreover not all runs produce error. Commented Jun 15, 2016 at 12:55
  • Does the error occur when you only use the MainTabsFrame in the main method or does it happen for both? Commented Jun 15, 2016 at 19:39
  • Error occures when I use in main method MainTabsFrame alone. Commented Jun 16, 2016 at 8:21

1 Answer 1

2

It is necessary one more line of code.

This is the final solution:

public static void main(String[] args){
    SwingUtilities.invokeLater(new Runnable() {
        public void run() {
            JFrame _mf= new MainFrame();
            _mf.setVisible(true);
        }
    });
}

Please remember that every kind of operation that involves a Swing object has to be performed by the Event Dispatch Thread, so also the creation and visualization of the first JFrame.

Sign up to request clarification or add additional context in comments.

2 Comments

If i check status of swing object for example 'myJFrame.isVisible()' i should also do it from EDT, am i right?
It should be better, but, from another side, you perform a read-only operation, so it is not so necessary.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.