Results 1 to 7 of 7
  1. #1
    Indegon is offline Member
    Join Date
    Nov 2010
    Location
    My own world
    Posts
    15
    Rep Power
    0

    Default Text Box Lock Up

    I'm having a small problem while building a JFrame. I'm building a pokedex for a class assignment and I've finally got the frame working only, whenever I try to enter ANYTHING into the search tab, the JVM just completely locks up. I don't know why it's doing this, I've tried moving it to the main frame and taking it out of the panels but nothing seems to work. Is there something I'm missing here? If someone could take a look at my code and tell me what I'm doing wrong it would be much appreciated.

    Java Code:
    package projectb;
    import javax.swing.*;
    import java.util.Scanner;
    import java.util.Arrays;
    import java.awt.*;
    import java.awt.event.*;
    /********************************************************
     ********************************************************
     **  Pokedex V1.0 Pokemon 1 - 151 original.            **
     **  Completed on: 11/02/2010                          **
     **  Info: This program acts as a rudimentary pokedex  **
     **  including basic search and sort functions, more   **
     **  be added later.                                   **
     ********************************************************
     *******************************************************/
    public class Main {
    
        
        public static class mainMenu extends JFrame
            {
                JPanel frame = new JPanel();
                    ImageIcon myImg = new ImageIcon("image.JPG");
                        JLabel imgImage = new JLabel(myImg);
                    JPanel main = new JPanel();
                        JButton sbName = new JButton("Search By Name");
                        JButton sbNum = new JButton("Search By Number");
                        JButton soName = new JButton("Sort By Name");
                        JButton soNum = new JButton("Sort By Num");
                    JPanel search = new JPanel();
                        JPanel srcPnl = new JPanel();
    
    // this is the field giving me the error
                            JTextField fldSearch = new JTextField(25);
    
                            JButton btnSearch = new JButton("Search");
                        JPanel srcNum = new JPanel();
                            JLabel lblNum = new JLabel("Number: ");
                            JTextField fldNum = new JTextField(3);
                        JPanel srcName = new JPanel();
                            JLabel lblName = new JLabel("Name: ");
                            JTextField fldName = new JTextField(20);
                        JPanel srcInfo = new JPanel();
                            JLabel lblInfo = new JLabel("Info: ");
                            JTextArea txtInfo = new JTextArea(3,30);
                public mainMenu()
                {
                    frame.setLayout(new BorderLayout(5,5));
                          frame.add(imgImage,BorderLayout.CENTER);
                                imgImage.setVisible(true);
                          frame.add(main,BorderLayout.NORTH);
                                main.setLayout(new GridLayout(2,2,5,5));
                                main.setVisible(true);
                                     main.add(sbName);
                                          sbName.addActionListener(new ActionListener()
                                          {
                                              public void actionPerformed(ActionEvent e)
                                              {
                                                  main.setVisible(false);
                                                  search.setVisible(true);
                                                  imgImage.setVisible(true);
                                              }
                                          });
                                     main.add(sbNum);
                                     main.add(soName);
                                     main.add(soNum);
                          frame.add(search,BorderLayout.SOUTH);
                                search.setLayout(new GridLayout(4,1,0,0));
                                search.setVisible(false);
                                       search.add(srcPnl);
                                       srcPnl.setLayout(new FlowLayout(FlowLayout.LEFT,5,5));
                                              srcPnl.add(fldSearch);
                                                     fldSearch.setEditable(true);
                                              srcPnl.add(btnSearch);
                                       search.add(srcNum);
                                       srcNum.setLayout(new FlowLayout(FlowLayout.LEFT,5,5));
                                              srcNum.add(lblNum);
                                              srcNum.add(fldNum);
                                                     fldNum.setEditable(false);
                                       search.add(srcName);
                                       srcName.setLayout(new FlowLayout(FlowLayout.LEFT,5,5));
                                              srcName.add(lblName);
                                              srcName.add(fldName);
                                                      fldName.setEditable(false);
                                       search.add(srcInfo);
                                       srcInfo.setLayout(new FlowLayout(FlowLayout.LEFT,5,5));
                                              srcInfo.add(lblInfo);
                                              srcInfo.add(txtInfo);
                                                      txtInfo.setEditable(false);
                      add(frame);
                }            
            }
        public static void main(String[] args) throws Exception
        {
            // Array data types declared here
            String[][] dexData = new String[152][3];
            String[] sortName = new String[151];
            String[][] dexData2 = new String[152][3];
            
            
            // Primitive data types declared here.
            int myChoice = 0;
            int searchNum = 0;
            boolean mainLoop = false;
            String myStop = "";
            String myName = ""; // This is used to search pokedex by name
            String myNumber = ""; // This is used to search pokedex by number.
            String compareName = ""; // This is used to compare sortName to dexData
            int countNum = 0; // this is used to keep track of index number
            String terminator = "";
            
            // counter variables for loops declared here
            int counter = 0;
            int counter1 = 0;
            int counter2 = 0;
            int counter3 = 0;
            int counter4 = 0;
            int counter5 = 0;
            int counter6 = 0;
            int counter7 = 0;
           
            
            // File instances created here. 
            java.io.File pNum = new java.io.File("pokeNum.txt");
            java.io.File pName = new java.io.File("pokeName.txt");
            java.io.File pInfo = new java.io.File("pokeInfo.txt");
            
            // scanner instances created here
            Scanner nums = new Scanner(pNum);
            Scanner names = new Scanner(pName);
            Scanner infos = new Scanner(pInfo);
            Scanner menuChoice = new Scanner(System.in);
     
            /* Need to add in user prompt for files here eventually */
            
            // dexData array filled here using for loop
            while(counter<152)
            {
                
                dexData[counter][0]=nums.nextLine();
                dexData[counter][1]=names.nextLine();
                dexData[counter][2]=infos.nextLine();
                counter++;
            }
            
            
            
            JFrame pokedex = new mainMenu();
            pokedex.setTitle("Pokedex");
            pokedex.setLocation(325,100);
            pokedex.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            pokedex.setSize(400,600);
            pokedex.setVisible(true);
            
            // ***********************************
            // The main program block starts here!
            // ***********************************
            
            
            do 
            {
                welcomeText();
                myChoice = menuChoice.nextInt();
                
                switch (myChoice)
                {
                    case 1: myName = searchName();
                            for(counter=0;counter<152;counter++)
                            {
                                if(dexData[counter][1].equals(myName))
                                {
                                    System.out.print(dexData[counter][0]+" ");
                                    System.out.print(dexData[counter][1]+" ");
                                    System.out.print("- ");
                                    System.out.println(dexData[counter][2]);
                                    break;
                                }
                                else if(counter==151)
                                {
                                    System.out.print(dexData[counter][0]+" ");
                                    System.out.print(dexData[counter][1]+" ");
                                    System.out.println(dexData[counter][2]);
                                    break;
                                }
                            }
                            System.out.print("Press 1 key & hit [enter] to continue.");
                            myStop = menuChoice.next();
                            break;
                    case 2: myNumber = searchNum();
                            for(counter1=0;counter1<152;counter1++)
                            {
                                if(dexData[counter1][0].equals(myNumber))
                                {
                                    System.out.print(dexData[counter1][0]+" ");
                                    System.out.print(dexData[counter1][1]+" ");
                                    System.out.print("- ");
                                            
                                    System.out.println(dexData[counter1][2]);
                                    break;
                                }
                                else if(counter==151)
                                {
                                    System.out.print(dexData[counter1][0]+" ");
                                    System.out.print(dexData[counter1][1]+" ");
                                    System.out.println(dexData[counter1][2]);
                                    break;
                                }
                            }
                            System.out.print("Press 1 key & hit [enter] to continue.");
                            myStop = menuChoice.next();
                            break;
                    case 3: for(counter2=0;counter2<151;counter2++)
                            {
                                sortName[counter2]=dexData[counter2][1];
                            }
                            Arrays.sort(sortName);
                            // Everything is ok up to this point!
                            
                            for(counter3=0;counter3<151;counter3++)
                            {
                                compareName = sortName[counter3];
                                countNum = counter3;
                                for(counter4=0;counter4<151;counter4++)
                                {
                                    if(dexData[counter4][1].equals(compareName))
                                    {
                                        dexData2[countNum][0]=dexData[counter4][0];
                                        dexData2[countNum][1]=dexData[counter4][1];
                                        dexData2[countNum][2]=dexData[counter4][2];
                                        break;
                                    }                                
                                }
                            }
                            dexData2[151][0]=dexData[151][0];
                            dexData2[151][1]=dexData[151][1];
                            dexData2[151][2]=dexData[151][2];
                            for(counter5=0;counter5<151;counter5++)
                            {
                                  System.out.print(dexData2[counter5][0]+" ");
                                  System.out.print(dexData2[counter5][1]);
                                  System.out.print("- ");
                                  System.out.println(dexData2[counter5][2]);    
                            }
                            System.out.print("Press 1 key & hit [enter] to continue.");
                            myStop = menuChoice.next();
                            break;
                    case 4: for(counter6=0;counter6<152;counter6++)
                            {
                                dexData2[counter6][0]=dexData[counter6][0];
                                dexData2[counter6][1]=dexData[counter6][1];
                                dexData2[counter6][2]=dexData[counter6][2];
                            }
                            for(counter7=0;counter7<151;counter7++)
                            {
                                System.out.print(dexData2[counter7][0]+" ");
                                System.out.print(dexData2[counter7][1]);
                                System.out.print("- ");
                                System.out.println(dexData2[counter7][2]);
                            }
                             System.out.print("Press 1 key & hit [enter] to continue.");
                            myStop = menuChoice.next();
                            break;
                    case 5: mainLoop=true;
                            break;
                    default: System.out.print("Please choose an option from the"); 
                             System.out.print(" menu above using the number keys "); 
                             System.out.println("on your keyboard!");
                             break;
                                     
                }
                System.out.println("");
            } while (mainLoop==false); 
        }
        
        public static void welcomeText()
        {
            System.out.println("*************************************");
            System.out.println("*************************************");
            System.out.println("** Pokedex V1.0 Original 1-151     **");
            System.out.println("** Programmed By: Joseph Bourque   **");
            System.out.println("** Completed On: 11/02/2010        **");
            System.out.println("** Last Update On: --/--/--        **"); 
            System.out.println("*************************************");
            System.out.println("*************************************");
            System.out.println("");
            System.out.println("1. Search By Name");
            System.out.println("2. Search By Number");
            System.out.println("3. Sort By Name");
            System.out.println("4. Sort By Number");
            System.out.println("5. Exit Program");
            System.out.println("");
            System.out.print("Choose an Option: ");
        }
        
        public static String searchName()
        {
            Scanner pokeSearch = new Scanner(System.in);
            String pokeName = "";
            System.out.print("Please enter the name of a pokemon: ");
            pokeName = pokeSearch.next().toUpperCase();
            
            return pokeName;
        }
        
        public static String searchNum()
        {
            Scanner numSearch = new Scanner(System.in);
            String pokeNum = "";
            System.out.print("Please enter the number of the pokemon you ");
            System.out.print("wish to search for: ");
            pokeNum = numSearch.next();
            
            return pokeNum;
        }
    }
    Last edited by Indegon; 11-12-2010 at 12:54 AM. Reason: Forgot code tags

  2. #2
    Singing Boyo is offline Senior Member
    Join Date
    Mar 2009
    Posts
    552
    Rep Power
    6

    Default

    Please place code tags ([code] and [/code]) around your code to make it easier to read. If you can edit your post to add it, we can probably help you more. As it is, I'd guess that something is blocking on a read somewhere.
    If the above doesn't make sense to you, ignore it, but remember it - might be useful!
    And if you just randomly taught yourself to program, well... you're just like me!

  3. #3
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,315
    Blog Entries
    1
    Rep Power
    26

  4. #4
    Indegon is offline Member
    Join Date
    Nov 2010
    Location
    My own world
    Posts
    15
    Rep Power
    0

    Default

    It's not entirely done, I was trying to get the GUI hashed out before trying to convert the console code, it's eventually going to be a full GUI program. But I reached a snag when I tried to enter text in my text field and the entire thing went down :( Is this because of the console code do you think?

  5. #5
    Indegon is offline Member
    Join Date
    Nov 2010
    Location
    My own world
    Posts
    15
    Rep Power
    0

    Default

    OOP I got it working! It was the console code blocking it :O i saved the console code to a text file, I had NO idea that console code could lock GUI components like that. Thnx for all your help guys I couldn't have solved this one w/o you :)

  6. #6
    Singing Boyo is offline Senior Member
    Join Date
    Mar 2009
    Posts
    552
    Rep Power
    6

    Default

    Console code will lock GUI code down if it waits for input. It can be very hard to trace when that happens. So, always convert everything to GUI at once, or just go with a GUI from the beginning.

    In any case, glad I could help!
    If the above doesn't make sense to you, ignore it, but remember it - might be useful!
    And if you just randomly taught yourself to program, well... you're just like me!

  7. #7
    Indegon is offline Member
    Join Date
    Nov 2010
    Location
    My own world
    Posts
    15
    Rep Power
    0

Similar Threads

  1. Lock a folder
    By nchouhan15@gmail.com in forum New To Java
    Replies: 4
    Last Post: 03-13-2012, 09:33 AM
  2. lock on class
    By rajinder5 in forum New To Java
    Replies: 0
    Last Post: 10-13-2010, 11:08 AM
  3. How to enable Windows Lock?
    By chyrl in forum Advanced Java
    Replies: 2
    Last Post: 07-11-2010, 04:45 AM
  4. Logger and file lock
    By segolas in forum Advanced Java
    Replies: 3
    Last Post: 01-14-2010, 05:02 PM
  5. lock a file
    By flaca in forum New To Java
    Replies: 3
    Last Post: 08-19-2008, 04:00 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •