Results 1 to 3 of 3
  1. #1
    McChill is offline Member
    Join Date
    Feb 2009
    Rep Power

    Default JTextField not visible?

    I want my program to display a text field. The text will be a random spanish word or phrase. Then I enter in the correct english translation. I want it to wait for 5 seconds after I enter text. Then set the text to a new word, wait for my translation, wait 5 seconds, etc etc until I close the program.

    Java Code:
    import java.util.ArrayList;
    import java.util.Random;
    public class Library {
    	public ArrayList<String> spanish = new ArrayList<String>();		//make a spanish list
    	public ArrayList<String> english = new ArrayList<String>();		//make an english list
    	static Random random = new Random();		//make a random object
    	private int index;
    	public void setIndex(int index) {
    		this.index = index;
    	public int getIndex() {
    		return index;
    	//method to add all Strings to the lists
    	public void makeLists() {
    		spanish.add("Como te llamas?");	english.add("What is your name?");
    		spanish.add("Como se llama?");	english.add("What is his/her name?");
    		spanish.add("Por favor");		english.add("Please");
    	//method to grab a word or phrase from the spanish list
    	public String getOne() {
    		int x = random.nextInt(spanish.size());
    		return spanish.get(getIndex());
    Java Code:
    import javax.swing.*;
    import java.util.Random;
    public class Player extends Thread{
    	static JTextField text = new JTextField();		//make the text field
    	static Library library = new Library();			//make a library object
    	static Random random = new Random();			//make a random object
    	public static void main(String args[]) {
    		Player player = new Player();		//make a player
    		library.makeLists();				//make the lists
    		player.start();						//start
    	public void run() {
    		try {
    			text.setVisible(true);			//make the field visible
    			boolean loop = true;
    			while(loop) {					//loop forever
    				text.setText(library.getOne() + "\n");		//set the text to a random spanish phrase
    				InputStreamReader isr = new InputStreamReader(;
    				BufferedReader br = new BufferedReader(isr);
    					text.setText("Right!");		//if i enter in the correct english translation, i'm right
    				else text.setText("Wrong. The answer is " + library.english.get(library.getIndex()));		//if i'm wrong, display the right answer
    				Thread.sleep(5000);			//wait for 5 seconds
    		catch(Exception ex) {
    Whenever I run the program...nothing happens. Well I'm assuming SOMETHING happens, but no text field comes up on my screen. My screen just remains the same and eventually I click the red terminate button (in Eclipse) when I give up on waiting. Can anyone tell me what's wrong or point me in some direction please?? Thanks.

  2. #2
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Blog Entries
    Rep Power


    I assume that you're placing this JTextField into a JFrame or something similar right? Where's the code for this? If you're just playing with a JTextField and not placing it anywhere that has the ability to show it, you will see nothing.

    Bottom line here: I think that you will benefit greatly by going through the Sun Swing tutorial and learn how to create functioning GUI programs.

    Best of luck.
    Last edited by Fubarable; 05-20-2009 at 12:56 AM.

  3. #3
    skatefreak is offline Member
    Join Date
    Jan 2009
    Rep Power


    Fuba's right there but looking at your code, in class player it seems that bluej doesnt associate your closing bracket at the end of your if statment (line 31) but instead associates it with the bracket on line 35 after your else statment...
    Maybe there's a reason behind this i'm not familiar with? Could anyone clarify?
    Very perculiar indeed and got me curious heh.

    But yes, get onto java.sun and check out JFrames etc.
    Another alternative would be JOptionPanes to get the user input :)


Similar Threads

  1. Text are not visible in GUI
    By VinTiger in forum New To Java
    Replies: 5
    Last Post: 05-15-2009, 09:14 AM
  2. Tab or Table not visible
    By madhuvanthi2312 in forum SWT / JFace
    Replies: 1
    Last Post: 04-25-2009, 10:00 AM
  3. Replies: 1
    Last Post: 01-30-2009, 07:44 PM
  4. Address bar not visible
    By Akashchopra521 in forum New To Java
    Replies: 0
    Last Post: 12-03-2008, 08:13 AM
  5. constructor ... is not visible
    By emceenugget in forum New To Java
    Replies: 2
    Last Post: 09-27-2008, 06:12 AM

Posting Permissions

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