-
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.
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;
}
}
-
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.
-
You shouldn't mix GUI and console programs together like that
-
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?
-
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 :)
-
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!
-
lesson learned, thnx 4 the help & the advice.