Programming C, C++, Java, PHP, Ruby, Turing, VB
Computer Science Canada 
Programming C, C++, Java, PHP, Ruby, Turing, VB  

Username:   Password: 
 RegisterRegister   
 JFrame.setVisible(true) is causing an error?
Index -> Programming, Java -> Java Help
View previous topic Printable versionDownload TopicSubscribe to this topicPrivate MessagesRefresh page View next topic
Author Message
Benner




PostPosted: Fri Apr 19, 2013 10:16 pm   Post subject: JFrame.setVisible(true) is causing an error?

Hi, I finally pinpointed where my error was coming from in my code. I'm not sure if this error is causing any problems in my program as it's not complete yet but It would be nice to figure out why this is happening.

the code I have is:
code:

pickP.setLayout(new GridLayout(height, width));
pickF.setContentPane(pickP);

pickF.pack();
pickF.setVisible(true);             //when i remove this line, the error goes away.
pickF.setResizable(false);
pickF.setLocationRelativeTo(null);

The error I get is:

Exception in thread "AWT-EventQueue-0" java.lang.IllegalArgumentException: Comparison method violates its general contract!
at java.util.TimSort.mergeLo(TimSort.java:747)
at java.util.TimSort.mergeAt(TimSort.java:483)
at java.util.TimSort.mergeCollapse(TimSort.java:410)
at java.util.TimSort.sort(TimSort.java:214)
...this goes on for a hundred or so lines.

No where in the (stack?) is there anything to do with code in my project. TimSort is part of the java.util package.

Any help into why this happens would greatly be appreciated. Thanks

Full Code: (line that gives the error is at the bottom)
code:

package chap9ex14new;
import java.awt.*;
import javax.swing.*;
import java.awt.event.*;

public class Chap9ex14new implements ActionListener{
    JFrame pickF, runF, menuF;
    JPanel pickP, runP, menuP;
    JButton start, stop;
    JCheckBox[][] init;
    JLabel[][] cell;
    boolean[][] cellB;
   
    ImageIcon ON;
    ImageIcon OFF;
   
    String path;
    int height, width;
   
    public Chap9ex14new(){
        path = Chap9ex14new.class.getProtectionDomain().getCodeSource().getLocation().getPath();
        OFF = new ImageIcon(path+"/OFF.jpg");
        ON = new ImageIcon(path+"/ON.png");
   
        height = 70;
        width = 115;
       
        pickF = new JFrame("The Game of Life");
                pickF.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        runF = new JFrame("The Game of Life");
                runF.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
       
        pickP = new JPanel();
        pickP.setSize(width, height);
        runP = new JPanel();
        runP.setSize(width, height);
       
         start = new JButton("Start");
         start.setActionCommand("start");
         start.addActionListener(this);
         
         stop = new JButton("Stop");
         stop.setActionCommand("stop");
         stop.addActionListener(this);
         
         cell = new JLabel[height][width];      //Label Cells
            for (int i = 0; i <height; i++){       
                 for (int s = 0; s < width; s++){
                  cell[i][s] = new JLabel("hi");
                  runP.add(cell[i][s]);
                 }
             }
           
         cellB = new boolean[height][width];
            for (int i = 0; i <height; i++){        //Boolean Cells
                 for (int s = 0; s < width; s++){
                  cellB[i][s] = false;
                }
            }
           
         init = new JCheckBox[height][width];
            for (int i = 0; i < height ;i++){         //CheckBoxes
                 for (int s = 0; s < width; s++){
                 init[i][s] = new JCheckBox(OFF);
                 init[i][s].setSelectedIcon(ON);
                 
                 String a = Integer.toString(i);
                 String b = Integer.toString(s);
                 init[i][s].setActionCommand("b"+a+b);
                 
                 pickP.add(init[i][s]);
                 
                 System.out.println(init[i][s].getPreferredSize());
            }
        }
           
        runP.setLayout(new GridLayout(height, width));
        runF.setContentPane(runP);
       
        runF.pack();
        runF.setVisible(true);
        runF.setResizable(false);
        runF.setLocationRelativeTo(null);
       
        pickP.setLayout(new GridLayout(height, width));
        pickF.setContentPane(pickP);
   
        pickF.pack();
        pickF.setVisible(true);                                //this line causes the error. when I remove "pickF.setVisible(true);"
        pickF.setResizable(false);                                                                     //the the error goes away   
        pickF.setLocationRelativeTo(null);
       
           
    }
   
    @Override
    public void actionPerformed(ActionEvent event){
       
    }
   
    private static void runGUI(){
         Chap9ex14new run = new Chap9ex14new();
    }
    public static void main(String[] args) {
       javax.swing.SwingUtilities.invokeLater(new Runnable(){
            @Override
            public void run(){
                runGUI();
            }
        });
    }
}

Sponsor
Sponsor
Sponsor
sponsor
DemonWasp




PostPosted: Fri Apr 19, 2013 10:40 pm   Post subject: RE:JFrame.setVisible(true) is causing an error?

In the future, when you have a problem in Java, always post the stack trace in code blocks:

code:

Exception in thread "AWT-EventQueue-0" java.lang.IllegalArgumentException: Comparison method violates its general contract!
        at java.util.TimSort.mergeLo(Unknown Source)
        at java.util.TimSort.mergeAt(Unknown Source)
        at java.util.TimSort.mergeCollapse(Unknown Source)
        at java.util.TimSort.sort(Unknown Source)
        at java.util.TimSort.sort(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.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$200(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$1.doIntersectionPrivilege(Unknown Source)
        at java.security.ProtectionDomain$1.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$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$200(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$1.doIntersectionPrivilege(Unknown Source)
        at java.security.ProtectionDomain$1.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$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'm not a Swing/AWT expert, but it looks like you hit some bug that's caused by having WAY WAY WAY too many GUI elements. You're trying to add 70 * 115 = 8050 elements to EACH of three spots, which is just way too many (and, clearly, more than SUN/Oracle ever tested for).

If you reduce "width" and "height" to values like 5 and 7, then the error vanishes and your program works (or, at least, it looks like it does...I can't tell because I don't know what it's supposed to do).

The reason that removing setVisible(true) causes the error to disappear is because the error occurs when Swing/AWT tries to draw your panel, which doesn't happen if you never show it.

As for how to get 8000+ "cells" to appear, try using the advice I gave you in your other thread: http://compsci.ca/v3/viewtopic.php?t=33591
Benner




PostPosted: Fri Apr 19, 2013 10:48 pm   Post subject: Re: JFrame.setVisible(true) is causing an error?

You're right, the problem was having that many elements

With your suggestion by drawing the cells on the jpanel, would I be able to change the colour of each cell (like what is done in http://youtu.be/XcuBvj0pw-E?t=1m5s)? If so, don't worry about explaining more about it to me, I'll do research on it. Thanks again
DemonWasp




PostPosted: Fri Apr 19, 2013 11:05 pm   Post subject: RE:JFrame.setVisible(true) is causing an error?

Yes, you can. You could even draw images or more complicated shapes than just rectangles. The Oracle tutorial on how to use the Graphics and Graphics2D objects aren't bad: http://docs.oracle.com/javase/tutorial/2d/
Benner




PostPosted: Fri Apr 19, 2013 11:17 pm   Post subject: Re: JFrame.setVisible(true) is causing an error?

Thank you!
Display posts from previous:   
   Index -> Programming, Java -> Java Help
View previous topic Tell A FriendPrintable versionDownload TopicSubscribe to this topicPrivate MessagesRefresh page View next topic

Page 1 of 1  [ 5 Posts ]
Jump to:   


Style:  
Search: