Reputation: 841
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();
}
}
Upvotes: 0
Views: 6501
Reputation: 148
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.
Upvotes: 2